0

I have 2 nodejs applications running in my EC2 instance at PORT 3000 and 1337. What I want to achieve is

admin.mydomain.com

should be redirected to the application running on PORT 1337 and

mydomain.com www.mydomain.com

should be redirected to the application running on PORT 3000.

With my current nginx configuration I am getting a 502

  map $subdomain $subdomain_port {
  default    3000;
  www        3000;
  admin      1337;
}

server {
listen 80;
listen [::]:80;
server_name _;
return 301 https://$server_name$request_uri;
}

server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name ~^(?P<subdomain>.+?)\.historydiaries\.com$;

location / {
proxy_pass http://localhost:$subdomain_port;
proxy_redirect off;
}

ssl_certificate /etc/letsencrypt/live/historydiaries.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/historydiaries.com/privkey.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;

ssl_session_cache shared:SSL:5m;
ssl_session_timeout 1h;
add_header Strict-Transport-Security “max-age=15768000” always;
}
andrewJames
  • 19,570
  • 8
  • 19
  • 51
Sar AN
  • 31
  • 1
  • 3

2 Answers2

0

You can achieve this using two different nginx conf

0

I will go with separate Nginx vhost configuration.

One for www.mydomain.com and another one for admin.mydomain.com

server {
    listen 80;
    server_name www.mydomain.com;
    access_log /var/log/nginx/mydomain_access.log;

    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_pass http://localhost:3000/;
        proxy_redirect off;
    }
}

and

server {
    listen 80;
    server_name admin.mydomain.com;
    access_log /var/log/nginx/admin.mydomain_access.log;

    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_pass http://localhost:1337/;
        proxy_redirect off;
    }
}

This just simple vhost configuration. You can add Let's Encrypt later when you need.

Audy
  • 91
  • 1