0
我認爲在nginx的potential trailing hostname dot handling in two contexts,並很好奇的使用在任何一個是否有必要完全正確的配置:nginx的服務器名正則表達式時,「主機」頭具有後點
server_name ~^(\w+)\.(example\.com)\.?$;
if ($host ~ ^(\w*)\.(example\.com)\.?$) {
我認爲在nginx的potential trailing hostname dot handling in two contexts,並很好奇的使用在任何一個是否有必要完全正確的配置:nginx的服務器名正則表達式時,「主機」頭具有後點
server_name ~^(\w+)\.(example\.com)\.?$;
if ($host ~ ^(\w*)\.(example\.com)\.?$) {
不,它不是在任何情況下必須的 - nginx的自動處理的在$host
variable以及server_name
directive的上下文中,僅留下$http_host
variable以及額外的點(如果請求中存在)。
我相信這是在http/ngx_http_request.c#ngx_http_validate_host
實現:
1925 if (dot_pos == host_len - 1) {
1926 host_len--;
1927 }
它可以用下面的最小配置進行驗證:
server {
listen [::]:7325;
server_name ~^(\w*)\.?(example\.com\.?)$;
return 200 C:$2\tH:$host\tHH:$http_host\tSN:$server_name\n;
}
運行下面的測試針對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\.?)$
%
請注意,這兩個正則表達式都不會從內部捕獲server_name
指令或$host
變量都有一個尾部點。因此,在上述情況下解釋它是毫無意義的。