在我的 PHP 应用程序中:
die($_SERVER['SERVER_NAME']);
请求:
sub1.example.com
结果:
example.com
我的NGINX配置:
server
{
server_name .example.com;
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
root /var/www/html/example;
index index.php;
location ~ \.php$ {
if (-f $request_filename) {
fastcgi_pass 127.0.0.1:9000;
}
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location / {
if ($request_filename ~ /sub1) {
rewrite ^ http://sub1.example.com/? permanent;
}
if ($request_filename ~ /sub2) {
rewrite ^ http://sub2.example.com/? permanent;
}
if (!-f $request_filename) {
rewrite ^(.*)$ /index.php last;
}
}
}
为什么 php 中没有拾取子域名?
答案1
$_SERVER['SERVER_NAME']
是虚拟主机的名称,位于.example.com
您的 nginx 配置中。$_SERVER['HTTP_HOST']
无论 nginx server_name 值如何,您都需要从请求中获取主机名。