HTTP to HTTPS redirect mix-ups virtual hosts.
See original GitHub issueThanks for this amazing tool!
The issue is as following: If there are two virtual hosts (Server blocks) serving two host names or subdomains:
sd1.domain.com
sd2.domain.com
When the configuration is generated using the nginxconfig and if HTTP to HTTPS redirect is enabled following server block gets generated for each host:
--- For virtual host 1
# HTTP redirect
server {
listen 80;
listen [::]:80;
return 301 https://sd1.domain.com$request_uri;
}
-- For virtual host 2
# HTTP redirect
server {
listen 80;
listen [::]:80;
return 301 https://sd2.domain.com$request_uri;
}
Now when both sites are enabled, and if any request is received over HTTP, the server block which was included in main config file first will be considered, which may result in redirect to a different virtual host over HTTPS. This issue can be easily reproduced.
For example: If request is for http://sd2.domain.com
but first redirect server block read by the nginx from config is for sd1.domain.com, then request will be redirected to https://sd1.domain.com
as server block does not contain any server_name to distinguish incoming request.
There are two ways in which this issue can be solved: Either include only one redirect block which redirects to appropriate host:
server {
listen 80 default_server;
server_name _;
return 301 https://$host$request_uri;
}
Or include independent redirect block with server_name:
server {
listen 80;
server_name sd1.domain.com;
return 301 https://sd1.domain.com$request_uri;
}
Attribution: The solution was referred from https://serversforhackers.com/c/redirect-http-to-https-nginx
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (8 by maintainers)
Top GitHub Comments
When I attempted to reproduce this, the server blocks ended up with the seemingly appropriate
server_name
items. This seems like it is a duplicate of #160, though I’m not sure how since that was closed in July. Perhaps I misunderstood something?👍 Sweet, will close