如何设置 SERVER_NAME 和 HTTP_HOST 值

问题描述 投票:0回答:1

默认.模板

server {
    listen 80 default_server;
    
    add_header Content-Security-Policy "frame-ancestors 'self' https://${content_security_policy_url}";
    add_header X-XSS-Protection "1; mode=block";
    root /var/www/prod/public_html;
    include /etc/nginx/fcgiwrap.conf;
    include /etc/nginx/mime.types;
    index index.php index.sec index.html;
    error_page 405 =200 $uri;


    # Create route of /dashboard to the domain name dashboard
    location /dashboard {
        proxy_pass http://$host/dashboard;
    }

    location / {
        try_files $uri $uri/ /index.php?$args;
        # set_real_ip_from ::/0;
        # set_real_ip_from 0.0.0.0/0;
        # real_ip_header X-Forwarded-For;
        fastcgi_param HTTPS $fastcgi_param_https_variable;
        error_page 500 502 503 504  /500.html;
    }
    location ~ \.php|\.sec$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        fastcgi_param HTTPS $fastcgi_param_https_variable;
        # fastcgi_param sitePath "https://$host/";
        include fastcgi_params;
    }
    location = /500.html {
        root /usr/share/nginx/html;
        internal;
    }
}

我获得动态的子域值,我想使用 HTTP_HOST 中设置的值设置 SERVER_NAME 例如:在 URL 中,如果我在浏览器中点击 abc.test.com,$_SERVER['SERVER_NAME'] 应返回相同的值或 xyz.abc.com,$_SERVER['SERVER_NAME'] 应返回 xyz.abc.com。

我尝试了以下方法 1.

server {
   server_name ~^(?<subdomain>.+)\.abc\.com$;
}

with this solution, If I print $_SERVER['SERVER_NAME'] in code, it doesnt replace the regex with actual value
2. location ~ \.php|\.sec$ {
    fastcgi_param SERVER_NAME $http_host
}

This will not set the expected data, SERVER_NAME will be empty

/etc/nginx/fcgiwrap.conf 文件有 fastcgi_param SERVER_NAME $server_name;

PHP 和 NGINX 运行在 docker 容器下

如果我得到有关如何使用 HTTP_HOST 值设置 S_SERVER['SERVER_NAME'] 的任何输入,那就太好了

蒂亚

php linux nginx server
1个回答
0
投票
  1. 是预期的行为,您给出了正则表达式的虚拟服务器名称,可能有多个名称,那么第一个是主名称,主名称最终将出现在

    $server_name
    变量中。

  2. $http_host
    可能未设置,取决于请求是否包含主机标头,这几乎总是这种情况,因此您很可能忘记了
    fastcgi_params
    默认情况下包含 SERVER_NAME 的设置,类似于您提到的
    fcgiwrap.conf
    ,还有这一行:
    fastcgi_param SERVER_NAME $server_name;
    它将您刚刚所做的自定义设置覆盖为默认值
    $server_name = ""
    ,这就是为什么您只看到空字符串。

© www.soinside.com 2019 - 2024. All rights reserved.