当前位置: 移动技术网 > IT编程>开发语言>PHP > 关于THINKPHP 的 NGINX 配置,那些年才过的坑

关于THINKPHP 的 NGINX 配置,那些年才过的坑

2020年11月12日  | 移动技术网IT编程  | 我要评论
THINKPHP 的 NGINX 配置踩坑今天在用一个以 tp 为基础的快速开发框架时遇到一些问题:nginx 报错截图为了方便说明进行手动换行// 处理时重写或内部重定向循环2019/11/11 11:16:06 [error] 15164#15432: *1 rewrite or internal redirection cycle while processing "/index.php/index.php/index.php/index.php/index.php/index.

THINKPHP 的 NGINX 配置踩坑

今天在用一个以 tp 为基础的快速开发框架时遇到一些问题:

nginx 报错截图

在这里插入图片描述

为了方便说明进行手动换行

// 处理时重写或内部重定向循环
2019/11/11 11:16:06 [error] 15164#15432: *1 rewrite or internal redirection cycle while processing 
    "/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index/user/", 
    client: 127.0.0.1, 
    server: xxxxx, 
    request: "GET /index/user/ HTTP/1.1", 
    host: "xxxxx", 
    referrer: "xxxxx"

错误配置

参考 larvael 配置

server {
    .
    .
    .

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    .
    .
    .
    location ~ \.php$ {
        fastcgi_pass127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }
    .
    .
    .

}

发现所有路径都一样,都是首页效果

初步判断 nginx 重写规则有问题

# 路径 / 开头之后都走这个匹配
# 如 /index /index/user 
location / {
    # $uri 本地有就返回,或者$uri/ 本地有目录就返回,或者走后面的重写
    # 本地是指在网站根目录下,如 当 $uri=index 就是看根目录下面有 index 文件或者 index/ 目录
    try_files $uri $uri/ /index.php?$query_string;
}

开始报错

解决问题

网上查询后 tp5 的配置应为

location / {
    try_files $uri $uri/ /index.php$uri?$query_string;
}

改后,发现问题没解决;对比配置发现

  # location ~ \.php$ 改成  location ~ \.php(.*)$
    location ~ \.php(.*)$ {
        fastcgi_pass   127.0.0.1:9000;
        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;
    }

解决,完整配置

server {
    listen       80;
    server_name  xxxxxxx ;
    root  www;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    charset utf-8;

    index index.html index.htm index.php;
    location / {
        try_files $uri $uri/ /index.php$uri?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;


    location ~ \.php(.*)$ {
        fastcgi_pass   127.0.0.1:9000;
        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;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

一些说明

在伪静态配置用 try_files 而非 rewrite,是因为 rewrite 会对全部匹配(这里是 /)请求都做一次匹配,如:host.com/a.png,这会会浪费一些资源(服务器的算力等);同时也参考了 laravel 的官方配置加入一些安全设置。

如果在用到上线项目时最好加入静态资源缓存配置。

location ~* ^.+\.(css|js|ico|gif|jpg|jpeg|png)$ {
    expires 30d;
}

修复

2020-08-20 发现 get 没有参数,原因是没有 QUERY_STRING 为空,解决:

location / {
    # 带上 query_string
    try_files $uri $uri/ /index.php$uri?$query_string;
}

点关注,不迷路

好了各位,以上就是这篇文章的全部内容了,能看到这里的人呀,都是人才。之前说过,PHP方面的技术点很多,也是因为太多了,实在是写不过来,写过来了大家也不会看的太多,所以我这里把它整理成了PDF和文档,如果有需要的可以

点击进入暗号: PHP+「平台」

在这里插入图片描述

在这里插入图片描述


更多学习内容可以访问【对标大厂】精品PHP架构师教程目录大全,只要你能看完保证薪资上升一个台阶(持续更新)

以上内容希望帮助到大家,很多PHPer在进阶的时候总会遇到一些问题和瓶颈,业务代码写多了没有方向感,不知道该从那里入手去提升,对此我整理了一些资料,包括但不限于:分布式架构、高可扩展、高性能、高并发、服务器性能调优、TP6,laravel,YII2,Redis,Swoole、Swoft、Kafka、Mysql优化、shell脚本、Docker、微服务、Nginx等多个知识点高级进阶干货需要的可以免费分享给大家,需要的可以加入我的 PHP技术交流群

本文地址:https://blog.csdn.net/weixin_49163826/article/details/109648779

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网