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 enabling HTTPS, then I add the domain to IP mapping record in my local machine’s host file to make it accessible. The website management panel is the BT panel. Below is the local host’s 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 the 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.
  3. We can also run curl command in the command line to get the 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 a web browser, and then you can find it shows https://www.iphone-how-to.com web site’s index page content, it is same with http://www.iphone-how-to.com.
  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 browse URL 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 the web browser URL address text box is not changed ( https://www.test-1.com/ ).
  7. This is because the www.test-1.com domain does not enable HTTPS, so Nginx will pick up a website that 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 displays iphone-how-to.com website content.
  9. We can also find this error in the 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 the 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.