NGINX:如何在不同子域上设置多个虚拟主机(服务器块)?

发布于 2025-01-18 06:55:19 字数 2300 浏览 0 评论 0原文

编辑:


好吧,还是很奇怪。它似乎不适用于我的主浏览器。在隐身浏览器或全新的 chrome 窗口中,网站现在可以正常工作。我猜这与浏览器缓存有关?


因此,我在 Digital Ocean 上托管我的网站,并且我想在 1 个 Droplet/服务器上托管多个“网站”。我所说的多个网站是指我的主域的不同子域。所以我想在同一台服务器上托管 admin.domain.comdev.domain.comtest.domain.com。我遵循 这个教程,但它没有按预期工作。目前,这 3 个子域中只有 1 个在工作...

到目前为止我尝试过什么?

首先,我在 DNS 中创建了 3 个 A 记录,全部指向 Digital Ocean 上的同一个 server_ip Droplet。

我为 /var/www 文件夹中的每个子域创建了一个不同的文件夹,每个文件夹都包含一个 html 文件夹,其中包含一个简单的 index.html 文件和一些 html:

网站directiories

上图显示了我的 /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 个不同的子域“启用”服务器块。

这是我的启用站点的文件夹:

sitesenabled

我没有出现语法错误,因此使用以下命令重新启动了 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:

website directiories

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:

sites enabled

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静若繁花 2025-01-25 06:55:19

在/etc/nginx/nginx.conf中

http {

    # ...........................others contents.....................................

    # in bottom
    
    server {
        listen 80;
        root /var/www/html/cmp/api;
        server_name "cmpapi.localhost";
        index index.html index.php;
        location / {
            try_files $uri $uri/ $uri.html $uri.php =404;
        }
    }

    server {
        listen 80;
        root /var/www/html/cmp/frontend;
        server_name "cmp.localhost";
        index index.html index.php;
        location / {
            try_files $uri $uri/ $uri.html $uri.php =404;
        }
    }

}
  1. ,我的项目名称是CMP。这里2个项目
    1. 一个是反应前端
    2. 另一个是Laravel API项目
  2. 这里的2个文件夹
    1. /var/www/html/cmp/api
    2. /var/www/html/cmp/frontend

cmd

sudo find /var/www/html/cmp/frontend/ -type f -exec chmod 644 {} \;  
sudo find /var/www/html/cmp/frontend/ -type d -exec chmod 755 {} \;  

sudo find /var/www/html/cmp/api/ -type f -exec chmod 644 {} \;
sudo find /var/www/html/cmp/api/ -type d -exec chmod 755 {} \;  

systemctl reload nginx 

browser

IN /etc/nginx/nginx.conf

http {

    # ...........................others contents.....................................

    # in bottom
    
    server {
        listen 80;
        root /var/www/html/cmp/api;
        server_name "cmpapi.localhost";
        index index.html index.php;
        location / {
            try_files $uri $uri/ $uri.html $uri.php =404;
        }
    }

    server {
        listen 80;
        root /var/www/html/cmp/frontend;
        server_name "cmp.localhost";
        index index.html index.php;
        location / {
            try_files $uri $uri/ $uri.html $uri.php =404;
        }
    }

}
  1. Here my project name is cmp. here 2 project
    1. one is react frontend
    2. another is laravel api project
  2. Here 2 folder created in
    1. /var/www/html/cmp/api
    2. /var/www/html/cmp/frontend

cmd

sudo find /var/www/html/cmp/frontend/ -type f -exec chmod 644 {} \;  
sudo find /var/www/html/cmp/frontend/ -type d -exec chmod 755 {} \;  

sudo find /var/www/html/cmp/api/ -type f -exec chmod 644 {} \;
sudo find /var/www/html/cmp/api/ -type d -exec chmod 755 {} \;  

systemctl reload nginx 

Browser

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文