thinkphp5配置nginx访问

作者:谢高升 发布:2017-11-28 浏览:2537次

thinkphp采用pathinfo模式;不像laravel和yii配置上去直接就可以干;

测试了两个不同方式的nginx安装 yum 和lnmp一键安装包;

一键安装包里面有对pathinfo的支持 

image.png

下面是配置

一键安装包安装的lnmp centos7

server
    {
        listen 8080 default_server;
        #listen [::]:80 default_server ipv6only=on;
        server_name _;
        index index.html index.htm index.php;
        root  /var/www/html/ibjson;

        #error_page   404   /404.html;

        # Deny access to PHP files in specific directory
        #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

        include enable-php.conf;

        location /nginx_status
        {
            stub_status on;
            access_log   off;
        }

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }
		location / {
            if (!-e $request_filename){
                rewrite ^/(.*)$ /index.php?s=/$1 last;
            }
		}

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

        location ~ /.well-known {
            allow all;
        }

        location ~ /\.
        {
            deny all;
        }

        access_log  /home/wwwlogs/access.log;
    }


nginx端口转发的配置如下

server{
	listen      8089;
	server_name _;
	root           /var/www/html/tp5/public;
	index index.php index.html index.htm;

	location ~ \.php/?.* {    

		fastcgi_pass   127.0.0.1:9000;  

		fastcgi_index  index.php;  

		fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;  

		include        fastcgi_params;  

		#定义变量 $path_info ,用于存放pathinfo信息  

		set $path_info "";  

		#定义变量 $real_script_name,用于存放真实地址  

		set $real_script_name $fastcgi_script_name;  

		#如果地址与引号内的正则表达式匹配  

		if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {  

			#将文件地址赋值给变量 $real_script_name  

			set $real_script_name $1;  

			#将文件地址后的参数赋值给变量 $path_info  

			set $path_info $2;  

		}  

		#配置fastcgi的一些参数  

		fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;  

		fastcgi_param SCRIPT_NAME $real_script_name;  

		fastcgi_param PATH_INFO $path_info;  

	}  
	location / {

		if (!-e $request_filename){
		rewrite ^/(.*)$ /index.php/$1 last;
		}
	#try_files $uri $uri/ /index.php?$query_string;
	}
}


location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9001;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }