Unable to set nginx to serve my staging website
- by user100778
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? :)