天天看點

ThinkPHP->pathinfo模式(Linux下nginx的配置說明)

小夥伴在使用ThinkPHP搭建自己或者公司項目的時候,url模式設定成為相容模式,也就是URL_MODEL的值為3的時候是不是也遇到過Lnmp的環境不支援pathinfo模式,當然了PHP+Nginx+Linux+Mysql的小夥伴呢,就不用看了!

下面就教大家怎麼講我們的各種雲伺服器下lnmp的環境支援pathinfo,說到這裡是不是有的小夥伴可能不太明白pathinfo模式是什麼,下面小編就先給大家上個個小例子:

www.xxx.com/index.php/Admin/Manager/Login

這樣的url形式就是pathinfo的模式,這樣的模式更加利于百度小蜘蛛的識别,也就是更加優于SEO。

下面就是重點步驟了,小夥伴們要注意了:

實際上差不多就是把nginx.conf拆解下來

1,把原有的nginx.conf檔案備份(就是修改個名字),然後用下面的nginx.conf檔案

user www www;

worker_processes 4;

error_log /home/wwwlogs/nginx_error.log crit;

pid /usr/local/nginx/logs/nginx.pid;

#Specifies the value for maximum file descriptors that can beopened by this process.

worker_rlimit_nofile 51200;

events

{

use epoll;

worker_connections 51200;

}

http {

include /usr/local/nginx/conf/mime.types;

default_type text/plain;

log_format main '$remote_addr - $remote_user [$time_local]"$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"';

access_log off;

sendfile on;

#tcp_nopush on;

#keepalive_timeout 0;

keepalive_timeout 65;

gzip on;

client_max_body_size 20m;

# Load config files from the /etc/nginx/conf.d directory

# The default server is in conf.d/default.conf

include /usr/local/nginx/conf/conf.d/*.conf;

include /usr/local/nginx/conf/vhost/*.conf;

2,配置pathinfo在/usr/local/nginx/conf下建立pathinfo.conf(如果已經存在請先備份然後修改)

fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;

fastcgi_pass unix:/tmp/php-cgi.sock;

fastcgi_param PATH_INFO $fastcgi_path_info;

fastcgi_param PATH_TRANSLATED$document_root$fastcgi_path_info;

fastcgi_param SCRIPT_NAME $fastcgi_script_name;

fastcgi_param SCRIPT_FILENAME$document_root$fastcgi_script_name;

fastcgi_param REQUEST_METHOD $request_method;

fastcgi_param QUERY_STRING $query_string;

fastcgi_param CONTENT_TYPE $content_type;

fastcgi_param CONTENT_LENGTH $content_length;

fastcgi_param REQUEST_URI $request_uri;

fastcgi_param DOCUMENT_URI $document_uri;

fastcgi_param DOCUMENT_ROOT $document_root;

fastcgi_param SERVER_PROTOCOL $server_protocol;

#fastcgi_param HTTPS $https if_not_empty;

fastcgi_param GATEWAY_INTERFACE CGI/1.1;

fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;

fastcgi_param REMOTE_ADDR $remote_addr;

fastcgi_param REMOTE_PORT $remote_port;

fastcgi_param SERVER_ADDR $server_addr;

fastcgi_param SERVER_PORT $server_port;

fastcgi_param SERVER_NAME $server_name;

fastcgi_param REDIRECT_STATUS 200;

5,建立重定向 在/usr/local/nginx/conf下建立drect.conf

index index.php;

location ~ \.php$ {

include pathinfo.conf;

break;

autoindex on;

3,在/usr/local/nginx/conf裡建vhost和conf.d兩個檔案夾子(mkdir vhost)

在conf.d裡建立default.conf

server {

listen 80;# default_server;

server_name _;

charset utf-8;

#access_log logs/host.access.log main;

location / {

root /usr/share/nginx/html;

index index.html index.htm;

error_page 404 /404.html;

location = /404.html {

error_page 500 502 503 504 /50x.html;

location = /50x.html {

4,在vhost檔案夾裡建立站點配置資訊(要以.conf結尾,每次添加新的站點兒時隻需修改這裡就可以)

listen 80 default_server;

server_name nahan.cn;

//這個位址要和你的通路位址相對應

root /home/wwwroot/nahan.cn/;

if ($host = nahan.cn) {

rewrite ^/(.*)$ http://www.nahan.cn/$1 permanent;

try_files $uri $uri/ /index.php$uri;

location ~ \.php {

5,進入 /usr/local/php/etc/php.ini 修改cgi.fix_pathinfo=1

新安裝的lnmp如果php版本較低的話需要更新到5.4.27

6,所有檔案修改完畢後請重新開機所有服務。指令:/root/lnmp restart 新版的重新開機指令為:lnmprestart

小夥伴按照上面的步驟一步一步的配置就可以了,希望這篇文章能幫助那些還在苦于linux下配置pathinfo的小夥伴

更多的精彩内容:

小夥伴們可以加群:

思夢PHP官方交流1群 466388300 思夢PHP官方交流2群 527490769

作者QQ:476319748

以上聯系方式備注:微信公衆平台思夢PHP

我們會定期給大家推送項目實戰中非常有用的小技巧以及好的思維和小功能的案例!微信搜尋思夢PHP就可以找到我們了