2

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? :)

ngw
  • 21
  • 2

1 Answers1

0

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.

jeffatrackaid
  • 4,142
  • 19
  • 22