Nginx two virtual hosts on with domain name one in localhost

3.1k views Asked by At

On my Nginx I've got two hosts. One with the values server_name = www.mydomain.com; root /var/www/production/myFirstWebSite; and the other with server_name=localhost; root /var/www/development/mySecondWebSite;

To my domain registrar account I configured the DNS with two A record " www IN A myIP IN A myIP

This is cool, i can reach my first website with www.mydomain.com or mydomain.com.

Now the problem is how to reach my second website which is in development and I don't buy the domain name. And myIP/development/myScondWebSite is no more working ... I think that the problem come from the DNS entries but I'm not sure. Do you've got some ideas ?

Thanks in advance.

1

There are 1 answers

0
Mohammad AbuShady On BEST ANSWER

There's a couple of ways I could think of to access the localhost one.

  • Creating a subdomain instead of localhost
    This is the best one I'd recommend, try doing something like server_name localhost.mydomain.com.
    If you need to put further security, you could make it only allow a certain IP(s) or a range of IPs.

  • Play with your hosts file
    In this specific case I would not recommend this, because you're messing with localhost it self, might break some other stuff on your machine, if it was any other name I could have said it's fine.

  • Use an ssh tunnel to the server
    In this method you create a dynamic port on your ssh connection and set your browser to pass all traffic through tunnel which goes to the server then it's handled from there, so if you run localhost for example it would be like running localhost from over there, but since this involved a browser setting, you need to remember to disable it after you disconnect the ssh connection otherwise the browser would return an error saying that the proxy server is refusing the connection.

  • Using a local Nginx as a proxy
    This one I just came up with right now, and I can't say If it would work or not, the 3 before I've worked with before and I know they work.
    You'd set a certain domain name that your local nginx would capture and then proxy it to the remote server, but edit the host header setting it to localhost instead, that way it would match the localhost in the remote machine, if this one works it would not need any setting to be turned on and off every time.

Out of all these, I'd recommend the first one first (if it's an option), then try the last one if you don't want to keep turning things on and off before and after each setting.