2
\$\begingroup\$

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;
 }
}
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Oct 12, 2012 at 5:54
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

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)

answered Oct 12, 2012 at 17:22
\$\endgroup\$
2
  • 1
    \$\begingroup\$ +1 for seperating into multiple files - you can find something to compare to in the h5bp server config project \$\endgroup\$ Commented Nov 12, 2012 at 13:45
  • \$\begingroup\$ 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! \$\endgroup\$ Commented Nov 12, 2012 at 16:57

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.