Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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;
    }
}
share|improve this question
add comment

1 Answer

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)

share|improve this answer
1  
+1 for seperating into multiple files - you can find something to compare to in the h5bp server config project –  AD7six Nov 12 '12 at 13:45
    
That is an absolute treasure trove of conf files @AD7six! Downloaded them and will be implementing many in my servers. Thanks for sharing that resource! –  jsanc623 Nov 12 '12 at 16:57
add comment

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.