4

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

2
  • Can you post the config please, also - confirm that it isn't behind a proxy? Mar 3, 2014 at 21:20
  • The only thing i could think of is maybe you're trying to access a www site when you only defined one without in the nginx conf, is that the case?, try appending the www alternative in the server name if it's applicable. Mar 4, 2014 at 5:39

2 Answers 2

1

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.

0

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.

4
  • No nginx is running on AWS ec2 instance. Browser is on my laptop
    – Gelin Luo
    Mar 4, 2014 at 4:28
  • Then why the heck do you need localhost ?
    – Rico
    Mar 4, 2014 at 4:31
  • You are not running the default on localhost. If you were, it would have listen 127.0.0.1:80;. I assume that the missing semicolon on the server_name is lost in copying, but you should check for any typos.
    – user1600649
    Mar 4, 2014 at 9:39
  • That's my typo on SO (and fixed now). Not the typo in nginx conf file, otherwise nginx wouldn't get started.
    – Gelin Luo
    Mar 4, 2014 at 10:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.