NGINX:如何在不同子域上设置多个虚拟主机(服务器块)?
编辑:
好吧,还是很奇怪。它似乎不适用于我的主浏览器。在隐身浏览器或全新的 chrome 窗口中,网站现在可以正常工作。我猜这与浏览器缓存有关?
因此,我在 Digital Ocean 上托管我的网站,并且我想在 1 个 Droplet/服务器上托管多个“网站”。我所说的多个网站是指我的主域的不同子域。所以我想在同一台服务器上托管 admin.domain.com
、dev.domain.com
和 test.domain.com
。我遵循 这个教程,但它没有按预期工作。目前,这 3 个子域中只有 1 个在工作...
到目前为止我尝试过什么?
首先,我在 DNS 中创建了 3 个 A 记录,全部指向 Digital Ocean 上的同一个 server_ip Droplet。
我为 /var/www
文件夹中的每个子域创建了一个不同的文件夹,每个文件夹都包含一个 html
文件夹,其中包含一个简单的 index.html 文件和一些 html:
上图显示了我的 /var/www
文件夹。
然后我使用以下命令 sudo chmod -R 755 /var/www 。
接下来,我复制了默认服务器块文件,并使用以下命令将其用作新服务器块的默认值:
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/admin .domain.nl
我将 3 个子域的所有 3 个配置文件中的文件内容更改为以下内容(显然将 root
更改为特定子域也server_name
):
server {
listen 80;
listen [::]:80;
root /var/www/admin.domain.nl/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name admin.domain.nl;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
}
然后我使用了以下命令:sudo ln -s /etc/nginx/sites-available/dev.domain.nl /etc/nginx/sites-enabled/ 3 次,为 3 个不同的子域“启用”服务器块。
这是我的启用站点的文件夹:
我没有出现语法错误,因此使用以下命令重新启动了 nginx:sudo systemctl restart nginx
。
问题
现在,由于一些我不明白的非常奇怪的原因,只有 admin.domain.nl
网站正在工作。其他 2 个子域仅显示:无法访问此站点
。
我在这里缺少什么?
EDIT:
Ok, very strange still. It seems that it does not work on my main browser. In incognito browsers or just a completely new chrome window the sites now do work. I guess it has something to do with browser caching?
So I am hosting my website on Digital Ocean and I want to host multiple 'websites' on 1 droplet/server. By multiple websites, I mean different subdomains of my main domain. So I want to host admin.domain.com
, dev.domain.com
and test.domain.com
on the same server. I followed this tutorial, but it is not working like expected. Currently, only 1 subdomain of the 3 is working...
What have I tried so far?
First of, I created 3 A records in my DNS all pointing to the same server_ip droplet on Digital Ocean.
I've created a different a different folder for each subdomain in the /var/www
folder, each containing a html
folder with a simple index.html
file and some html:
The image above shows my /var/www
folder.
I then used the following command sudo chmod -R 755 /var/www
.
Next, I copied the default server block file and used this as the default for a new server block with the following command:
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/admin.domain.nl
I changed the contents of the file in all 3 config files for the 3 subdomains to the following (obviously changing the root
to the specific subdomain aswell as the server_name
):
server {
listen 80;
listen [::]:80;
root /var/www/admin.domain.nl/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name admin.domain.nl;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
}
I then used the following command: sudo ln -s /etc/nginx/sites-available/dev.domain.nl /etc/nginx/sites-enabled/
3 times for the 3 different subdomains to 'enable' the server blocks.
This is my sites-enabled folder:
I've had no syntax errors and thus restarted nginx with: sudo systemctl restart nginx
.
The problem
Now, for some very odd reason I do not understand, only the admin.domain.nl
site is working. The other 2 subdomains simply display: This site can’t be reached
.
What am I missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在/etc/nginx/nginx.conf中
cmd
browser
IN /etc/nginx/nginx.conf
cmd
Browser