# Question on server\_name\_in\_redirect

**URL:** <https://www.ruby-forum.com/t/question-on-server-name-in-redirect/193837>\
**Category:** NGINX\
**Created:** [August 25, 2010, 11:10am UTC](https://www.ruby-forum.com/t/question-on-server-name-in-redirect/193837 "2010-08-25T11:10:27Z")\
**Posts on this page:** 1\
**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:10am UTC](https://www.ruby-forum.com/t/question-on-server-name-in-redirect/193837/1 "2010-08-25T11:10:27Z")

</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 ~\* [example.com](http://example.com)) {  
rewrite ^(.\*)$ http://example.com: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.
