Can rewrite change port? for eg, 80 to 443

Mansoor Peerbhoy mansoor at zimbra.com
Mon Jul 21 16:24:35 MSD 2008


Yes, that's right. Always better to have the rule inside a location block, rather than the main conf.
Just as a matter of interest, do we have .htaccess support in NGINX ?

----- "Maxim Dounin" <mdounin at mdounin.ru> wrote:

| From: "Maxim Dounin" <mdounin at mdounin.ru>
| To: nginx at sysoev.ru
| Sent: Monday, July 21, 2008 4:25:09 PM GMT +05:30 Chennai, Kolkata, Mumbai, New Delhi
| Subject: Re: Can rewrite change port? for eg, 80 to 443
|
| Hello!
| 
| On Mon, Jul 21, 2008 at 02:10:07PM +0400, Igor Sysoev wrote:
| 
| >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$ {
| 
| -   location = /logging.php$ { 
| +   location = /logging.php {
| 
| >       rewrite ^/(.*)$ https://$host/$1 redirect;
| >   }
| >
| >   # https server
| >
| >   location / {
| >       rewrite ^/(.*)$ http://$host/$1 redirect;
| >   }
| >
| >   location = /logging.php$ {
| 
| -   location = /logging.php$ { 
| +   location = /logging.php {
| 
| >       ...
| >   }
| 
| Maxim Dounin





More information about the nginx mailing list