1

I have a VPS with a single IP assigned to it. I am going to set up an nginx reverse proxy to hopefully serve a number of websites from the same server.

As far as I understand, I can have various domains point to the same IP address - that of the server - and the nginx reverse proxy will use the domain names passed to it(?) to then serve the correct website sitting behind the proxy.

Is this correct?

Do I change the nameservers of each domain to point to the servers DNS Servers or vice versa? Or how do I have different domains pointing to the same IP?

  • 1
    I just wrote up an answer, but are you sure you are looking for a reverse proxy setup? A reverse proxy is needed only if you are running your website under a different web server than nginx, and you want nginx to "go between" your real web server and the outside world. If your sites are running directly on nginx itself, no reverse proxy is needed. – Maximillian Laumeister Oct 6 '18 at 23:31
  • Hi myol, do you feel as if I answered your question completely? If so, please don’t forget to mark my answer as "accepted" by clicking the gray checkmark to the left of the answer. If your question hasn't been fully answered, please elaborate on what else you need to know so the community can provide you with further help! Thanks! – Maximillian Laumeister Oct 8 '18 at 23:35
  • 1
    Yes, thank you, your explanation has clarified things. I delayed accepting your answer because I have been having an issue that I thought may have been related – myol Oct 9 '18 at 3:11
2

You do not need to change your nameservers at all - that's only relevant when you are changing DNS providers.

For each website, add an A record from the root domain to the IP address of your server, and an A record from the www subdomain to the IP address of your server.

When someone makes a request to one of your domains, the request will arrive to nginx with a HTTP Host header that indicates which domain they want to communicate with. In the nginx config file, you will need to set up virtual servers to proxy the request depending on the Host header, like so:

server {
    listen 80;
    server_name example1.com;
    location / {
        proxy_pass http://127.0.0.1:8080; # The port that web service 1 is running on
    }
}

server {
    listen 80;
    server_name example2.com;
    location / {
        proxy_pass http://127.0.0.1:8181; # The port that web service 2 is running on
    }
}

You will also want to set up redirects from your bare domain to www, or from www to your bare domain, like this:

server {
    listen 80;
    server_name example1.com;
    return 301 http://www.example1.com$request_uri;
}

server {
    listen 80;
    server_name example2.com;
    return 301 http://www.example2.com$request_uri;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.