http -> https redirection, with a twist?

António P. P. Almeida appa at perusio.net
Wed Feb 29 03:42:37 UTC 2012


On 29 Fev 2012 04h26 CET, brad at shub-internet.org wrote:

> On Feb 28, 2012, at 8:20 PM, António P. P. Almeida wrote:
>
>> If I understood correctly. Try:
>>
>> server { 
>> server_name a.domain.com c.domain.com d.domain.com; 
>> listen 80; 
>> return 301 https://$host:8443$request_uri; 
>> }
>
> That works for the one site that needs to be redirected to port
> 8443, but doesn't work for any of the other sites that should
> instead be redirected to port 443.
>
> I need both sets of redirects -- most to port 443, but one to port
> 8443 instead.

Then just define two server blocks. One redirects to 443 and the other
to 8443.

server { 
    server_name a.domain.com c.domain.com d.domain.com; # redirect to 8443
    listen 80; 
    return 301 https://$host:8443$request_uri; 
}

server { 
    server_name e.domain.com f.domain.com g.domain.com; # redirect to 443
    listen 80; 
    return 301 https://$host$request_uri; 
}

split the server blocks according to the redirect you want.


Alternatively you could use map and a single server block. At the http
level.

map $host $redirect_port {
    hostnames;
    default 443;
    f.domain.com 8443; # this is the domain that redirects to 8443
}

server {
    server_name a.domain.com c.domain.com d.domain.com f.domain.com; # list all domains   
    listen 80;
    return 301 https://$host:$redirect_port$request_uri;
}

--- appa



More information about the nginx mailing list