Last active
May 4, 2018 07:33
-
-
Save shukriadams/1f4c417209783463ccc08a6cf9319b7d to your computer and use it in GitHub Desktop.
Hosting multiple nodejs apps on one server with nginx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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