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:
- Why isn’t the second block working for the URL "dashboard-test-deb11/"
- 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
the
listen
parameter is not defined for the second blockThis should work assuming you have set your DNS records correctly.
you have set the
default_server
parameter.Nginx will forward any requests that do not match any other server blocks
server_name
parameter to thedefault_server
block regardless of theserver_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
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:
The third server block is working because it has a "server_name" directive set to "dashboard-test-deb11.company.com".