Nginx配置部署ThinkPHP 逐行解读
2022-07-28 14:14:58
135
{{single.collect_count}}

测试环境:

  • thinkphp5.1
  • nginx/1.14.0(Ubuntu)

文档说明: 此文档只说明如何在Nginx里配置TP重定向到index.php部分的配置。不包括Nginx与Fastcgi(例php-fpm)的配置。点此查看Nginx与Fastcgi配置:Nginx配置php-fpm 逐行解读

思路:

  • 将.php的所有请求配置到thinkphp/public/index.php上。

我的配置示例

涉及到项目配置的主要在location上下文,例:

root /var/www/html;location / {if (!-e $request_filename) {rewrite ^(.*)index.php(.*)$ $1index.php?s=$2 last;}}

逐行解读:

示例url:http://127.0.0.1/biosmanage/public/index.php/index/login/index

  1. root /var/www/html
    Nginx配置的根目录

  2. location / {
    匹配所有请求,但是正则和最长字符串会优先匹配

  3. if (!-e $request_filename) {
    作用:仅对不存在的路径请求进行处理

    • !-e:检测是否存在一个文件,一个目录或一个符号链接
    • $request_filename:
      This variable is equal to path to the file for the current request, formed from directives root or alias and URI request;
      此变量等于当前请求的文件路径,由根或别名和URI请求指令构成;
  4. rewrite ^(.*)index.php(.*)$ $1index.php?s=$2 last;

    • rewrite:HttpRewrite模块,重定向
    • ^(.*)index.php(.*)$:正则表达式,对示例url中的</biosmanage/public/index.php/index/login/index>部分进行匹配
    • $1index.php?s=$2:重写url,$1、$2为正则匹配。’?s='中的’s’是ThinkPHP配置文件app.php中的’var_pathinfo’配置参数在这里插入图片描述
    • last:完成重写指令的处理,然后搜索相应的 URI 和位置。(可以更换为redirect,就可以在浏览器url看到重写后的url,方便调试)
  5. }

  6. }

注:修改root为/var/www/html/biosmanage/public,可以用http://127.0.0.1/index.php/index/login/index访问
参考链接:
HttpRewrite模块
nginx详解之语法规则
Nginx Rewrite功能的使用及心得

有钱的打个赏,没钱的尽量打个赏。。。吧

回帖
全部回帖({{commentCount}})
{{item.user.nickname}} {{item.user.group_title}} {{item.friend_time}}
{{item.content}}
{{item.comment_content_show ? '取消' : '回复'}} 删除
回帖
{{reply.user.nickname}} {{reply.user.group_title}} {{reply.friend_time}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}