问题原文
原帖链接 · myol · CC BY-SA 4.0
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?
已采纳解答
原帖链接 · Maximillian Laumeister · CC BY-SA 4.0
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;
}
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
