Nginx subdomain only work when port number is added

596 Views Asked by At

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

1

There are 1 best solutions below

0
On

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