在我的PHP应用程序中:
die($_SERVER['SERVER_NAME']);
请求:
sub1.example.com
结果:
example.com
我的NGINXconfiguration:
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中没有拿起子域?
$_SERVER['SERVER_NAME']是虚拟主机的名称,在您的nginxconfiguration中是.example.com 。 如果需要来自请求的主机名,则无论nginx server_name的值如何,您都需要$_SERVER['HTTP_HOST'] 。