0

I created a website (Nginx) and everything work well except when I try to browse the subdomain, it seems like even a subdomain prefix is threated like it was "www" because it always lead to the main domain except when I add the port number.

ex : example.com => take me to the homepage. All good

sub.example.com => Take me to the homepage. Not good

sub.example.com:3000 => take me the the subdomain... Good but not practical, I would like to achieve the same result without the ":3000"

server {
    listen 80;
    listen [::]:80;
    server_name *.example.com;
    return 301 https://$host$request_uri;
    location / {
        proxy_pass http://localhost:3000/;
    }
}
server{
    listen 443 ssl;
    server_name *.sexample.com example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    location / {
        proxy_pass http://localhost:3000/;
        try_files $uri $uri/ =404;
    }
}

I forgot to mention I am using wildcard-subdomains to handle the subdomains request

  • So `example.com` and `sub.example.com` both point to `http://localhost:3000`. How does `localhost:3000` know that the request was made through the subdomain? Maybe you need to add `proxy_set_header Host $host;` – Richard Smith Dec 12 '19 at 09:51
  • I use wildcard-subdomains, before i erased the server, it was working fine. The subdomains are dynamically created, I mean whatever the prefix you enter, the router can deal with it and display the correct output – Joaquim Antonio Kapel Dec 12 '19 at 09:57

1 Answers1

0

I just found my old file setting which was somewhat hidden in my computer and I added this :

proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade; 

in the "location /" block and now it is working like a charm, so if anyone of you . understand what that mean, feel free to elaborate