Can rewrite change port? for eg, 80 to 443

Igor Sysoev is at rambler-co.ru
Mon Jul 21 14:10:07 MSD 2008


On Mon, Jul 21, 2008 at 01:40:09AM -0700, Mansoor Peerbhoy wrote:

> 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 http://wiki.codemongers.com/NginxHttpCoreModule for the list of variables

"if ($uri ...)" expressions are good candidates for locations:

   # plain server
   location = /logging.php$ {
       rewrite ^/(.*)$ https://$host/$1 redirect;
   }

   # https server

   location / {
       rewrite ^/(.*)$ http://$host/$1 redirect;
   }

   location = /logging.php$ {
       ...
   }


> 
> 
> 
> 
> ----- "baalchina" <baalchina at gmail.com> wrote:
> 
> | From: "baalchina" <baalchina at gmail.com>
> | To: nginx at sysoev.ru
> | 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
> | http://mysite.com/logging.php, nginx will automatic change the url to
> | https://mysite.com/logging.php. And when my user click any other
> | links
> | in my site, for example, https://mysite.com/index.php, 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
> | coding...so, I want to settle this in nginx rewrite...
> | 
> | -- 
> | from:baalchina

-- 
Igor Sysoev
http://sysoev.ru/en/





More information about the nginx mailing list