skip to Main Content

I have the following nginx configuration file

server {
        listen 80 default_server;

        server_name _;
        return 301 https://dashboard-test-deb11.company.com$request_uri;
}

server {
        server_name dashboard-test-deb11;
        return 301 https://dashboard-test-deb11.company.com$request_uri;
}

server {
        listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;

        server_name dashboard-test-deb11.company.com;

        ssl_certificate    /etc/nginx/certificates/certnew.crt;
        ssl_certificate_key    /etc/nginx/certificates/server.key;

        location / {
                proxy_pass http://localhost:3000;
                proxy_http_version 1.1;
                proxy_set_header   X-Forwarded-For $remote_addr;
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
        }
}

When the user enters the URL "dashboard-test-deb11/" I want to redirect them to "https://dashboard-test-deb11.company.com" but for some reason it just serves localhost:3000, as mentioned in block 3.

I have two doubts here:

  1. Why isn’t the second block working for the URL "dashboard-test-deb11/"
  2. Why is the third block working? I have mentioned "server_name dashboard-test-deb11.company.com" in that block, and still, it’s working for "dashboard-test-deb11/"

2

Answers


  1. Why isn’t the second block working for the URL "dashboard-test-deb11/"

    the listen parameter is not defined for the second block

    server {
            listen 443;
            server_name dashboard-test-deb11;
            return 301 https://dashboard-test-deb11.company.com$request_uri;
    }
    

    This should work assuming you have set your DNS records correctly.

    Why is the third block working? I have mentioned "server_name
    dashboard-test-deb11.company.com" in that block, and still, it’s
    working for "dashboard-test-deb11/"

    you have set the default_server parameter.
    Nginx will forward any requests that do not match any other server blocks server_name parameter to the default_server block regardless of the server_name defined in that block.

    Since your second server block was not listening on port 443 any https request made was being sent to third block

    you can read more about how requests are processed in Nginx here

    Login or Signup to reply.
  2. In order to make the second block work as expected, you should include a "location" block that defines how to handle requests for this server_name. For example:

    server {
        server_name dashboard-test-deb11;
        listen 80;
    
        location / {
            return 301 https://dashboard-test-deb11.company.com$request_uri;
        }
    }

    The third server block is working because it has a "server_name" directive set to "dashboard-test-deb11.company.com".

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search