如何获取请求nginx的主机名

你好,我正在尝试redirect所有的请求使用https。 我创build了一个侦听端口80(http)的服务器块,我想redirect用户使用https://但我不确定如何获取主机名。

这是我的服务器块

 server { listen 80; server_name : *.example.org return 301 https://{{hostname}}$request_uri } 

我知道URI是由$request_uri访问,但我不确定如何获取主机名。

你们把他们附在一起,没有空间吗?

迈克尔·汉普顿(Michael Hampton)发布的链接更详细地解释了这些variables,但这里是直接的答案。

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

你可以看到它使用curl和看标题。

http://bob.example.com到https://bob.example.com

 [root@hm1 conf.d]# curl --head bob.example.com HTTP/1.1 301 Moved Permanently Server: nginx/1.9.13 Date: Wed, 06 Apr 2016 02:01:24 GMT Content-Type: text/html Content-Length: 185 Connection: keep-alive Location: https://bob.example.com/ Strict-Transport-Security: max-age=15780000; preload X-Frame-Options: SAMEORIGIN X-Content-Type-Options: nosniff 

http://alice.example.com到https://alice.example.com

 [root@hm1 conf.d]# curl --head alice.example.com HTTP/1.1 301 Moved Permanently Server: nginx/1.9.13 Date: Wed, 06 Apr 2016 02:01:39 GMT Content-Type: text/html Content-Length: 185 Connection: keep-alive Location: https://alice.example.com/ Strict-Transport-Security: max-age=15780000; preload X-Frame-Options: SAMEORIGIN X-Content-Type-Options: nosniff