nginx配置server的时候server_name不起作用?

【字号: 日期:2022-07-20浏览:42作者:雯心

问题描述

nginx配置server的时候 server_name为什么不起作用

server { listen 8000; server_name kaixuan.test.com; root /data1/htdocs/kaixuan.test.com/; location ~ .php$ {fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;includefastcgi_params; } location / { index index.html; }}server { listen 80; server_name kaixuan.hehe.com; root /data1/htdocs/kaixuan.hehe.com/; location ~ .php$ {fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;includefastcgi_params; } location / { index index.html; }}

上面是我的代码,我配置了两个server,server_name 和端口是不一样的但是我访问 kaixuan.hehe.com:8000 竟然也进入了kaixuan.test.com。【注意端口】同样,我访问 kaixuan.test.com 也能进入kaixuan.hehe.com,这正常吗?如果这样正常的话,那么我们在线上怎么解决?加一个默认的让他默认进去吗?

问题解答

回答1:

对的,加一条默认的阻挡。

当所有server的规则都不匹配时,nginx会采用第一条server配置,所以一般第一条server会使用阻止页面。

server { listen 80; server_name _; return 404;}

相关文章: