Can rewrite change port? for eg, 80 to 443

Maxim Dounin mdounin at mdounin.ru
Mon Jul 21 14:55:09 MSD 2008


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