# Question on server\_name\_in\_redirect

**URL:** <https://www.ruby-forum.com/t/question-on-server-name-in-redirect/193836>\
**Category:** NGINX\
**Created:** [August 25, 2010, 11:09am UTC](https://www.ruby-forum.com/t/question-on-server-name-in-redirect/193836 "2010-08-25T11:09:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 25, 2010, 11:09am UTC](https://www.ruby-forum.com/t/question-on-server-name-in-redirect/193836/1 "2010-08-25T11:09:16Z")

</div>

server {  
listen 80;  
server\_name [example.com:7788](http://example.com:7788) [example.com](http://example.com);  
server\_name\_in\_redirect on;  
root /var/www/html/exampel/;  
index index.php index.html;

According to the wiki, this should automatically redirect any request  
to [example.com](http://example.com) to [example.com:7788](http://example.com:7788)

If server\_name\_in\_redirect is on, then Nginx will use the first value  
of the server\_name directive for redirects. If server\_name\_in\_redirect  
is off, then nginx will use the requested Host header.  
[http://wiki.nginx.org/NginxHttpCoreModule#server\_name\_in\_redirect](http://wiki.nginx.org/NginxHttpCoreModule#server_name_in_redirect)

But it didn’t work in my case. On nginx 0.8.44

So i had to use

if ($host ~\* [shop.2u4u.com.cn](http://shop.2u4u.com.cn) ) {  
rewrite ^(.\*)$ http://shop.2u4u.com.cn:7788$1 ; # $1 contains  
‘/foo’, not ‘[www.mydomain.com/foo](http://www.mydomain.com/foo)’  
}

Which is not clean and fell in the category of nginx pitfall ☹

> **[Pitfalls and Common Mistakes | NGINX](https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/)**
>
> This page outlines some of the NGINX configuration issues that we see frequently and then explains how to resolve them.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 25, 2010, 1:28pm UTC](https://www.ruby-forum.com/t/question-on-server-name-in-redirect/193836/2 "2010-08-25T13:28:35Z")

</div>

Hello!

On Wed, Aug 25, 2010 at 05:08:31PM +0800, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If server\_name\_in\_redirect is on, then Nginx will use the first value  
> of the server\_name directive for redirects. If server\_name\_in\_redirect  
> is off, then nginx will use the requested Host header.  
> [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/NginxHttpCoreModule#server_name_in_redirect)
> 
> But it didn’t work in my case. On nginx 0.8.44

You misunderstood wiki. It says “nginx will use first value …  
for redirects”, but it doesn’t say anything about “all request  
will be redirected”. Server name will be only used if nginx  
would issue redirect for some reason (e.g. directory redirect to  
add trailing slash).

Note well: using port in server\_name doesn’t make sense as nginx  
only match hostname part of a request’s Host header against it.  
Distinction between different ports is made at socket level.

If you want all requests to port 80 to be redirected to port 7788  
you should write something like this:

```
server {
    listen 80;
    server_name example.com;
    rewrite ^ http://example.com:7788$request_uri?;
}

server {
    listen 7788;
    server_name example.com;
    ... here actual request processing ...
}

```

Maxim D.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 26, 2010, 4:24am UTC](https://www.ruby-forum.com/t/question-on-server-name-in-redirect/193836/3 "2010-08-26T04:24:23Z")

</div>

Thank you for your explanation! Your solution is much elegant!

I have a follow up on this. Since the redirect is used for “issue  
redirect for some reason (e.g. directory redirect to add trailing  
slash).”, then, supposedly i can do this:

```
location ~ ^/download/.*$ {
# the rewrite works!
rewrite ^ http://example.com:7788$request_uri?;
}

```

It works! 🙂 Thank you! Now the site is running on deafutl port 80.  
Users won’t notice the download is through port 7788 unless they look  
at the header. Much better!

> You misunderstood wiki. Â It says “nginx will use first value …  
> for redirects”, but it doesn’t say anything about “all request  
> will be redirected”. Â Server name will be only used if nginx  
> would issue redirect for some reason (e.g. directory redirect to  
> add trailing slash).
