一个计算机技术爱好者与学习者

0%

好好学PHP:从零开始打造自己的PHP框架——第7章

1. 前言

《thinkphp部署到nginx服务器》一文中,小生提到过,nginx默认情况下不支持pathinfo模式,从而不能支持ThinkPHP。能访问的,只有首页,其他函数的路径,都无法访问。

这是因为,PHP中的全局变量$_SERVER['PATH_INFO'],常常被用来优化url路径格式,比如thinkphp,而nginx是不支持pathinfo的。

同样的,小生开发的vkphp,虽然没有使用$_SERVER['PATH_INFO'],但是也使用到了$_SERVER,所以,也需要配置nginx支持pathinfo。

2. 完整demo

1、首先,查看nginx配置文件的位置,ps aux | grep nginx

2、进入conf/vhost目录,添加enroll.voidking.com.conf文件。

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
43
44
45
46
47
48
49
50
51
52
53
54
server {
listen 80;
server_name enroll.voidking.com;
root /home/wwwroot/enroll;
index index.html index.htm index.php;

error_page 404 /404.html;
location = /404.html {
return 404 'Sorry, File not Found!';
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/nginx/html; # windows用户替换这个目录
}

location / {
try_files $uri @rewrite;
}

location @rewrite {
set $static 0;
if ($uri ~ \.(css|js|jpg|jpeg|png|gif|ico|woff|eot|svg|css\.map|min\.map)$) {
set $static 1;
}

if ($static = 0) {
rewrite ^/(.*)$ /index.php?s=/$1;
}

}

location ~ /Uploads/.*\.php$ {
deny all;
}

location ~ \.php/ {
if ($request_uri ~ ^(.+\.php)(/.+?)($|\?)) { }
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_NAME $1;
fastcgi_param PATH_INFO $2;
fastcgi_param SCRIPT_FILENAME $document_root$1;
}

location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

location ~ /\.ht {
deny all;
}
}

3、最后,重启nginx,./nginx -s reload

  • 本文作者: 好好学习的郝
  • 原文链接: https://www.voidking.com/dev-vkphp-7/
  • 版权声明: 本文采用 BY-NC-SA 许可协议,转载请注明出处!源站会即时更新知识点并修正错误,欢迎访问~
  • 微信公众号同步更新,欢迎关注~