Nginx proxy_pass 到 ThinkPHP 5.0 项目的配置应该怎么写?

2018-07-05 17:12:47 +08:00
 Loyalsoldier

ThinkPHP 5.0 项目 A 路径为:/www/a,运行目录为 /www/a/public

ThinkPHP 5.0 项目 B 路径为:/www/b,运行目录为 /www/b/public

为了能使两个项目使用同一个域名,项目 A 的 Nginx 配置内,将 请求 /go/ URL 的所有请求通过 proxy_pass 转发给项目 B 处理,也就是希望访问 http://example.com/go/ 时,实际访问的是项目 B 的运行目录 /www/b/public

PHP-FPM 开了 pathinfo,而且项目 A 已经成功跑起来了,而项目 B 所有响应都是 404 并有文字输出 File not found(不是浏览器默认的 404 页面)。

ThinkPHP 5.0 默认的 URL 规则是这样的:

访问 http://example.com/test,相当于访问 http://example.com/index.php/test

麻烦 V 有帮忙看看哪里有问题?

项目 A Nginx 配置如下:

server {
  listen         80;
  server_name    example.com;
  root           /www/a/public;
  
  location ^~ /go/ {
    proxy_pass   http://localhost:81;
  }

  location / {
    index                index.php;
    try_files            $uri $uri/ /index.php$request_uri;
  }

  location ~* \.php {
    fastcgi_pass               127.0.0.1:9000;
    fastcgi_index              index.php;
    fastcgi_split_path_info    ^(.+\.php)(.*)$;
    fastcgi_param              SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param              PATH_INFO       $fastcgi_path_info;
    include                    fastcgi_params;
  }

  location ~* \.(jpg|jpeg|bmp|png|webp|gif|svg|ico|js|css|eot|woff|woff2|ttf|otf) {
    add_header             Cache-Control max-age=2592000;
    etag                   on;

    access_log             off;
    log_not_found          off;
  }
}

项目 A 按照上面的 Nginx 配置,已经成功运行起来。

项目 B Nginx 配置如下:

server {
  listen         81;
  server_name    localhost;
  root           /www/b/public;

  location / {
    index                index.php;
    try_files            $uri $uri/ /index.php$request_uri;
  }

  location ~* \.php {
    fastcgi_pass               127.0.0.1:9000;
    fastcgi_index              index.php;
    fastcgi_split_path_info    ^(.+\.php)(.*)$;
    fastcgi_param              SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param              PATH_INFO       $fastcgi_path_info;
    include                    fastcgi_params;
  }

  location ~* \.(jpg|jpeg|bmp|png|webp|gif|svg|ico|js|css|eot|woff|woff2|ttf|otf) {
    add_header             Cache-Control max-age=2592000;
    etag                   on;

    access_log             off;
    log_not_found          off;
  }
}
1829 次点击
所在节点    NGINX
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/468420

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX