使用nginx设置常量SERVER_NAME(Set constant SERVER_NAME with nginx)

我有以下结构的nginx.conf

http { [ ... ] server { [ ... ] location ~ \.php$ { fastcgi_pass unix:/run/php/php7.0-fpm.sock; fastcgi_split_path_info ^(.+\.php)(/.*)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SERVER_NAME $host; fastcgi_read_timeout 3000; include fastcgi_params; } } }

这个nginx在Docker中运行,所以它不知道哪个域链接到它(托管系统上有nginx反向代理)。 但我有一个问题,当我尝试从PHP访问$_SERVER['SERVER_NAME']时,它是空的...如何将其设置为常量值? 当我尝试:

fastcgi_param SERVER_NAME example.com

它还是空的。

请注意,我必须使用SERVER_NAME ,因为它是第3部分代码。

I have nginx.conf with following structure:

http { [ ... ] server { [ ... ] location ~ \.php$ { fastcgi_pass unix:/run/php/php7.0-fpm.sock; fastcgi_split_path_info ^(.+\.php)(/.*)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SERVER_NAME $host; fastcgi_read_timeout 3000; include fastcgi_params; } } }

This nginx runs inside Docker so it has no idea, which domain is linked to it (there is nginx reverse proxy on hosting system). But I have a problem that when I try to acces $_SERVER['SERVER_NAME'] from PHP, it's empty... How can I set it to constant value? When I tried:

fastcgi_param SERVER_NAME example.com

it's still empty.

Please note that I have to use SERVER_NAME, because it's in 3rd part code.

最满意答案

设置相同参数的多个fastcgi_param语句(在同一块级别)将静默使用最后一个语句中的值。 这包括通过include指令读取的语句

始终在include fastcgi_params;之后声明fastcgi_param语句include fastcgi_params; 声明,以避免配置文件中的任何歧义。

Multiple fastcgi_param statements (at the same block level) setting the same parameter will silently use the value from the last statement. This includes statements read via an include directive.

Always declare fastcgi_param statements after the include fastcgi_params; statement to avoid any ambiguity in your configuration files.

更多推荐