大网数据广告活动
网站运营与故障问答

VPS with single IP and nginx reverse proxy

· 52CE 我爱测

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 addre…

问题原文

原帖链接 · 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;
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2018-10-06)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。