Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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.

@paulomcnally

This comment has been minimized.

Show comment Hide comment
@paulomcnally

paulomcnally Mar 23, 2013

I'm add:

location /socket/ {
    proxy_pass http://127.0.0.1:3300;
  }

load domain.com/socket/ Return

<?xml version="1.0"?><!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"><cross-domain-policy><allow-access-from domain="*" to-ports="*"/></cross-domain-policy>

What is the problem?

thanks

I'm add:

location /socket/ {
    proxy_pass http://127.0.0.1:3300;
  }

load domain.com/socket/ Return

<?xml version="1.0"?><!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"><cross-domain-policy><allow-access-from domain="*" to-ports="*"/></cross-domain-policy>

What is the problem?

thanks

@mccarmo

This comment has been minimized.

Show comment Hide comment
@mccarmo

mccarmo Jul 11, 2013

Thanks a lot man!! :)

mccarmo commented Jul 11, 2013

Thanks a lot man!! :)

@bmrinal

This comment has been minimized.

Show comment Hide comment
@bmrinal

bmrinal Aug 2, 2013

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)$ {

bmrinal commented Aug 2, 2013

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)$ {

@mm0

This comment has been minimized.

Show comment Hide comment
@mm0

mm0 Aug 16, 2013

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;
}

mm0 commented Aug 16, 2013

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;
}

@kahboom

This comment has been minimized.

Show comment Hide comment
@kahboom

kahboom Aug 29, 2013

Very helpful; thanks!

kahboom commented Aug 29, 2013

Very helpful; thanks!

@ghost

This comment has been minimized.

Show comment Hide comment
@ghost

ghost Nov 17, 2013

3Q!

ghost commented Nov 17, 2013

3Q!

@mattvleming

This comment has been minimized.

Show comment Hide comment
@mattvleming

mattvleming Feb 1, 2014

"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. :)

"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. :)

@ghost

This comment has been minimized.

Show comment Hide comment
@ghost

ghost Feb 9, 2014

helpful, thanks.

ghost commented Feb 9, 2014

helpful, thanks.

@sisingh

This comment has been minimized.

Show comment Hide comment
@sisingh

sisingh Mar 2, 2014

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

sisingh commented Mar 2, 2014

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

@efosao

This comment has been minimized.

Show comment Hide comment
@efosao

efosao Apr 2, 2014

@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.

efosao commented Apr 2, 2014

@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.

@ayxos

This comment has been minimized.

Show comment Hide comment
@ayxos

ayxos Jun 22, 2014

Hi, whats happen if i have subroutes inside my node app? im not able to make it works, my app is running on 8002 port and i have the following conf:

  #proxy to a node app running on 8002 port
  location ^~ /auth/ {
      proxy_pass http://localhost:8002/;

redirect works, but when im trying to go to a any node app'subroute, nginx dont know what to do and returns a 404.

any idea?
thanks

ayxos commented Jun 22, 2014

Hi, whats happen if i have subroutes inside my node app? im not able to make it works, my app is running on 8002 port and i have the following conf:

  #proxy to a node app running on 8002 port
  location ^~ /auth/ {
      proxy_pass http://localhost:8002/;

redirect works, but when im trying to go to a any node app'subroute, nginx dont know what to do and returns a 404.

any idea?
thanks

@carlosazaustre

This comment has been minimized.

Show comment Hide comment
@carlosazaustre

carlosazaustre Jul 2, 2014

In my case I have API Rest on Node in localhost:3000 and I call this with:

location /api {
    proxy_pass              http://localhost:3000;
    proxy_buffering         on;
}

Now, my app on mydomain.com serves the frontend and if I call mydomain.com/api serves my API Rest.

I hope this help you 👍

In my case I have API Rest on Node in localhost:3000 and I call this with:

location /api {
    proxy_pass              http://localhost:3000;
    proxy_buffering         on;
}

Now, my app on mydomain.com serves the frontend and if I call mydomain.com/api serves my API Rest.

I hope this help you 👍

@murindwaz

This comment has been minimized.

Show comment Hide comment
@murindwaz

murindwaz Dec 31, 2014

@cybermarkus1 I am having exactly the same problem like you, do you remember how you solved that problem? thanks!

@cybermarkus1 I am having exactly the same problem like you, do you remember how you solved that problem? thanks!

@idriskhenchil

This comment has been minimized.

Show comment Hide comment
@idriskhenchil

idriskhenchil Jan 18, 2015

Same problem here @cybermarkus1

Same problem here @cybermarkus1

@alcreates

This comment has been minimized.

Show comment Hide comment
@alcreates

alcreates Aug 30, 2016

Amazing! Was up all night trying to figure this out. Thanks.

Amazing! Was up all night trying to figure this out. Thanks.

@Noel-A-Gonzalez

This comment has been minimized.

Show comment Hide comment
@Noel-A-Gonzalez

Noel-A-Gonzalez Aug 30, 2016

Hello!
I have several applications in node.js, and I want to run with nginx to redirect me. I can not achieve it is to manage the routing of express good.

I have the following in node:

/ * GET home page. * /
router.get ( '/', function (req, res, next) {
  res.render ( 'index.html');
});

/ * GET dashboard * /
router.get ( '/ timelife', function (req, res, next) {
    res.render ( 'admin / timeLife.html');
});

and my nginx configuration is as follows:
location / editor / {
        proxy_set_header X-Real-IP $ remote_addr;
        proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
        Host proxy_set_header $ HTTP_HOST;
        proxy_set_header X-NginX-Proxy true;
        PROXY_PASS http: //190.28.3615: 3001 /;

        proxy_redirect off;
        proxy_http_version 1.1;
        Upgrade proxy_set_header $ http_upgrade;
        proxy_set_header Connection "upgrade";

        proxy_redirect off;
        proxy_set_header X-Forwarded-Proto $ scheme;
        proxy_cache_key sfs $ request_uri $ scheme;
    }

when I try to access http://mydomain.com/editor/, I redirected the product http://mydomain.com/login/ and shows me 404 error.

Any idea how to fix this?

Hello!
I have several applications in node.js, and I want to run with nginx to redirect me. I can not achieve it is to manage the routing of express good.

I have the following in node:

/ * GET home page. * /
router.get ( '/', function (req, res, next) {
  res.render ( 'index.html');
});

/ * GET dashboard * /
router.get ( '/ timelife', function (req, res, next) {
    res.render ( 'admin / timeLife.html');
});

and my nginx configuration is as follows:
location / editor / {
        proxy_set_header X-Real-IP $ remote_addr;
        proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
        Host proxy_set_header $ HTTP_HOST;
        proxy_set_header X-NginX-Proxy true;
        PROXY_PASS http: //190.28.3615: 3001 /;

        proxy_redirect off;
        proxy_http_version 1.1;
        Upgrade proxy_set_header $ http_upgrade;
        proxy_set_header Connection "upgrade";

        proxy_redirect off;
        proxy_set_header X-Forwarded-Proto $ scheme;
        proxy_cache_key sfs $ request_uri $ scheme;
    }

when I try to access http://mydomain.com/editor/, I redirected the product http://mydomain.com/login/ and shows me 404 error.

Any idea how to fix this?

@quique123

This comment has been minimized.

Show comment Hide comment
@quique123

quique123 Apr 25, 2017

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:

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