本文介绍了“主机"时的 nginx 服务器名称正则表达式标题有一个尾随点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经考虑过将 可能的尾随主机名点处理分为两种nginx 中的上下文,并且很好奇是否需要在任何一个中使用才能完全正确配置:

I've considered potential trailing hostname dot handling in two contexts in nginx, and was curious whether usage in either one is necessary for an entirely correct configuration:

  • server_name ~^(w+).(example.com).?$;

if ($host ~ ^(w*).(example.com).?$) {

推荐答案

不,在这两种情况下都不需要——nginx 会自动处理尾随点,无论是在 $host 变量,以及 server_name 指令,只留下 $http_host 变量 带有额外的点(如果存在于请求中).

No, it is not necessary in either context — nginx automatically takes care of the trailing dot, both in the context of the $host variable, as well as the server_name directive, leaving only the $http_host variable with the extra dot (if present in the request).

我相信它是在 http/ngx_http_request.c#ngx_http_validate_host:

1925    if (dot_pos == host_len - 1) {
1926        host_len--;
1927    }

可以通过以下最小配置进行验证:

It can be verified with the following minimal config:

server {
    listen  [::]:7325;
    server_name ~^(w*).?(example.com.?)$;
    return  200 C:$2	H:$host	HH:$http_host	SN:$server_name
;
}

针对 nginx/1.2.1 运行以下测试:

Running the following tests against nginx/1.2.1:

%printf 'GET / HTTP/1.0
Host: head.example.com.

' | nc localhost 7325 | fgrep example
C:example.com   H:head.example.com  HH:head.example.com.    SN:~^(w*).?(example.com.?)$
%
%printf 'GET http://line.example.com./ HTTP/1.0

' | nc localhost 7325 | fgrep example
C:example.com   H:line.example.com  HH: SN:~^(w*).?(example.com.?)$
%
%printf 'GET http://line.example.com./ HTTP/1.0
Host: head.example.com.

' | nc localhost 7325 | fgrep example
C:example.com   H:line.example.com  HH:head.example.com.    SN:~^(w*).?(example.com.?)$
%

请注意,server_name 指令中的正则表达式捕获和 $host 变量都没有尾随点.因此,在上述情况下解释它是没有意义的.

Note that neither the regexp capture from within the server_name directive, nor the $host variable, ever has a trailing dot. As such, it is pointless to account for it in above contexts.

这篇关于“主机"时的 nginx 服务器名称正则表达式标题有一个尾随点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 05:43
查看更多