Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP SERVER_NAME not passed correctly for multiple canonical domains #1550

Closed
strarsis opened this issue Nov 2, 2024 · 2 comments · Fixed by #1552
Closed

PHP SERVER_NAME not passed correctly for multiple canonical domains #1550

strarsis opened this issue Nov 2, 2024 · 2 comments · Fixed by #1552
Labels

Comments

@strarsis
Copy link
Contributor

strarsis commented Nov 2, 2024

Version

1.22.1

What did you expect to happen?

PHP $_SERVER['SERVER_NAME'] is assigned the correct domain that is requested,
not the first (or random?) one in server_name in nginx site configuration.

This prevents redirection from redirection plugins.

What actually happens?

PHP $_SERVER['SERVER_NAME'] is assigned incorrect domain, as the first (or random?) one in server_name in nginx site configuration.

Steps to reproduce

  1. Set up a site in Trellis with two or more canonical domains (not to be confused with redirections).
  2. Request one of the canonical domains.
  3. Debug the request (e.g. print_r($_SERVER); in index.php).
    Note that the SERVER_NAME field in $_SERVER is not correct for all other requested canonical domains except for one (usually the first listed one).

Note that the SERVER_NAME is not always the first canonical domain, apparently this can change, which caused issues with a redirection plugin that redirected from the wrong canonical domain.

System info

Ubuntu LTS

Log output

No response

Please confirm this isn't a support request.

Yes

Context

In order to fix the passed SERVER_NAME, $host instead of $server_name has to be used:

fastcgi_param SERVER_NAME         $host;

In fastcgi_params configuration file included by the sites, $server_name is used:

fastcgi_param  SERVER_NAME        $server_name;

And $server_name is the wrong value:

  • $host contains "in this order of precedence: host name from the request line, or host name from the 'Host' request header field, or the server name matching a request"
  • $http_host contains the content of the HTTP "Host" header field, if it was present in the request
  • $server_name contains the server_name of the virtual host which processed the request, as it was defined in the nginx configuration. If a server contains multiple server_names, only the first one will be present in this variable.

(from Michael Hampton in https://serverfault.com/a/706439/958731).

As fastctgi_params is a default configuration file shipped with nginx, it makes sense to
override the SERVER_NAME field after that file was included, in order to override it effectively:

include fastcgi_params;
[...]
fastcgi_param SERVER_NAME $host;
@strarsis strarsis added the bug label Nov 2, 2024
@swalkinshaw
Copy link
Member

Makes sense to me. I assume it should be added right after the include here?

include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;

Want to contribute the fix?

@strarsis
Copy link
Contributor Author

PR: #1552

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants