Staging/release nginx config I don't like how this config has so much duplication. Any suggestions on how to at least shorten it somewhat? Normally we only have one server block as the staging server is separate from production, but the client has gotten their own server so we'll need to stage on that.

upstream upgrade-release{
    server 127.0.0.1:8000;
}

upstream upgrade-staging{
    server 127.0.0.1:8010;
}

server {
    listen 80;
    server_name www.upgrade.sg upgrade.sg;

    access_log  /var/log/nginx/upgrade.access.log;
    error_log   /var/log/nginx/upgrade.error.log;

    root /home/upgrade/npcec/release/src/npcec;

    location /media/ {
        expires max;
        access_log off;
    }

    location /static/ {
        expires max;
        access_log off;
    }

    location /robots.txt {
        alias /home/upgrade/npcec/release/src/npcec/static/robots.txt;
        access_log off;
    }

    location /favicon.ico {
        alias /home/upgrade/npcec/release/src/npcec/static/favicon.ico;
        expires max;
        access_log off;
    }

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://upgrade-release;
    }
}

server {
    listen 80;
    server_name staging.upgrade.sg;

    access_log  /var/log/nginx/upgrade.access.log;
    error_log   /var/log/nginx/upgrade.error.log;

    root /home/upgrade/npcec/staging/src/npcec;

    location /media/ {
        expires max;
        access_log off;
    }

    location /static/ {
        expires max;
        access_log off;
    }

    location /robots.txt {
        alias /home/upgrade/npcec/staging/src/npcec/static/robots.txt;
        access_log off;
    }

    location /favicon.ico {
        alias /home/upgrade/npcec/staging/src/npcec/static/favicon.ico;
        expires max;
        access_log off;
    }

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://upgrade-staging;
    }
}
My little attempt basically takes items that are repeated and puts them into their own separate conf files and just include them into the main conf files. On my servers, I structure them as such:

global
    include1.conf
    include2.conf
    include3.conf
sites-available
    site1.conf
    site2.conf


And the includes are included in the SA conf files. Not good for performance, but negligible.

Anyways, here's an idea:

upstream upgrade-release{
    server 127.0.0.1:8000;
}

upstream upgrade-staging{
    server 127.0.0.1:8010;
}

server {
    listen 80;
    server_name www.upgrade.sg upgrade.sg;

    access_log  /var/log/nginx/upgrade.access.log;
    error_log   /var/log/nginx/upgrade.error.log;

    root /home/upgrade/npcec/release/src/npcec;

    include media_static.conf;

    location /robots.txt {
        alias /home/upgrade/npcec/release/src/npcec/static/robots.txt;
        access_log off;
    }

    location /favicon.ico {
        alias /home/upgrade/npcec/release/src/npcec/static/favicon.ico;
        expires max;
        access_log off;
    }

    location / {
        include proxy.conf;
        proxy_pass http://upgrade-release;
    }
}

server {
    listen 80;
    server_name staging.upgrade.sg;

    access_log  /var/log/nginx/upgrade.access.log;
    error_log   /var/log/nginx/upgrade.error.log;

    root /home/upgrade/npcec/staging/src/npcec;

    include media_static.conf;

    location /robots.txt {
        alias /home/upgrade/npcec/staging/src/npcec/static/robots.txt;
        access_log off;
    }

    location /favicon.ico {
        alias /home/upgrade/npcec/staging/src/npcec/static/favicon.ico;
        expires max;
        access_log off;
    }

    location / {
        include proxy.conf;
        proxy_pass http://upgrade-staging;
    }
}


In media_static.conf: 

location /media/ {
    expires max;
    access_log off;
}

location /static/ {
    expires max;
    access_log off;
}


In proxy.conf:

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;


Also, instead of proxy_pass http:// perhaps consider using proxy_pass $scheme. Perhaps someone with more experience with Nginx confs will follow up with a way to store certain bits and pieces in variables or will offer a better solution than mine (I for one would love to know a better way of doing things)