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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值