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;
        }

    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM