0

I am pretty new to nginx, I managed to get web server running and I can access my Node.js app (port 5000) on my domain. However I would like to add a subdomain to access static website. At the moment with my config, if I go to my subdomain I see my node.js application.

My nginx config files inside /etc/nginx/sites-available are the following:

  • default
  • subdomain.example.com.conf

Default config:

server {
    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;
    server_name example.com www.example.com; # managed by Certbot

    listen [::]:443 ssl ipv6only=on; # managed by Certbot                                    
    listen 443 ssl; # managed by Certbot                                                     
    ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem; # managed by Certbot  
    ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot                    
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


    location / {
       proxy_pass http://localhost:5000; #whatever port your app runs on
       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;
    }
}

Subdomain config:

server {

        listen 443 ssl;
        listen [::]:443 ssl;
        root sites/example;
        index test.example.com.html;
        server_name test.example.com www.test.example.com;
        ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
        include /etc/letsencrypt/options-ssl-nginx.conf;                   
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

        location / {
                try_files $uri $uri/ =404;
        }
}

How could I see different content other than my Node.js app from my subdomain? Thank you in advance.

Bix
  • 23
  • 2
  • Use `nginx -T` (uppercase `T`) to view the entire configuration across all included files and ensure that the `server` blocks appear with the correct `server_name`. – Richard Smith Oct 23 '20 at 13:21
  • Don't you have any `server` blocks listening on port 80? How do you try to access your domain/subdomain, via HTTP or via HTTPS? – Ivan Shatsky Oct 23 '20 at 13:21
  • @RichardSmith Every server_name is different and they're correct. @IvanShatsky At the very bottom of default config file there is `server` block which redirects non-www and www to https (managed by Certbot). And it does use port 80. – Bix Oct 23 '20 at 14:18
  • You can't refer two users in one comment, the second one (i.e. me) won't receive incoming message notification, use two separate comments answering two users. If you have only one `server` block listening on port 80 redirecting HTTP requests to your Node app, it would be default server block for any plain HTTP request redirecting it to your Node app no matter what `Host` HTTP header value is, `example.com` or `test.example.com`. Read [this](http://nginx.org/en/docs/http/request_processing.html) document for details. – Ivan Shatsky Oct 23 '20 at 19:32
  • @IvanShatsky I tested some more and if I only used the "default" conf file then I got it working. However my goal is to use multiple configs (it'll be better also if I have more domains to use). Also can I only listen to 443 and still have the site working correctly, just on HTTPS only? Or the right approach is to listen to both ports and then redirecting to HTTPS if its port 80? – Bix Oct 23 '20 at 22:32
  • Yes, you can listen on 443 port only and your site would work correctly over HTTPS. All requests for this site on port 80 would be served by some other `server` block which will be chosen according to algorithm described [here](http://nginx.org/en/docs/http/request_processing.html). There is no such thing as *right* approach, you should choose an approach according to your needs, but the common approach is to listen on port 80 and redirect requests to port 443 for every of your domains. – Ivan Shatsky Oct 23 '20 at 22:56
  • [Here](https://stackoverflow.com/questions/60362642/nginx-doesnt-listen-on-port-80-twice/60362700#60362700) is my approach to serve several domains on the same server. I don't use a certbot, I think configurations produced by certbot is a scrap, and I'm [not alone](https://serverfault.com/questions/896543/nginx-certbot-certificate-www-and-non-www/896555#896555) on it (although my configuration somehow differ from Michael Hampton's one). – Ivan Shatsky Oct 23 '20 at 23:00
  • I see you are using the same certificate for both of your `server` blocks. Does this certificate SAN (Subject Alternative Name) list really include all of your domain names (`example.com`, `www.example.com`, `test.example.com`, `www.test.example.com`)? Or you are using two different certificates and this is just a fake filename/path? – Ivan Shatsky Oct 23 '20 at 23:03
  • @IvanShatsky Thanks for your reply. For unknown reason I have 2 certificates at the moment. But yes the certificate does include all domain names. I've got it working on separate config files now, also found out that on subdomain config `root` value was inside root folder, but there was no access for it, changed it to `/var/www/[FOLDER]` and it's working now. – Bix Oct 24 '20 at 10:34
  • @IvanShatsky Its out of topic of this post, but if I go to my subdomain. Sometimes I see the page and sometimes I see nginx "Not found" page. I am pretty sure the root folder the config points to is right and the "index" parameter is right file name. – Bix Oct 24 '20 at 18:23
  • The first thing, check the error log. Could be a couple of reasons. And it is definitely out of topic for this question. – Ivan Shatsky Oct 25 '20 at 00:40

0 Answers0