Skip to content

Instantly share code, notes, and snippets.

@joemccann
Created October 25, 2010 02:06
Show Gist options
  • Save joemccann/644282 to your computer and use it in GitHub Desktop.
Save joemccann/644282 to your computer and use it in GitHub Desktop.
Set up nginx as a reverse proxy to node.js.

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, www.foo.com 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.

  1. nginx listens on port 80.
  2. 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 http://127.0.0.1:8124; #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.

@quique123
Copy link

Ive set it up on a raspberry pi2. The site is at:

www/subdomain.domain.com/aism/index.html && the nodejs app is here too

from another computer on the network I can see the index.html but the app doesnt work. The app works fine remotely thru the router when on http:

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