# Using a proxied server as default\_server

**URL:** <https://www.ruby-forum.com/t/using-a-proxied-server-as-default-server/245472>\
**Category:** NGINX\
**Created:** [May 31, 2013, 4:16am UTC](https://www.ruby-forum.com/t/using-a-proxied-server-as-default-server/245472 "2013-05-31T04:16:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafa\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/958977/32.png) [@Rafa\_F](https://www.ruby-forum.com/u/Rafa_F)\
**Post date:** [May 31, 2013, 4:16am UTC](https://www.ruby-forum.com/t/using-a-proxied-server-as-default-server/245472/1 "2013-05-31T04:16:51Z")

</div>

Hi,

I have a tomcat app running behind nginx, it works, now I make it the  
default\_server, this works if it access with server name like  
[sample.com](http://sample.com),  
but if access with an IP, [http://192.168.1.1](http://192.168.1.1), it does not work, any  
idea ?  
Thanks,

server {  
listen 192.168.1.1:80 default\_server;  
server\_name [sample.com](http://sample.com)  
root /var/www/public\_html;  
location / {  
proxy\_set\_header X-Real-IP $remote\_addr;  
proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;  
proxy\_set\_header Host $http\_host;  
proxy\_set\_header X-NginX-Proxy true;  
proxy\_pass [http://127.0.0.1:8080](http://127.0.0.1:8080);  
proxy\_redirect off;  
}

Posted at Nginx Forum:

> **[using a proxied server as default\_server](https://forum.nginx.org/read.php?2,239707,239707#msg-239707)**
>
> Hi, I have a tomcat app running behind nginx, it works, now I make it the default\_server, this works if it access with server name like sample.com, but if access with an IP, http://192.168.1.1, it does not work, any idea ? Thanks, server { listen 192

---

<div class="post-metadata">

**Author:** ![angelochen960](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@angelochen960](https://www.ruby-forum.com/u/angelochen960)\
**Post date:** [May 31, 2013, 10:09am UTC](https://www.ruby-forum.com/t/using-a-proxied-server-as-default-server/245472/2 "2013-05-31T10:09:46Z")

</div>

On Thu, May 30, 2013 at 10:16:28PM -0400, angelochen960 wrote:

Hi there,

> I have a tomcat app running behind nginx, it works, now I make it the  
> default\_server, this works if it access with server name like [sample.com](http://sample.com),  
> but if access with an IP, [http://192.168.1.1](http://192.168.1.1), it does not work, any idea ?

What does “does not work” mean?

Be specific.

What do you do? (curl -i [http://192.168.1.1](http://192.168.1.1))  
What do you see? (The “wrong” content? An error message? Something  
else?)  
What do you expect to see? (The “right” content?)

You will probably find that it is easier for people to offer help and  
suggestions if you make it easy for them to do that.

In this particular case, what is expected to happen? I imagine it is:

browser makes request to nginx;  
nginx makes request to tomcat with some specific headers set;  
tomcat responds to nginx;  
nginx responds to browser

Can you see what actually does happen?

If you can find the first point in “what does happen” that doesn’t  
match “what should happen”, then you’ll have a good hint as to where  
the problem is.

Good luck,

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![angelochen960](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@angelochen960](https://www.ruby-forum.com/u/angelochen960)\
**Post date:** [May 31, 2013, 3:01pm UTC](https://www.ruby-forum.com/t/using-a-proxied-server-as-default-server/245472/3 "2013-05-31T15:01:55Z")

</div>

On Fri, May 31, 2013 at 07:30:22AM -0400, angelochen960 wrote:

Hi there,

> Sorry for not making it more specific, the issue is, the app in the tomcat  
> is a virtual host as well, so it checks ‘host’ field for ‘[sample.com](http://sample.com)’,

No worries.

Now that you have identified the problem, the possibilities for  
solutions  
are more obvious 🙂

For example, as an alternative to what you have already working, you  
could note that you have the following line in your config:

proxy\_set\_header Host $http\_host;

which sends the Host: header to the backend with the value of the  
$http\_host variable, which is the content of the incoming Host: header  
(and may be empty).

If your backend requires that this always be “[sample.com](http://sample.com)”, then you  
could instead use

proxy\_set\_header Host [sample.com](http://sample.com);

or possibly something like $server\_name that has the same value.

> default\_server with specific IP when accessed by a IP address like  
> [http://192.168.1.1/](http://192.168.1.1/), it will not have a ‘host’, thus when passed to the app  
> in the tomcat, it will not hit the right virtual host there, initially I was  
> thinking, probably nginx can insert the ‘host’ before proxy to tomcat to  
> make it work.

Strictly, it might have no Host, or it might have a Host of  
192.168.1.1. Either way, it is not what your tomcat expects (unless it  
is _also_ the default server for connections to this address).

And getting nginx to insert the Host header you want is one way to  
approach it.

Cheers,

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![angelochen960](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@angelochen960](https://www.ruby-forum.com/u/angelochen960)\
**Post date:** [May 31, 2013, 4:07pm UTC](https://www.ruby-forum.com/t/using-a-proxied-server-as-default-server/245472/4 "2013-05-31T16:07:21Z")

</div>

Hi Francis,

I think that’s a good approach, will give that a try, thanks,

Angelo

Posted at Nginx Forum:

> **[Re: using a proxied server as default\_server](https://forum.nginx.org/read.php?2,239707,239726#msg-239726)**
>
> Hi, I have a tomcat app running behind nginx, it works, now I make it the default\_server, this works if it access with server name like sample.com, but if access with an IP, http://192.168.1.1, it does not work, any idea ? Thanks, server { listen 192

---

<div class="post-metadata">

**Author:** ![angelochen960](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@angelochen960](https://www.ruby-forum.com/u/angelochen960)\
**Post date:** [May 31, 2013, 1:30pm UTC](https://www.ruby-forum.com/t/using-a-proxied-server-as-default-server/245472/5 "2013-05-31T13:30:45Z")

</div>

Hi,

Sorry for not making it more specific, the issue is, the app in the  
tomcat  
is a virtual host as well, so it checks ‘host’ field for ‘[sample.com](http://sample.com)’, a  
default\_server with specific IP when accessed by a IP address like  
[http://192.168.1.1/](http://192.168.1.1/), it will not have a ‘host’, thus when passed to the  
app  
in the tomcat, it will not hit the right virtual host there, initially I  
was  
thinking, probably nginx can insert the ‘host’ before proxy to tomcat to  
make it work.

however, do find a simple solution to this problem, in the default.conf,  
I  
added:

localtion / { return 302 [http://sample.com](http://sample.com);}

and remove the default\_server from my virtual host for [sample.com](http://sample.com)

this meets my requirement

thanks,

Angelo

Posted at Nginx Forum:

> **[Re: using a proxied server as default\_server](https://forum.nginx.org/read.php?2,239707,239716#msg-239716)**
>
> Hi, I have a tomcat app running behind nginx, it works, now I make it the default\_server, this works if it access with server name like sample.com, but if access with an IP, http://192.168.1.1, it does not work, any idea ? Thanks, server { listen 192
