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

h79rfbju  于 2023-11-17  发布在  Nginx
关注(0)|答案(2)|浏览(146)

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

  1. server {
  2. listen 80;
  3. server_name *.abc.com,abc.com;
  4. root /var/www/abc/public;
  5. add_header X-Frame-Options "SAMEORIGIN";
  6. add_header X-Content-Type-Options "nosniff";
  7. index index.php;
  8. charset utf-8;
  9. location / {
  10. try_files $uri $uri/ /index.php?$query_string;
  11. }
  12. location = /favicon.ico { access_log off; log_not_found off; }
  13. location = /robots.txt { access_log off; log_not_found off; }
  14. error_page 404 /index.php;
  15. location ~ \.php$ {
  16. fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
  17. fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
  18. include fastcgi_params;
  19. }
  20. location ~ /\.(?!well-known).* {
  21. deny all;
  22. }
  23. }

字符串
它与D1配合得非常好。
但当配置与以下服务器块相同的D2域时

  1. server {
  2. listen 80;
  3. server_name *.def.com,def.com;
  4. root /var/www/def/public;
  5. add_header X-Frame-Options "SAMEORIGIN";
  6. add_header X-Content-Type-Options "nosniff";
  7. index index.php;
  8. charset utf-8;
  9. location / {
  10. try_files $uri $uri/ /index.php?$query_string;
  11. }
  12. location = /favicon.ico { access_log off; log_not_found off; }
  13. location = /robots.txt { access_log off; log_not_found off; }
  14. error_page 404 /index.php;
  15. location ~ \.php$ {
  16. fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
  17. fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
  18. include fastcgi_params;
  19. }
  20. location ~ /\.(?!well-known).* {
  21. deny all;
  22. }
  23. }


配置后,按如下方式运行命令:

  1. sudo ln -s /etc/nginx/sites-available/abc.com /etc/nginx/sites-enabled/
  2. sudo ln -s /etc/nginx/sites-available/def.com /etc/nginx/sites-enabled/
  3. sudo nginx -t //gives me ok as output
  4. sudo systemctl reload nginx
  5. sudo systemctl restart nginx


它将重定向到D1,名称为“abc.com“
我使用的是Laravel8,php8.1和Nginx
是否需要在2个不同的服务器上托管应用程序?如何实现?

s71maibg

s71maibg1#

  • 这是一个多余的答案,但我没有足够的声誉来评论,所以通过一个答案发布。

我认为问题是理查德已经在评论中提到的。你需要在server_name下的每个域名之间留出间距,而不是逗号。
为什么它会为abc.com工作的原因可能是它回落到默认/第一配置。

tct7dpnv

tct7dpnv2#

我同意其他人的看法,这是因为第一个服务器块被视为默认服务器。
由于其他域无法Map(两个名称之间缺少空格),因此从第一个块提供请求。
任何一个都应该总是有虚拟的默认服务器“来捕捉这些场景”

  1. server_name _ default_server;

字符串

我个人最喜欢的服务器块

  1. # cat /etc/nginx/conf.d/wildcard.conf
  2. server {
  3. listen 80;
  4. server_name ~^((?<subdomain>.*)\.)?(?<domain>.*)\.com$;
  5. root /var/www/$domain/public;
  6. add_header X-Frame-Options "SAMEORIGIN";
  7. add_header X-Content-Type-Options "nosniff";
  8. index index.php;
  9. charset utf-8;
  10. location / {
  11. try_files $uri $uri/ /index.php?$query_string;
  12. }
  13. location = /favicon.ico { access_log off; log_not_found off; }
  14. location = /robots.txt { access_log off; log_not_found off; }
  15. error_page 404 /index.php;
  16. location ~ \.php$ {
  17. fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
  18. fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
  19. include fastcgi_params;
  20. }
  21. location ~ /\.(?!well-known).* {
  22. deny all;
  23. }
  24. }


由于这两个服务器在其他配置上没有变化,这将有助于维护和调试。
下面的小演示:注意:403状态码只是因为在指定的根目录下没有索引文档,否则这是工作配置。


的数据

展开查看全部

相关问题