当前位置: 移动技术网 > 网络运营>服务器>nginx > Nginx 重定向时获取域名的方法示例

Nginx 重定向时获取域名的方法示例

2019年04月17日  | 移动技术网网络运营  | 我要评论

st45.8,宝泉投资银条,液晶显示

tl;dr

如果你在处理 nginx 重定向时要获取原请求的域名(比如 http 到 https),请用 $host 而不是 $server_name 。

问题和解决方案

今天碰到一个问题,服务器上一个子域名的请求重定向到另一个子域名上面去了。查了一段时间发现这个问题只有在 http 到 https 跳转的时候才会发生。大概是这样:

从 http 的 sub2 子域名跳转到 https 的 sub1 子域名

http://sub2.example.com/more_things -> https://sub1.example.com/more_things

我用的 nginx ,当初为了让 http 请求跳转到同名的 https 请求,配置如下:

http {
 server {
 listen  80;
 server_name sub1.example.com sub2.example.com;
 return  301 https://$server_name$request_uri;
 }

 server {
 listen  443 ssl spdy;
 server_name sub1.example.com sub2.example.com;
 # ...
 }
}

因为 301 是永久重定向,某些浏览器的缓存会记住重定向,下次访问原地址就会直接向新地址发请求,所以这个问题在浏览器里面不一定重现得了(包括 chrome 的 incognito window),能每次完整重现的方式只有 curl 。

$ curl -i http://sub2.example.com/

http/1.1 301 moved permanently
server: nginx/1.9.3 (ubuntu)
date: tue, 23 feb 2016 06:06:30 gmt
content-type: text/html
content-length: 193
connection: keep-alive
location: https://sub1.example.com/

查了一下,发现问题出在 $server_name 变量上。这个变量会始终返回 server_name 中第一个名字。这里其实应该用 $host 变量。修改后的配置如下:

http {
 server {
 listen  80;
 server_name sub1.example.com sub2.example.com;
 return  301 https://$host$request_uri;
 }
}

$host 变量会按照以下优先级获取域名:

  1. request-line 中的域名信息。request-line 包含 method, uri 和 http 版本。
  2. 请求头信息中的 "host" 。
  3. nginx 中匹配的 server_name 配置。

这几乎可以保证在任何环境下正确地得到域名。如果是同域名下的重定向最好都用 $host 。

参考资料


nginx 官方文档。其中对 $host 讲的比较详细,但 $server_name 只是一笔带过。

stackoverflow - what is the difference between nginx variables $host, $http_host, and $server_name?
stackoverflow 上关于三个变量区别的讨论。里面提到了为什么 $host 是适用于所有场景的唯一选择。

http/1.1 : request-line
http/1.1 规范中对 request-line 的描述。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网