Go Back   HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials > ISPConfig 3 > Installation/Configuration

Do you like HowtoForge? Please consider supporting us by becoming a subscriber.
Reply
 
Thread Tools Display Modes
  #1  
Old 3rd July 2013, 16:38
cbj4074 cbj4074 is offline
Senior Member
 
Join Date: Nov 2010
Posts: 392
Thanks: 29
Thanked 58 Times in 50 Posts
Default CGI variable SERVER_NAME lacks the www prefix despite auto-subdomain and SEO redirect

Hello,

I'm running ISPConfig 3.0.5.2 with nginx.

I noticed that the CGI variable SERVER_NAME lacks the www prefix, despite auto-subdomain being set to "www" and SEO redirect set to "domain.tld -> www.domain.tld".

My hunch is that given that SERVER_NAME can and should only contain a single server name string (and not both of the names that are defined in the nginx configuration), the server_name that's listed first is takes precedence and is used for the CGI SERVER_NAME value.

The vhost's nginx configuration file contains the following line (from ISPC's template, presumably):

Code:
server_name example.com www.example.com;
As stated above, SERVER_NAME takes the first value, "example.com".

Given that I've set auto-subdomain to www and the SEO redirect to www, does it not follow that the server_name values should be listed in the opposite order?

Code:
server_name www.example.com example.com;
The reason that this is a problem for me is that my PHP application compares the SERVER_NAME to a known, constant value, and if the two values don't match, a redirect to the correct domain, with https prepended, is performed. This eliminates "SSL certificate mismatch" warnings (provided that the domain has a proper certificate installed).

Due to the behavior that I describe, this logic fails and causes an endless redirection loop, because SERVER_NAME is "example.com" when the PHP constant is defined as "www.example.com".

Basically, I'm wondering if this is a "bug" and ISPConfig should swap the order of the server_name values in the nginx configration when the user has set auto-subdomain to www and set SEO redirect to www.

When I list the server_name values in the opposite order, with the www subdomain first, then the problem disappears and my application works correctly. This seems to confirm that nginx assigns the first value listed to the SERVER_NAME CGI variable.

Or, if there is some other way to achieve what I'm after, I am all ears.

Thanks!

Last edited by cbj4074; 3rd July 2013 at 16:45.
Reply With Quote
Sponsored Links
  #2  
Old 3rd July 2013, 17:41
till till is online now
Super Moderator
 
Join Date: Apr 2005
Location: Lüneburg, Germany
Posts: 35,811
Thanks: 821
Thanked 5,339 Times in 4,188 Posts
Default

Quote:
I noticed that the CGI variable SERVER_NAME lacks the www prefix, despite auto-subdomain being set to "www" and SEO redirect set to "domain.tld -> www.domain.tld".
Thats correct.

Quote:
Given that I've set auto-subdomain to www and the SEO redirect to www, does it not follow that the server_name values should be listed in the opposite order?
No. The main domain of the website is the domain name that you entered into the domain field of the website and the main domain is listed first.
__________________
Till Brehm
--
Get ISPConfig support and the ISPConfig 3 manual from ispconfig.org.
Reply With Quote
  #3  
Old 3rd July 2013, 18:29
cbj4074 cbj4074 is offline
Senior Member
 
Join Date: Nov 2010
Posts: 392
Thanks: 29
Thanked 58 Times in 50 Posts
Default

Quote:
No. The main domain of the website is the domain name that you entered into the domain field of the website and the main domain is listed first.
Ah, I see. So, all I need to do is add the www to the main domain name for the website and the problem will be solved.

My only other question is whether or not including the www subdomain in the main domain name will change the behavior of either a) setting auto-subdomain to "www", and/or b) setting SEO redirect to "domain.tld -> www.domain.tld"; does the main domain value have any effect on the behavior of those settings?

Thanks for clarifying, Till!
Reply With Quote
  #4  
Old 3rd July 2013, 19:15
till till is online now
Super Moderator
 
Join Date: Apr 2005
Location: Lüneburg, Germany
Posts: 35,811
Thanks: 821
Thanked 5,339 Times in 4,188 Posts
Default

The auto subdomain www function adds a aliasdomain to the vhost were www is prepended to the main domain name. So if your main domain name is already www.domain.tld and you enable the auto subdomain, then you will have a alias for www.www.domain.tld.

The seo redirect will then be "www.domain.tld -> www.www.domain.tld".
__________________
Till Brehm
--
Get ISPConfig support and the ISPConfig 3 manual from ispconfig.org.
Reply With Quote
  #5  
Old 9th July 2013, 19:10
cbj4074 cbj4074 is offline
Senior Member
 
Join Date: Nov 2010
Posts: 392
Thanks: 29
Thanked 58 Times in 50 Posts
Default

Okay, so I modified the main domain name so that it includes the www. To be clear, I changed it from "example2.com" to "www.example2.com" in the Clients -> Domains interface.

(It is important to note that I have www.example1.com on this server, too, which is a completely unrelated domain.)

This "fixes" the problem of the CGI "SERVER_NAME" variable lacking the www prefix (it now reflects "www.example2.com", as expected), but there is an unintended side-effect: SEO redirects no longer function as expected.

After making this change, if I try to browse to example2.com (without the www), the SEO redirect, which I have set to "domain.tld => www.domain.tld", is not effective.

Not only is the redirect not effective, but browsing to example2.com redirects me to example1.com, which is a very bad scenario in a production environment. Visitors to one client's site see a different client's site.

I assume that this occurs because a symlink for example2.com doesn't actually exist in /etc/nginx/sites-available (nor should it; the symlink name is now www.example2.com), so nginx redirects the request to the "first domain, alphabetically, with a '100-' prefix" with no regard whatsoever for any SEO redirect that might be set for www.example2.com (e.g., "domain.tld => www.domain.tld").

Is this a case of "operator error"? Or is this an actual problem/bug?

Again, all I want is for the CGI variable "SERVER_NAME" to include the "www" prefix, without having to include the www subdomain in the main domain name. Not only is including the www in the main domain name redundant when SEO redirect is set, but doing this causes the SEO redirects to stop functioning altogether.

I suppose my only option is to add custom nginx configuration directives to set the SERVER_NAME manually, e.g., change:

Code:
fastcgi_param	SERVER_NAME		$server_name;
to

Code:
fastcgi_param	SERVER_NAME		www.$server_name;
(I haven't actually tested the above syntax, either.)
Reply With Quote
  #6  
Old 9th July 2013, 22:28
cbj4074 cbj4074 is offline
Senior Member
 
Join Date: Nov 2010
Posts: 392
Thanks: 29
Thanked 58 Times in 50 Posts
Default

A quick follow-up after making some progress.

With the objective "to redirect requests for all subdomains (other than www) to www subdomain", I was able to fix all of the "unexpected" SEO redirection issues by setting Auto-Subdomain to "*." and setting SEO Redirect to "* => www.domain.tld.

As it turns-out (and as usual), ISPConfig is behaving "correctly".

That leaves just the SERVER_NAME CGI variable problem. With the above settings, the server_name directive looks like this:

Code:
server_name example2.com *.example2.com;
When I var_dump() the $_SERVER['SERVER_NAME'] from within PHP, the value is example2.com. (I should note an important correction to an assumption made in my initial post: it seems that nginx doesn't always use the first server_name value in a list of several values; unfortunately, I don't know by what criteria nginx decides which value to assign to its $server_name configuration variable, which is later passed to CGI.)

Of course, as stated previously, I need this value to be www.example2.com, in order to secure my application against misconfiguration or malice.

I was able to do this by overwriting and hard-coding the fastcgi_param that is defined within ISPC's nginx vhost template:

Code:
location @php {
            try_files $uri =404;
            include /etc/nginx/fastcgi_params;
            fastcgi_param	SERVER_NAME		www.example2.com;
            # [...]
}
Obviously, this approach is unacceptable because ISPC will overwrite the change whenever the vhost is modified via the GUI.

Ultimately, I am wondering if there is a "correct" means by which to achieve the same. I'm failing to see how I might overwrite the SERVER_NAME value within the vhost's "nginx Directives" field, given that the "location @php" block is already closed by the time those directives are written to the configuration file.

Thanks again, Till.

EDIT:

I'm all set, regarding the SERVER_NAME value; the following works:

Code:
fastcgi_param	SERVER_NAME		www.$server_name;

Last edited by cbj4074; 10th July 2013 at 16:06.
Reply With Quote
  #7  
Old 9th July 2013, 22:59
till till is online now
Super Moderator
 
Join Date: Apr 2005
Location: Lüneburg, Germany
Posts: 35,811
Thanks: 821
Thanked 5,339 Times in 4,188 Posts
Default

ISPConfig can merge the nginx settings. Try this:

Code:
location @php {
            ##merge##
            try_files $uri =404;
            include /etc/nginx/fastcgi_params;
            fastcgi_param	SERVER_NAME		www.example2.com;
            ##merge##
}
__________________
Till Brehm
--
Get ISPConfig support and the ISPConfig 3 manual from ispconfig.org.
Reply With Quote
  #8  
Old 10th July 2013, 16:01
cbj4074 cbj4074 is offline
Senior Member
 
Join Date: Nov 2010
Posts: 392
Thanks: 29
Thanked 58 Times in 50 Posts
 
Default

Thanks for the info, Till.

Does the snippet you provided go into the ISPConfig nginx configuration template? Or somewhere else?

If it goes into the template, I assume that the settings will apply to all vhosts. I need to change the hard-coded SERVER_NAME for each vhost; am I out of luck?

EDIT:

This seems to work, regarding the SERVER_NAME value being generated dynamically:

Code:
fastcgi_param	SERVER_NAME		www.$server_name;

Last edited by cbj4074; 10th July 2013 at 16:14.
Reply With Quote
Reply

Bookmarks

Tags
conf, config, nginx, server, server_name

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 14:13.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.