Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set constant SERVER_NAME with nginx

Tags:

php

nginx

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.

like image 507
Pavel Štěrba Avatar asked Jan 12 '17 10:01

Pavel Štěrba


1 Answers

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.

like image 146
Richard Smith Avatar answered Sep 29 '22 09:09

Richard Smith