1、打开php.ini
通常该文件在 /usr/local/php/etc/php.ini vi /usr/local/php/etc/php.ini
找到 cgi.fix_pathinfo,默认为0,修改为1,保存退出;
2、打开nginx.conf配置文件
通常该文件在 /usr/local/nginx/conf/nginx.conf vi /usr/local/nginx/conf/nginx.conf
(1)找到 include enable-php.conf;注释掉,在下面一行添加include enable-php-pathinfo.conf;
(2)添加以下代码,并保存退出
#Rewrite模式 location / { index index.htm index.html index.php; if (!-e $request_filename) { rewrite ^/(.*)$ /index.php/$1 last; break; } } #pathinfo模式 location ~ .php/?.*$ { root /home/wwwroot/default; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; 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; }
(3)重启nginx service nginx restart
这样就可以用pathinfo模式访问了
提示:项目中必须设置’URL_MODEL’ => 1才能用pathinfo模式访问
当设置’URL_MODEL’ => 2时,也可以用Rewrite模式访问
Nginx设置虚拟主机,添加多站点
nginx.conf中添加include vhost/*.conf;
添加一个站点信息:
在vhost中添加一个 xxx.conf的文件 大致内容如下:
server { listen 80; server_name my.nginx1.com; root /home/wwwroot/default/nginx1; index index.html index.php index.htm; error_page 400 /errpage/400.html; error_page 403 /errpage/403.html; error_page 404 /errpage/404.html; include enable-php-pathinfo.conf; location ~ .php$ { try_files $uri =404; fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; include fastcgi.conf; } }