1. 程式人生 > >nginx 配置支援pathinfo和url重寫 thinkphp5

nginx 配置支援pathinfo和url重寫 thinkphp5

apache是預設支援pathinfo,而nginx本身是不支援pathinfo 
何為pathinfo?請看這條url127.0.0.1/test.php/a/b/c,/a/b/c即為pathinfo

配置支援pathinfo

location ~ \.php(.*)$ {
		include snippets/fastcgi-php.conf;
	
		## With php-fpm (or other unix sockets):
		fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
		## With php-cgi (or other tcp sockets):
		#fastcgi_pass 127.0.0.1:9000;
        
        fastcgi_param PATH_INFO $1;//新增這一句
}

以上便配置成功,測試   127.0.0.1/xxx/public/index.php/index

配置url重寫

當你配置好了pathinfo時,如果此時你執行thinkphp5,需要這樣子訪問,127.0.0.1/xxx/public/index.php/xxx/xx,每一次訪問都需要攜帶index.php,這時可通過url的重寫將index.php隱藏起來

在location / { …. }當中新增下列指令

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

/(.*)/public/index.php,index.php前面有幾級的目錄就需要新增幾級的,這個是正則表示式,不理解的請自行百度