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

1. bug 描述

当请求 https://shandiankd.chengwithle.com/order/index 会莫名的跳转到 http://wndd.mayiapps.cn/
nginx.conf 配置如下

server {
    listen 443 ssl;
    server_name kaola.chengwithle.com;
    ...
    省略 ssl 配置
	...
    rewrite  ^/(.*)$  http://wndd.mayiapps.cn/$1 permanent;
}

2. 原因排查

如果 nginx 找不到任何一个 server 匹配,那就会使用监听这个端口的默认配置进行处理,如果没有默认则使用第一个

If its value does not match any server name, or the request does not contain this header field at all, then nginx will route the request to the default server for this port.

参考: How nginx processes a request

3. 解决方案

增加一个 https 的默认配置

server {
   listen 80 default_server;
   server_name _;
	...省略 ssl 的配置   
   return 404;
}

或者返回 301 http请求

server {
    listen 443 ssl;
    server_name kaola.chengwithle.com;
    ...
    省略 ssl 配置
	...
    rewrite  ^/(.*)$  http://$host/$1 permanent;
}

你可能感兴趣的:(网络,bug,nginx,http)