question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

HTTP to HTTPS redirect mix-ups virtual hosts.

See original GitHub issue

Thanks 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:closed
  • Created 3 years ago
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
RytoEXcommented, Oct 4, 2020

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?

0reactions
MattIPv4commented, Oct 20, 2020

👍 Sweet, will close

Read more comments on GitHub >

github_iconTop Results From Across the Web

Redirect HTTP to HTTPS on default virtual host without ...
On my apache server I'd like to be able to redirect all incoming http requests to the equivalent https request ...
Read more >
Virtual host http-to-https redirects are going to the wrong ...
I have two sites hosted on one machine: foo.com and blank.test, I want each of them to redirect HTTP traffic to the corresponding...
Read more >
View Source - LYRASIS Wiki
Method 1 - redirecting with IP tables ... This is known to work on Red Hat Enterprise Linux 3 and other similar versions...
Read more >
Redirect HTTP to HTTPS in Apache - Linuxize
Typically when an SSL certificate is installed on a domain, you will have two virtual host directives for that domain. The first one...
Read more >
Amazon CloudFront with Drupal 8 | Drupal Sun
This allows virtual hosts, and any SSL redirect logic on the origin to function ... [meta] External caches mix up response formats on...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found