DestinyHunter / DestinyHunter.github.io

Blog
https://DestinyHunter.cn
1 stars 0 forks source link

Thinkphp nginx 隐藏 index.php #9

Open DestinyHunter opened 6 years ago

DestinyHunter commented 6 years ago
server {  
 ...  
    location / {  
        index  index.htm index.html index.php;  
        #如果文件不存在则尝试TP解析  
        try_files  $uri  /index.php$uri;  # 重写 index.php
    }  
    location ~ .+\.php($|/) {  
        root        /var/www/html/website;  
        fastcgi_pass   127.0.0.1:9000;  
        fastcgi_index  index.php;  

        #设置PATH_INFO,注意fastcgi_split_path_info已经自动改写了fastcgi_script_name变量,  
        #后面不需要再改写SCRIPT_FILENAME,SCRIPT_NAME环境变量,所以必须在加载fastcgi.conf之前设置  
        fastcgi_split_path_info  ^(.+\.php)(/.*)$;  
        fastcgi_param  PATH_INFO $fastcgi_path_info;  

        #加载Nginx默认"服务器环境变量"配置  
        include        fastcgi.conf;  
    }  
}  
DestinyHunter commented 6 years ago

旧版

server {  
 ...  
    location / {  
        index  index.htm index.html index.php;  
        #访问路径的文件不存在则重写URL转交给ThinkPHP处理  
        if (!-e $request_filename) {  
           rewrite  ^/(.*)$  /index.php/$1  last;  
           break;  
        }  
    }  
    location ~ \.php/?.*$ {  
        root        /var/www/html/website;  
        fastcgi_pass   127.0.0.1:9000;  
        fastcgi_index  index.php;  
        #加载Nginx默认"服务器环境变量"配置  
        include        fastcgi.conf;  

        #设置PATH_INFO并改写SCRIPT_FILENAME,SCRIPT_NAME服务器环境变量  
        set $fastcgi_script_name2 $fastcgi_script_name;  
        if ($fastcgi_script_name ~ "^(.+\.php)(/.+)$") {  
            set $fastcgi_script_name2 $1;  
            set $path_info $2;  
        }  
        fastcgi_param   PATH_INFO $path_info;  
        fastcgi_param   SCRIPT_FILENAME   $document_root$fastcgi_script_name2;  
        fastcgi_param   SCRIPT_NAME   $fastcgi_script_name2;  
    }  
}