0%

LNMP 配置NGINX 支持THINKPHP PATHINFO模式

写在前面的话:
ThinkPHP的四种URL模式:0(普通模式);1(PATHINFO模式);2(REWRITE模式);3(兼容模式)
nginx需要PATHINFO模式,但需要更改nginx配置文件让其支持PATHINFO模式。

首先开启相关目录权限,确保mysql能访问(权限,防火墙)
1.修改php配置文件php.ini,将其中cgi.fix_pathinfo = 0,值改为1
重启php-fpm
2.ssh里执行:

1
2
3
4
5
6
7
8
9
10
11
cat > /usr/local/nginx/conf/pathinfo.conf << 'EOF'
set $real_script_name $fastcgi_script_name;
if ($fastcgi_script_name ~ "(.+?\.php)(/.*)") {
set $real_script_name $1;
set $path_info $2;
}
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;

EOF

3.再将虚拟主机配置文件里的location ~ .*\.(php|php5)?$ 替换为:location ~ .*\.php
再在include fcgi.conf; 下面添加一行include pathinfo.conf;
重启nginx

完整的虚拟主机配置文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
server
{
listen 80;
#listen [::]:80;
server_name www.rdzyw.com;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/www.rdzyw.com;

include other.conf;
#error_page 404 /404.html;
include enable-php.conf;

location ~ .*\.php
{
#try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
include pathinfo.conf;
}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}

location ~ .*\.(js|css)?$
{
expires 12h;
}

location ~ /\.
{
deny all;
}

if (!-e $request_filename){
rewrite ^/(.*)$ /index.php?s=/$1 last;
}

access_log /home/wwwlogs/www.rdzyw.com.log;
}