gla*_*ain 140 configuration nginx http-headers
在Nginx中,变量$host和变量之间有什么区别$http_host.
gla*_*ain 191
$host是Core模块的变量.
$主机
如果Host标头不可用,则此变量等于请求标头中的行Host或处理请求的服务器的名称.
在这种情况下,此变量可能与$ http_host具有不同的值:1)当Host输入头不存在或具有空值时,$ host等于server_name指令的值; 2)当Host的值包含端口号时,$ host不包含该端口号.自0.8.17起,$ host的值始终为小写.
$http_host也是同一模块的变量,但您不会使用该名称找到它,因为它通常定义为$http_HEADER(ref).
$ HTTP_HEADER
转换为小写并将"破折号"转换为"下划线"时HTTP请求标头HEADER的值,例如$ http_user_agent,$ http_referer ...;
总结:
$http_host始终等于HTTP_HOST请求标头.$host等于$http_host,小写且没有端口号(如果存在),除非HTTP_HOST缺少或为空值.在这种情况下,$host等于server_name处理请求的服务器的指令的值.Chr*_*ian 21
接受的答案及其评论似乎不再正确。文档(http://nginx.org/en/docs/http/ngx_http_core_module.html#var_host)说的$host是
\n\n按此优先顺序:请求行中的主机名,或 \xe2\x80\x9cHost\xe2\x80\x9d 请求标头字段中的主机名,或与请求匹配的服务器名称
\n
$http_host标头字段的值始终如此Host。如果请求行中的主机(如果指定)与Host标头字段不同,它们可能会有所不同。或者如果Host未设置标题。
server_name仅匹配Host标头字段(http://nginx.org/en/docs/http/request_processing.html),因此$host可能与匹配的server_name.
Ste*_*ing 16
$http_host$http_host始终等于Host请求标头字段
Host: example.org
Run Code Online (Sandbox Code Playgroud)
$host$host按照以下优先顺序(从高到低):
GET http://example.org/test/ HTTP/1.1
Run Code Online (Sandbox Code Playgroud)
Host请求头字段server_name的(Nginx 配置中),即使是server_name通配符(例如server_name *.example.org;:)当打开网址http://example.org/test/...
大多数浏览器都是这样发送请求的
GET /test/ HTTP/1.1
Host: example.org
Run Code Online (Sandbox Code Playgroud)
大多数浏览器不会像这样发送请求(但这是有效的请求)
GET http://example.org/test/ HTTP/1.1
Run Code Online (Sandbox Code Playgroud)
server {
listen 80;
server_name *.example.org;
location / {
default_type "text/plain";
return 200 "[host] = $host";
}
}
Run Code Online (Sandbox Code Playgroud)
$host= 请求行中的主机名
Host: example.org
Run Code Online (Sandbox Code Playgroud)
该命令将
127.0.0.1GET http://request.example.org/test/ HTTP/1.1Host标题设置为Host: host.example.org* Trying 127.0.0.1:80...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> GET http://request.example.org/test/ HTTP/1.1
> Host: host.example.org
> User-Agent: curl/7.68.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Server: nginx/1.23.1
< Date: Fri, 21 Oct 2022 02:00:56 GMT
< Content-Type: text/plain
< Content-Length: 28
< Connection: keep-alive
<
* Connection #0 to host 127.0.0.1 left intact
[host] = request.example.org
Run Code Online (Sandbox Code Playgroud)
Host存在标头时...$host=Host标题
GET http://example.org/test/ HTTP/1.1
Run Code Online (Sandbox Code Playgroud)
* Trying 127.0.0.1:80...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> GET /test/ HTTP/1.1
> Host: host.example.org
> User-Agent: curl/7.68.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Server: nginx/1.23.1
< Date: Fri, 21 Oct 2022 02:01:37 GMT
< Content-Type: text/plain
< Content-Length: 25
< Connection: keep-alive
<
* Connection #0 to host 127.0.0.1 left intact
[host] = host.example.org
Run Code Online (Sandbox Code Playgroud)
$host= server_name(在 Nginx 配置中)
GET /test/ HTTP/1.1
Host: example.org
Run Code Online (Sandbox Code Playgroud)
* Trying 127.0.0.1:80...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> GET /test/ HTTP/1.0
> User-Agent: curl/7.68.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Server: nginx/1.23.1
< Date: Fri, 21 Oct 2022 02:02:20 GMT
< Content-Type: text/plain
< Content-Length: 22
< Connection: close
<
* Closing connection 0
[host] = *.example.org
Run Code Online (Sandbox Code Playgroud)
参考:ngx_http_core_module,Nginx$host验证
| 归档时间: |
|
| 查看次数: |
123479 次 |
| 最近记录: |