PHP中SERVER_NAME获取的问题

2022/6/7 1:20:43

本文主要是介绍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相关配置是可以解决的,我比较的懒。懒的找



这篇关于PHP中SERVER_NAME获取的问题的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程