Score:0

Why my nginx conf file redirects subdomains to main domain?

cn flag

I have removed all files from /etc/nginx/sites-enabled/ directory.

I have also removed all files from /etc/nginx/sites-available/ directory.

I only have one file in /etc/nginx/conf.d/ called my-domain-name.com.conf and it contains:

server {
    listen 80;
    server_name my-domain-name.com www.my-domain-name.com;
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl;
    server_name my-domain-name.com www.my-domain-name.com;

    ssl_certificate /etc/letsencrypt/live/my-domain-name.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/my-domain-name.com/privkey.pem;

    include /etc/letsencrypt/options-ssl-nginx.conf;

    location / {
        proxy_pass http://localhost:4000;
    }
}

I proxy traffic to a docker container that listens on port 4000.

It works great for my-domain-name.com and www.my-domain-name.com.

However, it also redirects ALL of my subdomains that have been defined in DNS.

This is not the intended behavior. I only want this file to serve these two domains, not more.

What is wrong here?

Update:

I added another file, called sudomain.my-domain-name.com.conf and I added this configuration inside it:

server {
    listen 80;
    server_name subdomain.my-domain-name.com;
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl;
    server_name subdomain.my-domain-name.com;

    ssl_certificate /etc/letsencrypt/live/my-domain-name.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/my-domain-name.com/privkey.pem;

    include /etc/letsencrypt/options-ssl-nginx.conf;

    location / {
        proxy_pass http://localhost:3131;
    }
}

And nginx -t shows success and nginx -s reload is also applied. But again when I go to subdomain.my-domain-name.com, instead of getting my second docker that is running on port 3131, again I'm redirected to my-domain-name.com page.

Score:1
pl flag

Please show nginx configuration of your subdomains.

EDIT 1:

Try adding this config file (name it as you wish):

server {
    listen 80;
    server_name my-subdomain-name.my-domain-name.com www.my-subdomain-name.my-domain-name.com;
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl;
    
    server_name my-subdomain-name.my-domain-name.com www.my-subdomain-name.my-domain-name.com;

    ssl_certificate /etc/letsencrypt/live/my-subdomain-name.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/my-subdomain-name.com/privkey.pem;

    include /etc/letsencrypt/options-ssl-nginx.conf;

    location / {
#      You can change port, url if you wish
        proxy_pass http://localhost:4000;
    }
}

EDIT 2:

For example you have this subdomain: test.my-domain.com Then you have to set up your server to serve files from that subdomain. For that, we've to create a nginx config file.

Saeed Neamati avatar
cn flag
There is no config file for subdomains. They are only defined in my DNS, which is CloudFlare.
Saeed Neamati avatar
cn flag
That config file is my only config file in `/etc/nginx/conf.d/` directory.
pl flag
You must have to create a config file for each domain and subdomain.
Saeed Neamati avatar
cn flag
Dear @ArRakin, thanks for answering. But I fail to understand how this helps me.
Saeed Neamati avatar
cn flag
With all respect, I think you have misunderstood my problem.
pl flag
I'm editing this post now.
Saeed Neamati avatar
cn flag
Let's say I only have my subdomains in my DNS and I don't care about serving anything from them. Why does Nginx have this behavior with this one single file? Why it redirects **ALL** traffic to my main domain, while it's explicitly being told to only serve my main domain?
Michael Hampton avatar
cz flag
@SaeedNeamati You have sent the traffic to nginx, so it must do something with it. In the case where you did not specify a server block to use, it will use the default.
Saeed Neamati avatar
cn flag
that logic is absurd. can you show me where is the **default**? my config is explicit and it has no defaults in it.
Saeed Neamati avatar
cn flag
Please see the update.
djdomi avatar
za flag
show us the complete output of `nginx -T`
Score:0
sz flag

However, it also redirects ALL of my subdomains that have been defined in DNS. [...] This is not the intended behavior. I only want this file to serve these two domains, not more.

If a subdomain resolves to the same IP address as the server hosting the two domains that you actually want to serve, Nginx is going to be forced to deal with those incoming requests.

Why it redirects ALL traffic to my main domain, while it's explicitly being told to only serve my main domain?

This is the default behavior. Nginx does try to route the requests to the appropriate server by using the Host HTTP header, but if there's no match, it routes the traffic to the default server. This seems to be the source of the confusion.

If the Host header field does not match a server name, NGINX Plus routes the request to the default server for the port on which the request arrived. The default server is the first one listed in the nginx.conf file, unless you include the default_server parameter to the listen directive to explicitly designate a server as the default1.

To actually prevent the server from serving your domain's content unless specifically requested, you could add an actual default server block. You could even return 204 No Content for all requests if you wanted to, although something even slightly more nuanced is probably always preferable.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.