Using a proxied server as default_server

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.168.1.1:80 default_server;
server_name 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;
proxy_redirect off;
}

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,239707,239707#msg-239707

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,
but if access with an IP, 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)
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. [email protected]

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’,

No worries.

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

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”, then you
could instead use

proxy_set_header Host 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/, 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. [email protected]

Hi Francis,

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

Angelo

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,239707,239726#msg-239726

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’, a
default_server with specific IP when accessed by a IP address like
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;}

and remove the default_server from my virtual host for sample.com

this meets my requirement

thanks,

Angelo

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,239707,239716#msg-239716