为了账号安全,请及时绑定邮箱和手机立即绑定

nginx 配置2个域名为什么都指向同一个网站?

nginx 配置2个域名为什么都指向同一个网站?

PHP
小唯快跑啊 2019-03-16 00:29:11
在nginx配置了2个server块,都listen 80端口 server_name不一样,root不一样,为什么访问两个网址都会调到同一个网站?下面上代码 server { listen 80 ; # SSL configuration # # listen 443 ssl default_server; # listen [::]:443 ssl default_server; # # Note: You should disable gzip for SSL traffic. # See: https://bugs.debian.org/773332 # # Read up on ssl_ciphers to ensure a secure configuration. # See: https://bugs.debian.org/765782 # # Self signed certs generated by the ssl-cert package # Don't use them in a production server! # # include snippets/snakeoil.conf; root /code/HappyCoding/public; # Add index.php to the list if you are using PHP index index.php index.html index.htm index.nginx-debian.html; server_name *.happy.co; location / { try_files $uri $uri/ /index.php?$query_string; # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. #try_files $uri $uri/ =404; autoindex_localtime on; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.1-fpm.sock; # With php-cgi (or other tcp sockets): #fastcgi_pass 127.0.0.1:9000; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } server { listen 80 ; # SSL configuration # # listen 443 ssl default_server; # listen [::]:443 ssl default_server; # # Note: You should disable gzip for SSL traffic. # See: https://bugs.debian.org/773332 # # Read up on ssl_ciphers to ensure a secure configuration. # See: https://bugs.debian.org/765782 # # Self signed certs generated by the ssl-cert package # Don't use them in a production server! # # include snippets/snakeoil.conf; root /code/hanxun/public; # Add index.php to the list if you are using PHP index index.php index.html index.htm index.nginx-debian.html; server_name *.hanxun.co; location / { try_files $uri $uri/ /index.php?$query_string; # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. #try_files $uri $uri/ =404; autoindex_localtime on; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.1-fpm.sock; # With php-cgi (or other tcp sockets): #fastcgi_pass 127.0.0.1:9000; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } 求大神指点!
查看完整描述

1 回答

?
不负相思意

TA贡献1777条经验 获得超10个赞

当 nginx 匹配不到任何 server 规则的时候,会默认采用第一条 server 配置。

所以,你可以在最前面加一个空的 server 就可以了。

server {
    listen 80 ;
    server_name _;
    return 403;
}
查看完整回答
1 反对 回复 2019-03-18
  • 1 回答
  • 0 关注
  • 1380 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信