Getting wrong $_SERVER[‘SERVER_NAME’] on Nginx PHP5-FPM

I was testing some old code that used $_SERVER[‘SERVER_NAME’] to get the sub domain, but I was getting *.mysite.com  no matter what was the sub domain, so looking on the nginx site config file on the php section had and include

location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params; #<-- includes fastcgi paramters file
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}

which calls a file /etc/nginx/fastcgi_params

On the file I found the problem, nginx send as SERVER_NAME parameter the $server_name var, which in this case because we use wildcards  anysubdomain.mysite.com will trigger *.mysite.com  and that’s why the nginx variable get $server_name get that domain

To fix this problem I just comment that $server_name and use the $http_host variable.

fastcgi_param  SERVER_NAME        $http_host; #$server_name;

Restart nginx and test

sudo service nginx restart

 

400 Bad Request – request header or cookie too large

I was getting this error on heavy usage Cookie App in nginx, so looking at nginx documation, found the solution

Terminal

you can go global or site specific, because large_client_header_buffers. it’s only valid in http or server contexts,

so for only site specific I went server way

sudo nano /etc/nginx/sites-enabled/yoursite.com

Add this line on your server context

large_client_header_buffers 4 16k;

you can play with size of the client buffer, I need only 4 16k , default is  4 8k

restart nginx and test

sudo service nginx restart