【解釋server_name _】的意思
經常在nginx中看到一段以下的server_name 比對。在虛拟主機當中。
server_name _;
這裡指定的不是什麼特别的名字,它隻是一個無效的域名。從來不會比對任何真實名字相比對。
如:
server {
listen 8080;
server_name _;
access_log /data1/logs/nginx/monitor_access.log base;
root /data1/www/other/monitor;
location / { return 403; }
location ~ "^/(monitor|apc|clear_apc)\.php$" {
fastcgi_pass php-fpm;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
其中當通路:
curl -I http://localhost:8080/monitor.php
日志格式:
log日志為:
0.001 0.001 127.0.0.1 - unix:/dev/shm/php-fpm1.sock [30/Aug/2016:21:12:52 +0800] localhost "HEAD /monitor.php HTTP/1.1" 200 143 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.16.2.3 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2" "-" "-" - "127.0.0.1"
其中localhost為nginx的 $host字段。
當其通路:
curl -I http://192.168.100.10:8080/monitor.php
log中$host為192.168.100.10
注意:
變量中$host
功能:該變量的值等于請求頭中的Host值,如果Host無效時,那麼就處理該請求的Server的名稱。
注意:$host和$http_host變量有些情況是有差別的。$host不包括端口号。
疑問:
curl -I http://localhost:8080/monitor.php
listen 8080;
server_name localhost;
root ...
location / {
.....
}
server_name _;
上面兩個虛拟主機,會被比對到哪個呢?