Gelin Luo
Gelin Luo

Reputation: 14373

nginx $server_name is always localhost

I have an nginx 1.4.5 server running on an AWS EC2 instance. I have two conf file: the default.conf and myapp.conf. The default.conf listen to localhost, and myapp.conf listen to myapp.mydomain.com.

However I found even I type in myapp.mydomain.com in the browser, it always load the default.conf. I've changed the log_format in nginx.conf file add a "$server_name" field to check the server name of each request. I found it's always localhost.

Any one has any idea about this?

Updates with configuration info

the nginx.conf

server {
     listen       80;
     server_name  localhost;

     #charset koi8-r;
     #access_log  /var/log/nginx/log/host.access.log  main;

     location / {
         root   /usr/share/nginx/html;
         index  index.html index.htm;
     }
...
}

The myapp.conf

 server {
     listen 80;
     server_name myapp.mydomain.com;

     access_log /var/log/nginx/myapp.log;

     location / {
         add_header P3P 'CP="CAO PSA OUR"';
         proxy_pass http://127.0.0.1:8080/myapp/;
         set $ssl off;
         if ($scheme = https) {
             set $ssl on;
         }
         proxy_set_header X-Forwarded-Ssl $ssl;
         proxy_set_header Host $host;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_set_header X-Real-IP $remote_addr;
     }
 }

And yes I am behind two firewalls, one is iptable, the other one is AWS built in secure group

Upvotes: 4

Views: 5593

Answers (2)

Uri Agassi
Uri Agassi

Reputation: 37409

Someone with a similar problem got it fixed by:

I've fixed this now, in case anyone has the same problem as me it was to do with "server_names_hash_bucket_size 64;" in /etc/nginx/nginx.conf.

That line was commented out by default so I uncommented and restarted nginx and it works fine now.

Upvotes: 1

Rico
Rico

Reputation: 61661

I'm assuming that you are running the browser and nginx on the same machine. In that case myapp.mydomain.com probably resolves to localhost or 127.0.0.1 thus nginx thinks it's going to localhost.

Try adding this to your /etc/hosts file:

<external IP of your host>  myapp.mydomain.com

You should also try accessing your nginx server from another machine.

Upvotes: 0

Related Questions