V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
Loyalsoldier
V2EX  ›  NGINX

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

  •  
  •   Loyalsoldier · 2018-07-05 17:12:47 +08:00 · 1808 次点击
    这是一个创建于 2093 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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;
      }
    }
    
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5399 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 06:57 · PVG 14:57 · LAX 23:57 · JFK 02:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.