Welcome! Log In Create A New Profile

Advanced

showing correct server_name to fcgi (php)

Posted by eimermusic 
showing correct server_name to fcgi (php)
July 02, 2009 09:47AM
Hi,
I am in the process of deploying nginx to my servers but have run into a small problem.

I have php and rewrites working fine (thanks to all the excellent guides and examples around.).
The thing that is a bit odd though is that nginx forwards the first server_name defined to fcgi... not the one actually being requested.

My server block has:
server_name client1.example.com client2.example.com client3.example.com;

php $_SERVER['SERVER_NAME'] is always client1.example.com no matter which domain I request.

the fastcgi.params file is untouched and has the following like to set the server name for php (I think).
fastcgi_param SERVER_NAME $server_name;

I was wondering if I was doing something wrong or if nginx can't be configured like this? In Apache I used the ServerName and then a ServerAlias for each extra domain pointing to the same root.

I have figured out that I can put 99% of my server block into a default config file, create a separate "vhost" for each server_name and include the default for the rest of the rewrites and things. THis would be my fall-back setup but I would prefer to be able to pass on the requested server_name to php.

Thanks in advance for any suggestions.
Re: showing correct server_name to fcgi (php), is it safe?
July 02, 2009 10:20AM
Reading the manual a bit more led me to the following:

fastcgi_param SERVER_NAME $host;

This change makes php see the "correct" server name but I wonder if this is an unsafe setting or bad in some other way?
Re: showing correct server_name to fcgi (php)
July 02, 2009 08:20PM
I doubt that it's a security risk but I have never encountered this as an issue. What version of nginx are you running?

The default fastcgi_params file that is installed on a fresh nginx compile has

fastcgi_param SERVER_NAME $server_name;

So I wonder if Igor is aware of this...

--
Jim Ohlstein
Re: showing correct server_name to fcgi (php)
July 03, 2009 03:41AM
Hi,
I am on the stable version (0.7...) but have also checked this on the Ubuntu apt version (0.5.33).

The thing is that php show the requested name in HTTP_HOST while SERVER_NAME shows the first name in the configuration. So this may not at all be anything like a bug but simple a difference in interpretation of the use of these values.

I do believe that most php applications use the SERVER_NAME though.
Re: showing correct server_name to fcgi (php)
July 30, 2016 04:48PM
I've now been bitten by the same issue.
So, let me clarify the question:
How to pass correct SERVER_NAME to the CGI interpreter?
With "correct" being "the one matching HTTP_HOST, or the first entry from server_name othervise".
Use case is, I have two names for a server. One I use in my local network for development, and one I use to show the site to the outside world.
Building a convoluted DNS overrides is not an option. Building equally convoluted rewrites at application level is not an option, either.
Re: showing correct server_name to fcgi (php)
August 05, 2016 07:27AM
Worked around it in two steps.
1. Created an unrelated catch-all vhost.
2. In real hosts used $http_host instead of $server_name where necessary, since $http_host will always match to one of the server names, given #1.
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 216
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 500 on July 15, 2024
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready