Welcome to 16892 Developer Community-Open, Learning,Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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).?$) {

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
2.2k views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to 16892 Developer Community-Open, Learning and Share
...