Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save shukriadams/1f4c417209783463ccc08a6cf9319b7d to your computer and use it in GitHub Desktop.

Select an option

Save shukriadams/1f4c417209783463ccc08a6cf9319b7d to your computer and use it in GitHub Desktop.
Hosting multiple nodejs apps on one server with nginx
Nginx is perfect for hosting multiple nodejs apps on a single box, and easier to set up than Apache if you're a noob like me.
1 - install nginx :
sudo apt-get install nginx -y
2 - create an nginx config file for your site - you can keep each site's config in its own file
sudo gedit /etc/nginx/conf.d/myFirstSite.conf
3 - Set its content.
server {
listen 80;
# add your public and local-machine domain names here
server_name myfirstsite.com myfirstsite.local;
# optional : if your node app has static content, let nginx serve these directly
location ~ \.(css|js) {
root /path/to/static/content;
access_log off;
expires max;
}
# add a location entry for each name in your domain list
location / {
rewrite ^/myfirstsite.com?(.*) /$1 break;
# the nodejs app for this site runs at this port number
proxy_pass http://127.0.0.1:3000;
}
location / {
rewrite ^/myfirstsite.local?(.*) /$1 break;
proxy_pass http://127.0.0.1:3000;
}
}
A simpler version:
server {
listen 80;
server_name www.example.com;
location / {
rewrite ^/www.example.com?(.*) /$1 break;
proxy_pass http://127.0.0.1:3000;
}
}
4 - Open your hosts file
sudo gedit /etc/hosts
5 - Add local domain names
127.0.0.1 myfirstsite.local
6 - Restart nginx
sudo nginx -s reload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment