登录  /  注册
首页 > php框架 > ThinkPHP > 正文

ThinkPHP 的 nginx 配置踩坑

藏色散人
发布: 2019-11-13 14:03:34
转载
3454人浏览过

thinkphp 的 nginx 配置踩坑

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

nginx 报错截图

f2017d20e32b8c28eb4e4203551ca14.png

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

// 处理时重写或内部重定向循环
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/index.html", 
    client: 127.0.0.1, 
    server: xxxxx, 
    request: "GET /index/user/index.html 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;
    }
登录后复制

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

  # 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;
    }
    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;
    }
}
登录后复制

推荐学习:thinkphp教程

以上就是ThinkPHP 的 nginx 配置踩坑的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
来源:learnku网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号