Skip to content

Instantly share code, notes, and snippets.

@learncodeacademy
Created July 24, 2014 14:47
Show Gist options
  • Select an option

  • Save learncodeacademy/ebba574fc3f438c851ae to your computer and use it in GitHub Desktop.

Select an option

Save learncodeacademy/ebba574fc3f438c851ae to your computer and use it in GitHub Desktop.
Nginx Node Frontend / Load Balancer / Static Assets Caching
upstream project {
server 22.22.22.2:3000;
server 22.22.22.3:3000;
server 22.22.22.5:3000;
}
server {
listen 80;
location / {
proxy_pass http://project;
}
location ~* \.(css|js|gif|jpe?g|png)$ {
expires 168h;
}
location /api {
expires 10m;
}
}
@matt212
Copy link
Copy Markdown

matt212 commented Feb 26, 2016

Hi,
I am using pm2 for hosting nodejs webapps and pm2 has cluster mode for creating multiple instances

screenshot from 2016-02-26 17 21 53

How should I put my multiple instances in nginx for load balancing and reverse proxy operation as for now my pm2 enabled nodejs works on locahost:3000 port 80 and i have configure my nginx on port 81
Please suggest !

@oddoye-david
Copy link
Copy Markdown

put nginx on port 80 and your node application on say 8000, then in the upstream project block, use

server 127.0.0.1:8000

pm2 will automatically balance the load between the cluster.

@basedalexander
Copy link
Copy Markdown

Why doesn't nginx load balance to my box with an express app running ?

ngnix is running on box1 under 55.55.55.5
express app is running on box2 uder 55.55.55.2:3000

When I'm going to 55.55.55.5 I'm getting this - Welcome to nginx! bla bla bla

upstream project {
  server 55.55.55.2:3000
}

server {
  listen 80;

  location / {
    proxy_pass http://project;
  }
}

@basedalexander
Copy link
Copy Markdown

SOLVED

The issue was because of default conf file in sites-available, just delete or rename it and you good to go.

@mustafa05deniz
Copy link
Copy Markdown

if something is wrong try like this

location ~ ^/views/((css|js|gif|jpe?g|png).*)$ {
expires 168h;

}

@Vinayk93
Copy link
Copy Markdown

can you please write this code in apache also.

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