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).
I believe it is implemented in 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:$2H:$hostHH:$http_hostSN:$server_name
;
}
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.?)$
%
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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…