# Http -\> https redirection, with a twist?

**URL:** <https://www.ruby-forum.com/t/http-https-redirection-with-a-twist/217380>\
**Category:** NGINX\
**Created:** [February 29, 2012, 3:07am UTC](https://www.ruby-forum.com/t/http-https-redirection-with-a-twist/217380 "2012-02-29T03:07:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [February 29, 2012, 3:07am UTC](https://www.ruby-forum.com/t/http-https-redirection-with-a-twist/217380/1 "2012-02-29T03:07:46Z")

</div>

Folks,

I’ve been trying to figure out how to set this up, I’ve gone through as  
much of the web site and wiki as I can, and I’ve searched on the net as  
much as I can. I’m still stumped.

We have several different servers that we want to redirect from port 80,  
and most of them will land on the same machine but on port 443.  
However, one of those needs to land on a different port – 8443.

If this were a simple redirect without a twist, I’d probably go with  
something like the example shown at  
[http://serverfault.com/questions/192382/nginx-redirect-all-traffic/263091#263091](http://serverfault.com/questions/192382/nginx-redirect-all-traffic/263091#263091),  
although because I’m doing a one-to-one mapping of multiple FQDNs, and  
not just mapping a bunch of FQDNs to a single name, I’d be inclined to  
use an example more like this:

server {  
server\_name [a.domain.com](http://a.domain.com) [c.domain.com](http://c.domain.com) [d.domain.com](http://d.domain.com); # you can serve  
any number of redirects from here…  
listen 80;  
rewrite ^ https://$host$1$uri$is\_arg$args permanent;  
}

But with the single server definition listening to port 80 on all  
interfaces, I don’t see how to make that one FQDN get redirected to port  
8443 instead of port 443.

Am I missing something obvious here?

–  
Brad K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
LinkedIn Profile: [http://tinyurl.com/y8kpxu](http://tinyurl.com/y8kpxu)

---

<div class="post-metadata">

**Author:** ![bradkenyon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c68b51/32.png) [@bradkenyon](https://www.ruby-forum.com/u/bradkenyon)\
**Post date:** [February 29, 2012, 3:20am UTC](https://www.ruby-forum.com/t/http-https-redirection-with-a-twist/217380/2 "2012-02-29T03:20:46Z")

</div>

On 29 Fev 2012 03h07 CET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If this were a simple redirect without a twist, I’d probably go with  
> But with the single server definition listening to port 80 on all  
> interfaces, I don’t see how to make that one FQDN get redirected to  
> port 8443 instead of port 443.
> 
> Am I missing something obvious here?

If I understood correctly. Try:

server {  
server\_name [a.domain.com](http://a.domain.com) [c.domain.com](http://c.domain.com) [d.domain.com](http://d.domain.com);  
listen 80;  
return 301 https://$host:8443$request\_uri;  
}

You can use a wildcard to match all subdomains. Perhaps it suits you:

server {  
server\_name \*.domain.com; # this is more generic [1]  
listen 80;  
return 301 https://$host:8443$request\_uri;  
}

— appa

[1] [Server names](http://nginx.org/en/docs/http/server_names.html#wildcard_names)

---

<div class="post-metadata">

**Author:** ![bradkenyon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c68b51/32.png) [@bradkenyon](https://www.ruby-forum.com/u/bradkenyon)\
**Post date:** [February 29, 2012, 4:26am UTC](https://www.ruby-forum.com/t/http-https-redirection-with-a-twist/217380/3 "2012-02-29T04:26:44Z")

</div>

On Feb 28, 2012, at 8:20 PM, Antnio P. P. Almeida wrote:

> If I understood correctly. Try:
> 
> server {  
> server\_name [a.domain.com](http://a.domain.com) [c.domain.com](http://c.domain.com) [d.domain.com](http://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.

> You can use a wildcard to match all subdomains. Perhaps it suits you:
> 
> server {  
> server\_name \*.domain.com; # this is more generic [1]  
> listen 80;  
> return 301 https://$host:8443$request\_uri;  
> }

I would like to avoid wildcards because they’re not going to happen in  
the real world (our list of sites that we serve is static), and I want  
to prevent redirects from happening for anything but the real sites that  
we do actually serve.

The only queries that would be coming into us that would match the  
wildcard and would NOT match the static list of sites would be people  
who are fishing around for security vulnerabilities or other types of  
less intelligent robots. I don’t want them causing any further load on  
our systems than we will already have.

–  
Brad K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
LinkedIn Profile: [http://tinyurl.com/y8kpxu](http://tinyurl.com/y8kpxu)

---

<div class="post-metadata">

**Author:** ![bradkenyon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c68b51/32.png) [@bradkenyon](https://www.ruby-forum.com/u/bradkenyon)\
**Post date:** [February 29, 2012, 4:45am UTC](https://www.ruby-forum.com/t/http-https-redirection-with-a-twist/217380/4 "2012-02-29T04:45:59Z")

</div>

On Feb 28, 2012, at 9:42 PM, Antnio P. P. Almeida wrote:

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

I didn’t realize that you could have multiple server blocks that were  
listening to the same port. That was the piece I was missing!

> server\_name [a.domain.com](http://a.domain.com) [c.domain.com](http://c.domain.com) [d.domain.com](http://d.domain.com) [f.domain.com](http://f.domain.com); # list all  
> domains  
> listen 80;  
> return 301 https://$host:$redirect\_port$request\_uri;  
> }

Ahh, that’s very cool, too. Now I have two solutions for just the one  
problem.

Thanks!

–  
Brad K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
LinkedIn Profile: [http://tinyurl.com/y8kpxu](http://tinyurl.com/y8kpxu)

---

<div class="post-metadata">

**Author:** ![bradkenyon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c68b51/32.png) [@bradkenyon](https://www.ruby-forum.com/u/bradkenyon)\
**Post date:** [February 29, 2012, 4:43am UTC](https://www.ruby-forum.com/t/http-https-redirection-with-a-twist/217380/5 "2012-02-29T04:43:30Z")

</div>

On 29 Fev 2012 04h26 CET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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](http://a.domain.com) [c.domain.com](http://c.domain.com) [d.domain.com](http://d.domain.com); # redirect to  
8443  
listen 80;  
return 301 https://$host:8443$request\_uri;  
}

server {  
server\_name [e.domain.com](http://e.domain.com) [f.domain.com](http://f.domain.com) [g.domain.com](http://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](http://f.domain.com) 8443; # this is the domain that redirects to 8443  
}

server {  
server\_name [a.domain.com](http://a.domain.com) [c.domain.com](http://c.domain.com) [d.domain.com](http://d.domain.com) [f.domain.com](http://f.domain.com); #  
list all domains  
listen 80;  
return 301 https://$host:$redirect\_port$request\_uri;  
}

— appa
