I see that this configuration made by Certbot is indeed redirecting 301 or making as not found !? 404
But the order and the arrangement seem odd.
Here a longer extract of my config file.
server {
server_name sub.example.com
location / {
proxy_pass http://1.2.3.4/;
}
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/xxxxxx;
ssl_certificate_key /etc/letsencrypt/live/xxxxxx;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}
server {
if ($host = sub.example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name sub.example.com;
return 404; # managed by Certbot
}
So, is that normal that I have twice server {} ?
If each server{} section are to split 80 and 443 port.
Why in the second server{} (80) the if statement is before specifying the port 80 ?? seem so odd.
Yes, one server block is for port 80 (HTTP) requests and the other for port 443 (HTTPS)
The placement of the listen is not important within a server block (nor the server_name)
The return will occur for requests to that domain name but otherwise returns a 404. If this server block is your nginx default then it's possible non-standard requests will reach this server block and so will get a 404 (spammers and such).