CentOS7 Nginx ThinkPHP5.1 配置

Posted ZY_Liang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CentOS7 Nginx ThinkPHP5.1 配置相关的知识,希望对你有一定的参考价值。

最近在研究Thinkphp5.1框架,服务器是CentOS7,在配置到nginx的时候出了点小问题,因为测试练习的服务器上还有其他的程序,所以路径访问过程中需要重新配置。

1.tp5.1框架URL访问pathinfo模式,在访问中省略 index.php

location / {
        
            if (!-e $request_filename){
                rewrite ^/(.*)$ /index.php/$1 last;    
            }
        }

这里有个小问题,就是 if 后面要有个空格,之前没有留空格,Nginx重启时总是报错,后来发现是这个原因 !-_-

2.fastcgi 的配置

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

最后附上完整的配置。thinkphp5.1 入口目录在项目目录的public下,所以如果你想省去url路径中的/thinkphp51/public/的话,就将 root 写成下面配置中的样式。

还有server_name 这里不要设置为空。

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  localhost;      
        root         /usr/share/nginx/html/thinkphp51/public;
        include         /etc/nginx/default.d/*.conf;
        index         index.php index.html index.htm;
        location / {
        
            if (!-e $request_filename){
                rewrite ^/(.*)$ /index.php/$1 last;    
            }
        }
    


        error_page 404 /404.html;
            location = /40x.html {
              root /usr/share/nginx/html;
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
      location ~ .php(.*)$ {
    
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info ^((?U).+.php)(/?.+)$;
            fastcgi_param PATH_INFO $fastcgi_path_info;
            fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }

    }

 

以上是关于CentOS7 Nginx ThinkPHP5.1 配置的主要内容,如果未能解决你的问题,请参考以下文章

thinkphp5.1在nginx环境路由不论怎么修改路由,都只能指向index控制器index方法

nginx+vue+thinkphp5.1部署,解决前端刷新404,以及前端404解决后,后台又404的问题

thinkphp5 - 1 - 初始安装

thinkphp5.1-jwt的安装与使用

服务器VMware Workstation Pro虚拟机搭建本地服务器CentOs7和宝塔面板(保姆式教程)

centos7 安装Nginx