I have an nginx server running. I want it to redirect http://www.example.com
to https://www.example.com
, but not touch any other subdomains like http://foo.example.com
.
For some reason, no matter what I add in the subdomain, it still gets rerouted. My webpage shows on www.example.com (as it should), but also on foo.example.com and example.com (as it shouldn't)
This is my example.com config file:
server { listen 80; server_name www.example.com; # For debug add_header X-debug-message "listen:80, server_name:www.example.com, redirect:https://$host$request_uri" always; # Riderect return 301 https://$host$request_uri;}server { listen 443 ssl; server_name www.example.com; # For debug add_header X-debug-message "listen:443, server_name:www.example.com, redirected:https://$host$request_uri" always; # 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; root /var/www/example.com; # Redirect location / { proxy_pass http://192.168.1.224:80; }}
Going to www.example.com
shows me my webpage as expected. But going to foo.example.com
also gives me my webpage - which it shouldn't. example.com
also gives me the webpage.
Opening www.example.com
in my browser, I see the following http header (as expected):
X-debug-message: DEBUG: listen:443, server_name:www.example.com, redirected:https://www.example.com/
Opening foo.example.com
in my browser, I see the following http header (not as expected):
X-debug-message: DEBUG: listen:443, server_name:www.example.com, redirected:https://foo.example.com/
How can I make my nginx only redirect www.example.com
?