nginx “server” directive with multiple “server_nam

2019-03-17 06:13发布

My configuration file has a server directive block that begins with...

server {
    server_name www.example1.com www.example2.com www.example3.com;

...in order to allow the site to be accessed with different domain names.

However PHP's $_SERVER['SERVER_NAME'] always returns the first entry of server_name, in this case http://www.example1.com

So I have no way from the PHP code to know which domain the user used to access the site.

Is there any way to tell nginx/fastcgi to pass the real domain name used to access the site?


The only solution I've found so far is to repeat the entire server block for each domain with a distinct server_name entry but obviously I'm looking for a better one.

2条回答
叛逆
2楼-- · 2019-03-17 06:55

Set SERVER_NAME to use $host in your fastcgi_params configuration.

fastcgi_param   SERVER_NAME         $host;

Source: http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_param

查看更多
等我变得足够好
3楼-- · 2019-03-17 07:00

This is intended and the proper solution is to use $_SERVER['HTTP_HOST'] in your code instead.

You should interpret SERVER_NAME as a verified server-name, and HTTP_HOST as user-input which can be modified quite easily, and thus shouldn't be trusted.

查看更多
登录 后发表回答