Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;
}
}
@g8d3

This comment has been minimized.

Copy link

@g8d3 g8d3 commented Jan 26, 2015

where can I read what does ~* means? I read that ~ is to put a PCRE in front of that, but I don't understand the * (star space).

@molinto

This comment has been minimized.

Copy link

@molinto molinto commented Jan 30, 2015

Does this still need app.use(express.static('public/dir')); to work please?

@robinpaul

This comment has been minimized.

Copy link

@robinpaul robinpaul commented Mar 8, 2015

@molinto, yes still you will be needed!

@saurabhghewari

This comment has been minimized.

Copy link

@saurabhghewari saurabhghewari commented Apr 16, 2015

I didnt get how same port can be acquired by 3 diff servers? is upstream block containing 3 different IPs or 3 different servers are running on same port? Please help me i m totally confued.

@saurabhghewari

This comment has been minimized.

Copy link

@saurabhghewari saurabhghewari commented Apr 16, 2015

Ok. I got the answer what actual that means. Thanks a lot for the article and video.

@sergeifilippov

This comment has been minimized.

Copy link

@sergeifilippov sergeifilippov commented Jun 10, 2015

@hex20dec

This comment has been minimized.

Copy link

@hex20dec hex20dec commented Jun 17, 2015

@juanpastas The ~* means start case-insensitive regex, while ~ means case-sensitive regex.

@matt212

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

@oddoye-david oddoye-david commented Mar 4, 2016

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.

@thohoh

This comment has been minimized.

Copy link

@thohoh thohoh commented Mar 18, 2016

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

This comment has been minimized.

Copy link

@thohoh thohoh commented Mar 19, 2016

SOLVED

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

@mustafa05deniz

This comment has been minimized.

Copy link

@mustafa05deniz mustafa05deniz commented Aug 13, 2016

if something is wrong try like this

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

}

@Vinayk93

This comment has been minimized.

Copy link

@Vinayk93 Vinayk93 commented Sep 24, 2016

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