Skip to content

Instantly share code, notes, and snippets.

@turtlesoupy
Created July 8, 2012 21:16
Show Gist options
  • Save turtlesoupy/3072833 to your computer and use it in GitHub Desktop.
Save turtlesoupy/3072833 to your computer and use it in GitHub Desktop.
node.js upstream nginx config
http {
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=one:8m max_size=3000m inactive=600m;
proxy_temp_path /var/tmp;
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
gzip_comp_level 6;
gzip_vary on;
gzip_min_length 1000;
gzip_proxied any;
gzip_types text/plain text/html text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_buffers 16 8k;
ssl_certificate /some/location/sillyfacesociety.com.bundle.crt;
ssl_certificate_key /some/location/sillyfacesociety.com.key;
ssl_protocols SSLv3 TLSv1;
ssl_ciphers HIGH:!aNULL:!MD5;
upstream silly_face_society_upstream {
server 127.0.0.1:61337;
server 127.0.0.1:61338;
keepalive 64;
}
server {
listen 80;
listen 443 ssl;
server_name sillyfacesociety.com;
return 301 $scheme://www.sillyfacesociety.com$request_uri;
}
server {
listen 80;
listen 443 ssl;
server_name www.sillyfacesociety.com;
error_page 502 /errors/502.html;
location ~ ^/(images/|img/|javascript/|js/|css/|stylesheets/|flash/|media/|static/|robots.txt|humans.txt|favicon.ico) {
root /usr/local/silly_face_society/node/public;
access_log off;
expires max;
}
location /errors {
internal;
alias /usr/local/silly_face_society/node/public/errors;
}
location / {
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_cache one;
proxy_cache_key sfs$request_uri$scheme;
proxy_pass http://silly_face_society_upstream;
}
}
}
@vendruscolo
Copy link

Hey! Nginx authors discourage using an if to handle the www to non-www redirection!
http://wiki.nginx.org/Pitfalls#Server_Name

Use two server blocks instead :)

@turtlesoupy
Copy link
Author

Hey! You are right. I've updated the gist to have better behaviour. Thanks!

@acornies
Copy link

Hey, I'm having an issue with my nodejs + nginx setup. I get intermittent 502 bad gateway errors, so I added:

proxy_next_upstream error timeout http_502;

nodejs app runs on localhost:3000, 3001, 3002 and I have something similar to this

upstream silly_face_society_upstream {
server 127.0.0.1:3000;
server 127.0.0.1:3001;
server 127.0.0.1:3002;
keepalive 64;
}

Noticed you don't have that in here... maybe because it's not working as I expect. Any help would be appreciated.

@philipheinser
Copy link

I use this config and everything is working expect for when i try to access www.sillyfacesociety.com/ then i get an 404 from nginx. so www.sillyfacesociety.com/everythnigelse is working.

@ccnokes
Copy link

ccnokes commented Feb 13, 2016

Thanks for posting this 😸. Is there any way to test and verify the proxy caching behavior is working as expected?

@ccnokes
Copy link

ccnokes commented Feb 13, 2016

Well, to answer my own question:
In the location / block in this example, you can add this line:

add_header X-Cache-Status $upstream_cache_status;

which will output if it's a HIT, MISS, or EXPIRED in the X-Cache-Status header. Source: http://serverfault.com/questions/434915/nginix-proxy-caching-how-to-check-if-it-is-working

Nginx's caching behavior seems to respect other caching configuration you have on that resource, for example, if you have a Cache-Control: no-cache header, Nginx won't cache it and that header's value will always be MISS.

@andrewmclagan
Copy link

gzip_types text/html

Will generally throw an error as its included by default.

Copy link

ghost commented Sep 14, 2017

I've just installed nginx, and am following your tutorial on how to use nginx to avoid node.js load

Is this gist meant to replace the entire default nginx.conf or just the sections of it that the gist includes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment