Skip to content

Instantly share code, notes, and snippets.

@Denver-sn
Forked from rgwozdz/nginx-subdomains.md
Created November 25, 2023 00:29
Show Gist options
  • Save Denver-sn/8838dea8cd898fab3b79a8324aa89fb2 to your computer and use it in GitHub Desktop.
Save Denver-sn/8838dea8cd898fab3b79a8324aa89fb2 to your computer and use it in GitHub Desktop.
2 node apps on one Ubuntu EC2, access via two different subdomains

create this file:

/etc/nginx/sites-available/crp-pmte-stage.spatialdevmo.com

This file will handle visits to crp-pmte-stage.spatialdevmo.com. A Node.js app running on port 5000 needs to be served. Give the file this content:

upstream crp-pmte-stage {
    server 127.0.0.1:5000;
}

server {
    listen 0.0.0.0:80;
    server_name crp-pmte-stage.spatialdevmo.com;
    access_log /var/log/nginx/crp-pmte-stage.spatialdevmo.log;

    # Gzip Compression
    gzip on;
    gzip_comp_level 6;
    gzip_vary on;
    gzip_min_length  1000;
    gzip_proxied any;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript
    gzip_buffers 16 8k;

    # Proxy to the Node instance
    location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy true;
      proxy_pass http://localhost:5000;
      proxy_redirect off;
    }
}

Create a symbolic link to the above file in the site-enabled directory:

 sudo ln -s /etc/nginx/sites-available/crp-pmte-stage.spatialdevmo.com /etc/nginx/sites-enabled/crp-pmte-stage.spatialdevmo.com

Now, create this file:

/etc/nginx/sites-available/tanaim-pmte-stage.spatialdevmo.com

This file will handle visits to tanaim-pmte-stage.spatialdevmo.com. A Node.js app running on port 3000 needs to be served. Give the file this content:

upstream tanaim-pmte-stage{
    server 127.0.0.1:3000;
}

server {
    listen 0.0.0.0:80;
    server_name tanaim-pmte-stage.spatialdevmo.com;
    access_log /var/log/nginx/tanaim-pmte-stage.spatialdevmo.log;

    # Gzip Compression
    gzip on;
    gzip_comp_level 6;
    gzip_vary on;
    gzip_min_length  1000;
    gzip_proxied any;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript
    gzip_buffers 16 8k;

    # Proxy to the Node instance
    location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy true;
      proxy_pass http://localhost:3000;
      proxy_redirect off;
    }
 }

Create a symbolic link to the above file in the site-enabled directory:

 sudo ln -s /etc/nginx/sites-available/tanaim-pmte-stage.spatialdevmo.com /etc/nginx/sites-enabled/tanaim-pmte-stage.spatialdevmo.com

Finally, reload NGINX (if it is already running):

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