http - nginx server name regex when "Host" header has a trailing dot -
i've considered potential trailing hostname dot handling in 2 contexts in nginx, , curious whether usage in either 1 necessary entirely correct configuration:
server_name ~^(\w+)\.(example\.com)\.?$;
if ($host ~ ^(\w*)\.(example\.com)\.?$) {
no, not necessary in either context — nginx automatically takes care of trailing dot, both in context of $host
variable, server_name
directive, leaving $http_host
variable dot (if present in request).
i believe implemented in http/ngx_http_request.c#ngx_http_validate_host
:
1925 if (dot_pos == host_len - 1) { 1926 host_len--; 1927 }
it can verified 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 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 neither regexp capture within server_name
directive, nor $host
variable, ever has trailing dot. such, pointless account in above contexts.
Comments
Post a Comment