Is it correct to define both listen
(80 and 443) under same server {}
like this ?
The other method would be to define two separate server{}
for each port which I know would be OK.
server {
listen 80 default_server;
server_name example.com;
root /usr/share/nginx/html;
location / {
}
listen example.com:443 ssl; # <<< !!!
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}
It is allowed to listen on multiple ports on the same
server
block, but it depends on your objective if you want to do it.Personally I use the following setup when I have the following set of domains and I want to use
https://www.example.com
as the main domain:server
block forexample.com
ports 80 and 443:This
server
block redirects all requests coming intohttp://example.com
orhttps://example.com
tohttps://www.example.com
.Then, there are two blocks total for
http://www.example.com
andhttps://www.example.com
:As far as I remember, yes it is. Also says so in the documentation (link below):
src: https://nginx.org/en/docs/http/configuring_https_servers.html