Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm having some troubles setting up nginx to serve my staging website. What I did is change the server_name but for some reasons it just doesn't work.

The url scheme is "domain.foo" is production, "staging.domain.foo" is staging, "foobar.domain.foo" is a web service, "foobar.staging.domain.foo" is the staging version of the same webserver, ".domain.foo" is routed to serve some s3 static HTML, ".staging.domain.foo" is routed to serve some s3 static HTML in another bucket. All production urls work and are correctly configured, all staging urls doesn't work.

Here is my conf file. You will see some duplication, I will gladly accept any correction/optimization, I'm a coder and configuring servers is definitely not my thing (but I'm eager to learn and improve...).

server {
    listen   80; ## listen for ipv4
    server_name "domain.foo" "www.domain.foo" default_server;
    access_log  /var/log/nginx/access.log;
    client_max_body_size 5M;
    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_redirect off;
        location ~* \.(jpg|jpeg|gif|png|ico|css|bmp|js|html)$ {
            access_log off;
            expires max;
            root /home/foo/Foo/current/public;
            break;
        }
        if ($host ~ 'www.domain.foo') {
            rewrite ^/(.*)$ http://domain/foo/$1 permanent;
        }
        proxy_pass http://production;
        break;
    }
}

server {
    listen 80;
    server_name "staging.domain.foo";
    access_log  /var/log/nginx/access.staging.log;
    error_log /var/log/nginx/error.staging.log;
    client_max_body_size 5M;
    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_redirect off;
        proxy_pass http://staging;
        break;
    }
}

server {
    listen   80; ## listen for ipv4
    server_name "foobar.domain.foo";
    access_log  /var/log/nginx/access.log;
    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_redirect off;
        if ($host = 'foobar.domain.foo') {
            proxy_pass http://foobar;
            break;
        }
    }
}

server {
    listen   80; ## listen for ipv4
    server_name foobar.staging.domain.foo;
    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_redirect off;
            proxy_pass http://foobar_staging;
            break;
    }
}

server {
    listen 80;
    server_name "~^(.+)\.domain\.foo$";
    location / {
        proxy_intercept_errors on;
        error_page 404 = http://domain.foo/404;
        set $subdomain $1;
        rewrite /$ "/$subdomain/index.html" break;
        rewrite ^ /$subdomain$request_uri? break;
        proxy_pass http://bucket.domain.foo.s3.amazonaws.com;
    }
}

server {
    listen 80;
    server_name "~^(.+)\.staging\.domain\.foo$";
    location / {
        proxy_intercept_errors on;
        set $subdomain $1;
        rewrite /$ "/$subdomain/index.html" break;
        rewrite ^ /$subdomain$request_uri? break;
        proxy_pass http://bucket.staging.domain.foo.s3.amazonaws.com;
    }
}

upstream production {
    server 111.255.111.110:8000;
    server 111.255.111.110:8001;
    server 111.255.111.110:8002;
    server 111.255.111.110:8003;
}

upstream staging {
    server 222.255.222.222:8000;
    server 222.255.222.222:8001;
}

upstream foobar {
    server 111.255.222.165:9000;
    server 111.255.222.165:9001;
    server 111.255.222.165:9002;
}

upstream foobar_staging {
    server 222.255.222.222:9000;
}

What happens now when I point my browser to staging.domain.foo is that it hangs. Can't find anything in the logs, but for example the access.staging.log and errors.staging.log are created.

Anybody has an idea? :)

share|improve this question
    
Can you telnet to 222.255.222.222:8000? –  quanta Nov 13 '11 at 2:21
    
I can open it with my browser, and it works correctly. –  ngw Nov 13 '11 at 22:11
    
To be more precise, yes, I can telnet :) –  ngw Nov 14 '11 at 1:26
    
basic question - DNS routing you properly to the box? Anything of interest in the logs (both nginx and syslog)? –  databyte Nov 16 '11 at 17:54
    
Have you check the top level error.log? default is /var/nginx/error.log. Which version of nginx are you using btw? –  number5 Dec 8 '11 at 11:22

1 Answer 1

DNS is often an issue here. Make sure both your client and server can resolve:

staging.domain.foo

If DNS is not setup for these, then update the /etc/hosts file on the server and on your desktop.

Nginx uses the Host: header to match server name:

Compares the Host header of the incoming HTTP request against the server { ... } blocks in the Nginx configuration files and selects the first one that matches.

http://wiki.nginx.org/HttpCoreModule#server

If a match is not found then the first listen stanza will be used.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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