在 Nginx 上为通配符域添加服务器块

Adding Server Block on Nginx for WildCard domains

提问人:sanket gawde 提问时间:11/6/2023 最后编辑:sanket gawde 更新时间:11/7/2023 访问量:73

问:

我有 2 个域,分别是 D1“abc.com”和 D2“def.com”。我已将 D1 配置为 Nginx 上的以下服务器块。

server {
    listen 80;
    server_name *.abc.com,abc.com;
    root /var/www/abc/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";
 
    index index.php;
 
    charset utf-8;
 
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
 
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }
 
    error_page 404 /index.php;
 
    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }
 
    location ~ /\.(?!well-known).* {
        deny all;
    }
}

它与 D1 完美配合。
但是,当配置与以下服务器块相同的 D2 域时

server {
    listen 80;
    server_name *.def.com,def.com;
    root /var/www/def/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";
 
    index index.php;
 
    charset utf-8;
 
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
 
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }
 
    error_page 404 /index.php;
 
    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }
 
    location ~ /\.(?!well-known).* {
        deny all;
    }
}

配置后运行命令如下:

sudo ln -s /etc/nginx/sites-available/abc.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/def.com /etc/nginx/sites-enabled/

sudo nginx -t //gives me ok as output

sudo systemctl reload nginx
sudo systemctl restart nginx

它以“abc.com”身份重定向到 D1,
我正在使用 Laravel8、php8.1 和 Nginx

是否需要在 2 台不同的服务器上托管应用程序?如何实现这一点?

nginx laravel-8 ubuntu-20.04 php-8.1

评论

0赞 Richard Smith 11/6/2023
server_name采用空格分隔的列表,而不是逗号分隔的列表。查看文档
0赞 sanket gawde 11/7/2023
@RichardSmith它是正确的,但为什么它为“abc.com”工作
0赞 Richard Smith 11/7/2023
用于“abc.com”的块也是默认服务器server

答:

0赞 Praveen Chauhan 11/7/2023 #1

您需要在server_name下的每个域名之间留有间距,而不是逗号。

它适用于 abc.com 的原因可能是它正在回退到默认/第一个配置。

评论

0赞 sanket gawde 11/24/2023
请查看@Richard史密斯的问题评论。这也是有效的答案。
0赞 Sunny 11/7/2023 #2

我同意其他人的观点,因为第一个服务器块被视为默认服务器。

由于其他域无法映射(两个名称之间缺少空格),因此请求从第一个块开始处理。

任何一个都应该始终具有虚拟默认服务器“以捕获这些场景”

server_name _ default_server;

我个人最喜欢的通配符服务器块

# cat /etc/nginx/conf.d/wildcard.conf 

server {
    listen 80;
    server_name ~^((?<subdomain>.*)\.)?(?<domain>.*)\.com$;
    root /var/www/$domain/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";
 
    index index.php;
 
    charset utf-8;
 
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
 
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }
 
    error_page 404 /index.php;
 
    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }
 
    location ~ /\.(?!well-known).* {
        deny all;
    }
}

由于两台服务器在其他配置中没有变化,这将有助于维护和调试。

下面的小演示: 注意:403 状态码只是因为指定根目录下没有索引文档,否则这是工作配置。

asciicast