PHP中SERVER_NAME获取的问题

PHP代码

网站配置根目录下 info.php

<?php
echo "<pre>";
echo $_SERVER['SERVER_NAME'];

host配置

127.0.0.1 www.my-search.com
127.0.0.1 www.my-play.com

nginx配置

server {
        listen        80;
        server_name  www.my-search.com www.my-play.com;
        root   "D:/phpstudy_pro/WWW/phpproject/mySearch/public";
		index  index.html index.php;
		
        location / {
		 if (!-e $request_filename) {
		    rewrite ^/index.php(.*)$ /index.php?s=$1 last;
            rewrite ^(.*)$ /index.php?s=$1 last;
            break;
			}
        }
		
        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9002;
			fastcgi_index  index.php;
			fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
		    include        fastcgi_params;
			
            # fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            # fastcgi_param  PATH_INFO  $fastcgi_path_info;
            # fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
           
        }
}

PHP代码中获取 $_SERVER['SERVER_NAME'] 是域名1:http://www.my-search.com/info.php,还是域名2: http://www.my-play.com/info.phpserver name,结果都是nginx配置中server_name的第一个值

配置的域名调换顺序,重启nginx再访问

参考: https://www.cnblogs.com/linewman/p/9918112.html

解决办法

  1. 两个域名分开配置

  2. 找nginx相关配置是可以解决的,我比较的懒。懒的找

posted @ 2022-06-06 22:25  ncsb  阅读(167)  评论(0编辑  收藏  举报