How To Fix Nginx Redirect To Wrong Website Error When There Are Multiple WebSites

I create two websites www.test-1.com and www.iphone-how-to.com in one Nginx server to demo this example. www.iphone-how-to.com is a real domain with https enabled. And www.test-1.com is a fake domain without enable https, then I add domain to ip mapping record in my local machine’s hosts file to make it accessible.

Below are the two websites information. The website management panel is BT panel.

create 2 websites in nginx and enable https for one website

Below is local hosts file domain to ip mapping data.

126.118.109.188  www.test-1.com

1. Nginx Redirect To Wrong Website Error.

Now we can produce nginx redirect to wrong website error follow below steps.

  1. Open a web browser such as Firefox.
  2. Input http://www.test-1.com/ in the web browser, then you can get www.test-1.com website’s index.html page as expected.
    http www.test-1.com website index page
  3. We can also run curl command in command line to get below result.
    $ curl http://www.test-1.com
    <!doctype html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>www.test-1.com</title>
        <style>
            .container {
                width: 60%;
                margin: 10% auto 0;
                background-color: #f0f0f0;
                padding: 2% 5%;
                border-radius: 10px
            }
    
            ul {
                padding-left: 20px;
            }
    
                ul li {
                    line-height: 2.3
                }
    
            a {
                color: #20a53a
            }
        </style>
    </head>
    <body>
        <div class="container">
            <h1>www.test-1.com</h1>
        </div>
    </body>
    </html>
  4. Input http://www.iphone-how-to.com in web browser, and then you can find it shows https://www.iphone-how-to.com website’s index page content, it is same with http://www.iphone-how-to.com.
    https iphone-how-to.com website index page content
  5. Run curl command in terminal to get http://www.iphone-how-to.com index page as below, we can see the http request has been moved to https request by nginx server permanently.
    $ curl http://www.iphone-how-to.com
    <html>
    <head><title>301 Moved Permanently</title></head>
    <body>
    <center><h1>301 Moved Permanently</h1></center>
    <hr><center>nginx</center>
    </body>
    </html>
  6. Now input https://www.test-1.com/ in the web browser, and you can find it shows https://www.iphone-how-to.com website index page content in the web browser. But the domain address in web browser url address text box is not changed.
    https www.test-1.com shows https iphone-how-to.com index page
  7. This is because www.test-1.com domain do not enable https, so nginx will pick up a website which enabled https by domain name alphabetic order and return it’s content.
  8. In this example, only iphone-how-to.com has enabled https, so when you request www.test-1.com with https protocol, it display iphone-how-to.com website content.
  9. We can also find this error in iphone-how-to.com website log file. When we add $host ( to print out request domain ) in iphone-how-to.com log data, we can see all request to https://www.test-1.com is routed to iphone-how-to.com in the log file.
  10. To fix this error, you should enable https for domain www.test-1.com in nginx, then nginx can find the domain website when client request the domain use https protocol.

References

  1. How To Install BT VPS Control Panel And Restore WordPress Website Into It
  2. How To Edit Hosts File In Mac OS And Linux, How To Modify Hosts File In Mac
  3. How To Edit Hosts File In Windows 10

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.