Last active

Set up nginx as a reverse proxy to node.js.

  • Download Gist
nginx + node

The idea is to have nginx installed and node installed. I will extend this gist to include how to install those as well, but at the moment, the following assumes you have nginx 0.7.62 and node 0.2.3 installed on a Linux distro (I used Ubuntu).

In a nutshell,

1) nginx is used to serve static files (css, js, images, etc.) 2) node serves all the "dynamic" stuff.

So for example, request comes and your css, js, and images get served thru nginx while everything else (the request for say index.html or "/") gets served through node.

3) nginx listens on port 80. 4) node listens on port 8124 (for this example only. you can change this port for your node app).

So in your /etc/nginx/sites-available/default modify your location / stanza and add the second stanza of this code block:

location / { proxy_pass; #this is the ip:port where your node app runs root /var/www/yoursitename; expires 30d; #uncomment this is you want to name an index file: #index index.php index.html;
access_log off; }

    location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$ {
        root   /var/www/yoursitename/public;

Note: I did not change the /etc/nginx/nginx.conf file. It is still the default nginx.conf from the nginx installation.

Now, restart nginx.

/etc/init.d/nginx restart

(Re)start your node app.

node /path/to/your/node/app.js

Navigate to your site and verify.

Enjoy blazing fast static files and blazing fast dynamic content.

I'm add:

location /socket/ {

load Return

<?xml version="1.0"?><!DOCTYPE cross-domain-policy SYSTEM ""><cross-domain-policy><allow-access-from domain="*" to-ports="*"/></cross-domain-policy>

What is the problem?


Thanks a lot man!! :)

well what will be the pcre if the static files needs to be served from the location /test/ location instead of root?

What portions would be changed in this:
location ~* ^.+.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$ {

as long as /test/ is within the document root you don't need to change anything.

try this if that doesn't work:

location ~* .*.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$ {
access_log off;
expires max;
root /var/www/yoursitename/public;

Very helpful; thanks!

"I will extend this gist to include how to install those as well." Did you ever get a chance to write those? This gist is handy. :)

When nodejs provides sendfile(), how would nginx be beneficial over nodejs to serve static files ?

@sisingh - Nginx is a lot faster than node at serving static files & performing optimizations such as gzip and caching. That is why even though node can serve static files as you correctly pointed out, this is not the best solution. Depending on your web server backend, it is best to let nginx, Apache, or IIS handle the serving of static files.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.