Can rewrite change port? for eg, 80 to 443

Mansoor Peerbhoy mansoor at
Mon Jul 21 12:40:09 MSD 2008

In your plain http server block:

if ($uri ~* "/logging.php$") {
 rewrite ^/(.*)$ https://$host/$1 redirect;

In your https server block

if ($uri !~* "/logging.php$") {
 rewrite ^/(.*)$ http://$host/$1 redirect;

This is when you are using standard ports (80 for HTTP and 443 for HTTPS). If you are using non-standard ports (say 8080 for http, and 8443 for https, then in this case, you should have in your http block)

if ($uri ~* "/logging.php$") {
 rewrite ^/(.*)$ https://$host:8443/$1 redirect;

and correspondingly, in your https block, you should have:

if ($uri !~* "/logging.php$") {
 rewrite ^/(.*)$ http://$host:8080/$1 redirect;

The $host variable is the host portion of the URL that was used to reach your server
See for the list of variables


----- "baalchina" <baalchina at> wrote:

| From: "baalchina" <baalchina at>
| To: nginx at
| Sent: Monday, July 21, 2008 12:48:36 PM GMT +05:30 Chennai, Kolkata, Mumbai, New Delhi
| Subject: Can rewrite change port? for eg, 80 to 443
| Hi, everyone.
| I have a server running a php web forum. User logging into my site
| using 'logging.php'.
| I had set up a https server using nginx, but consdering server load,
| I
| just want my user using https in only logging.php.
| I want to settle this by using url rewrite. When my users click
|, nginx will automatic change the url to
| And when my user click any other
| links
| in my site, for example,, nginx will
| change https to http.
| Can rewrite work like this?
| Thank you!
| BTW,changing my forum codes may work, but I didn't know much about
| php
|, I want to settle this in nginx rewrite...
| -- 
| from:baalchina

More information about the nginx mailing list