【问题标题】:NGINX config for php backend and JS frontend用于 php 后端和 JS 前端的 NGINX 配置
【发布时间】:2019-09-17 07:56:33
【问题描述】:

我正在尝试在 / 下为我的前端应用程序提供服务,但是对 /oauth2 的请求会传递给 php 后端。这是我最新的 nginx 配置尝试:

upstream dockerphp {
    server backendphp:9000;
}

server {
    listen       80;
    server_name  localhost;
    index index.html;
    root   /application/frontend/build;

    location /oauth2 {
        root   /application/public;
        index index.php;
        try_files $uri $uri/ /index.php$is_args$args;
        #try_files /index.php$is_args$args =404;

        location ~ \.php$ {
            include /etc/nginx/fastcgi_params;

            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_path_info;
            fastcgi_pass  dockerphp;
            fastcgi_index index.php;
        }
    }

    location / {
        try_files $uri $uri/ /index.html;
    }
}

我已经尝试了几乎所有我能想到的配置组合,但无法让它发挥作用。大多数时候我都以 404 结束。

我的 nginx 和 php docker 容器都挂载了相同的 /application 目录。

使用上面的配置,任何对 /oauth2/blah 的请求都会被底部的位置块接收,因此会返回到我的前端。这可能是我最大的问题 - 我认为 /oauth2 位置块更“具体”,那为什么不是“获胜”?

我尝试了注释掉的 try_files 行(以查看 index.php 作为“后备”值是否对特异性有影响),并且 nginx 刚刚开始下载 index.php 文件而不是传递请求。帮忙?

【问题讨论】:

    标签: php nginx nginx-location


    【解决方案1】:

    这是我使用的方法:

    1. 尝试先提供 js/静态页面
    2. 如果 1.) 失败,则传递给 PHP 后端
    3. 定义处理.php的位置
        upstream dockerphp {
            server backendphp:9000;
        }
    
    
        server {
            listen           80;
            server_name      localhost;
            index            index.html;
            root             /application/frontend/build;
    
            location / {
                try_files    $uri $uri/ @php;
            }
    
            location @php {
                root         /application/public;
                index        index.php;
                try_files    $uri $document_root/index.php?$query_string; 
                # $document_root/index.php is the important part due to how root and alias directives work
            }
    
            location ~ \.php$ {
                include      /etc/nginx/fastcgi_params;
    
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param PATH_INFO $fastcgi_path_info;
                fastcgi_pass  dockerphp;
                fastcgi_index index.php;
            }
        }
    

    【讨论】:

      【解决方案2】:

      location /oauth2 仅在您尝试的 URL 正好是 website.com/oauth2 时获胜。添加^~,该路由将赢得所有以/oauth2开头的URL,如下所示:

      location ^~ /oauth2 {
      

      【讨论】:

      • 不幸的是,情况并非如此,位置 /oauth2 与您发布的代码相同。 location = /oauth2 将是完全匹配的等价物。
      【解决方案3】:

      作为参考,我最终找到了一个简单的工作解决方案(如下)。

      upstream dockerphp {
          server backendphp:9000;
      }
      
      server {
          listen       80;
          server_name  localhost;
          index        index.html;
          root         /application/frontend/build;
      
          location / {
              try_files $uri $uri/ /index.html;
          }
      
          location /oauth2 {
              try_files $uri $uri/ @php;
          }
      
          location @php {
              include                  /etc/nginx/fastcgi_params;
              fastcgi_pass             dockerphp;
              fastcgi_param            SCRIPT_FILENAME /application/public/index.php;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2021-05-19
        • 2022-07-17
        • 2019-05-10
        • 1970-01-01
        • 2017-11-22
        • 2019-10-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多