Reputation: 27218
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)\.?$) {
Upvotes: 1
Views: 1584
Reputation: 27218
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:$2\tH:$host\tHH:$http_host\tSN:$server_name\n;
}
Running the following tests against nginx/1.2.1
:
%printf 'GET / HTTP/1.0\nHost: head.example.com.\n\n' | 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\n\n' | 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\nHost: head.example.com.\n\n' | 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.
Upvotes: 1