nginx 配置 php / pathinfo 模式 / php 多版本服務

結構

/home/wwwroot/demo/public/index.php
/index.php/index/index?p=1&ps=2

SCRIPT_FILENAME: /home/wwwroot/demo/public/index.php
PHP_SELF: /index.php/index/index
SCRIPT_NAME: /index.php
PATHI_INFO: /index/index
QUERY_STRING: p=1&ps=2
URI: /index.php/index/index?p=1&ps=2

fastcgi_split_path_info 指令

該指令會重寫 nginx 的 $fastcgi_script_name / $fastcgi_path_info 將請求路徑賦值到 $fastcgi_path_info,也就是咱們想要的 path_infophp

index.php/news/1
$fastcgi_script_name: index.php
$fastcgi_path_info: news/1

1、通用配置

如下配置文件同 nginx.conf 同級css

pathinfo.conf
解析並設定 PATH_INFOhtml

fastcgi_split_path_info ^(.+?\.php)(/.*)$;
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

fastcgi.conf
環境變量,open_basedir 是個須要注意的環境變量,會限制 php 載入文件的路徑。不少應用入口文件處於 siteApp/public/index.php 下的框架可能會受到限制。nginx

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
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  REQUEST_SCHEME     $scheme;
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;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;
fastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root/:/tmp/:/proc/";

enable-php.conf
只啓用 php 不開啓 pathinfoweb

location ~ [^/]\.php(/|$)
{
    # try_files $uri =404;
    # fastcgi_pass  unix:/tmp/php-cgi.sock;
    fastcgi_pass  127.0.0.0:9000;
    fastcgi_index index.php;
    include fastcgi.conf;
}

enable-php-pathinfo.conf
啓用 php 並開啓 pathinfothinkphp

location ~ [^/]\.php(/|$)
{
    # try_files $uri =404; pathinfo 下絕對是 404
    # fastcgi_pass  unix:/tmp/php-cgi.sock;
    fastcgi_pass  127.0.0.0:9000;
    fastcgi_index index.php;
    include pathinfo.conf;
    include fastcgi.conf;
}

2、站點配置示例

通配 / 根請求,使用 try_files 指令來進行 static / dynamic request 分離和 index.php 的隱藏。shell

若是你不想讓別人知道你使用的服務端語言是什麼,很簡單,把 index.php 改成 youwantkonwlanginmyserverhahahnoway.php 後將 try_files 指令更新爲:php7

location / {
    # dispatch static/dynamic request and hidde index.php
    try_files $uri $uri/ /youwantkonwlanginmyserverhahahnoway.php?$query_string =404;
}

站點配置

vhosts/site.conf框架

server {
    listen 80 default_server reuseport;
    #listen [::]:80 default_server ipv6only=on;
    server_name _;
    index index.html index.htm index.php;
    root  /home/wwwroot/default;
    
    location / {
        # thinkphp 兼容模式
        # last 使用重寫後的 url 再一次進入 location 解析
        # break 直接返回重寫後的 url 對應的資源
        #if (!-e $request_filename){
        #    rewrite  ^(.*)$  /index.php?s=$1  last;
        #}
        
        # dispatch static/dynamic request and hidde index.php
        # 隱藏 index.php /
        try_files $uri $uri/ /index.php$is_args$query_string =404;
        # 隱藏 index.php 並開啓 pathinfo
        try_files $uri $uri/ /index.php$uri?$query_string;
    }
    
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
        expires 30d;
    }

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

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

    location ~ /\. {
        deny all;
    }
    
    # 防止上傳 web-shell your documentRoot's directory
    location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }
    
    # 選擇要啓用的模式
    # include enable-php.conf
    include enable-php-pathinfo.conf
    
    access_log  /home/wwwlogs/access.log;
    error_log   /home/wwwlogs/error.log;
}

3、php 多版本服務

若是你能本地有多個站點,須要使用不一樣的 php 版本,則能夠爲不一樣版本的 php-fpm 服務建立不一樣的監聽端口,再分別建立相應的 fpm 服務轉發配置文件便可。php-fpm

enable-php74-pathinfo.conf

location ~ [^/]\.php(/|$)
{
    # try_files $uri =404;
    fastcgi_pass  127.0.0.0:9074;
    fastcgi_index index.php;
    include pathinfo.conf;
    include fastcgi.conf;
}

enable-php56-pathinfo.conf

location ~ [^/]\.php(/|$)
{
    # try_files $uri =404;
    fastcgi_pass  127.0.0.0:9056;
    fastcgi_index index.php;
    include pathinfo.conf;
    include fastcgi.conf;
}

enable-php53-pathinfo.conf

location ~ [^/]\.php(/|$)
{
    # try_files $uri =404;
    fastcgi_pass  127.0.0.0:9053;
    fastcgi_index index.php;
    include pathinfo.conf;
    include fastcgi.conf;
}
相關文章
相關標籤/搜索