Google Cloud App Engine app.yaml php72 路由问题

Posted

技术标签:

【中文标题】Google Cloud App Engine app.yaml php72 路由问题【英文标题】:Google Cloud App Engine app.yaml php72 issue with routing 【发布时间】:2019-06-13 00:47:54 【问题描述】:

我有一个核心 php 网站,这是我当前的 app.yaml 配置

runtime: php72

handlers:
# Serve a directory as a static resource.
- url: /assets
  static_dir: assets

- url: /css
  static_dir: css

- url: /js
  static_dir: js

- url: /ckeditor
  static_dir: ckeditor

# Serve images as static resources.
- url: /(.+\.(gif|png|jpg))$
  static_files: \1
  upload: .+\.(gif|png|jpg)$

# add all script entries like this
- url: /login.php
  script: auto

# Serve your app through a front controller at index.php or public/index.php.
- url: .*
  script: auto

此配置的问题是它不断重定向 (302) 到登录页面一次又一次.. 并以错误 Too Many Redirects 告终。

我错过了什么?

GAE 日志:

2019-01-18 17:10:07 default[20190118t223420]  "GET /login.php HTTP/1.1" 302
2019-01-18 17:10:10 default[20190118t223420]  "GET /login.php HTTP/1.1" 302
2019-01-18 17:10:13 default[20190118t223420]  "GET /login.php HTTP/1.1" 302
2019-01-18 17:10:16 default[20190118t223420]  "GET /login.php HTTP/1.1" 302
2019-01-18 17:10:19 default[20190118t223420]  "GET /login.php HTTP/1.1" 302
2019-01-18 17:10:22 default[20190118t223420]  "GET /login.php HTTP/1.1" 302
2019-01-18 17:10:25 default[20190118t223420]  "GET /login.php HTTP/1.1" 302
2019-01-18 17:32:26 default[20190118t225141]  "GET / HTTP/1.1" 302
2019-01-18 17:32:26 default[20190118t225141]  nginx: [warn] the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /tmp/google-config/nginx.conf:3
2019-01-18 17:32:30 default[20190118t225141]  "GET /login.php HTTP/1.1" 302
2019-01-18 17:32:33 default[20190118t225141]  "GET /login.php HTTP/1.1" 302
2019-01-18 17:54:59 default[20190118t230733]  "GET / HTTP/1.1" 302
2019-01-18 17:55:00 default[20190118t230733]  nginx: [warn] the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /tmp/google-config/nginx.conf:3
2019-01-18 17:55:02 default[20190118t230733]  "GET /login.php HTTP/1.1" 302
2019-01-18 17:55:05 default[20190118t230733]  "GET /login.php HTTP/1.1" 302
2019-01-18 17:55:07 default[20190118t230733]  "GET /login.php HTTP/1.1" 302
2019-01-18 17:55:09 default[20190118t230733]  "GET /login.php HTTP/1.1" 302
2019-01-18 17:55:11 default[20190118t230733]  "GET /login.php HTTP/1.1" 302
2019-01-18 18:17:29 default[20190118t230733]  "GET / HTTP/1.1" 302
2019-01-18 18:17:32 default[20190118t230733]  "GET /login.php HTTP/1.1" 302
2019-01-18 18:17:35 default[20190118t230733]  "GET /login.php HTTP/1.1" 302
2019-01-18 18:17:37 default[20190118t230733]  "GET /login.php HTTP/1.1" 302
2019-01-18 18:17:40 default[20190118t230733]  "GET /login.php HTTP/1.1" 302

【问题讨论】:

您能否显示多个连续此类请求的 GAE 日志,最好包括启动此类循环序列的原始请求? 我已经添加了我能收集到的东西.. @DanCornilescu 您的app.yaml 似乎没问题。你能发布你的index.php吗? 我的猜测是,您为/login.php 拥有的script: auto 实际上是在访问其他脚本,而不是您的login.php,因此再次被重定向到/login.php。但我不确定 - 不是 php 用户。我在代码 sn-p 中看到cloud.google.com/appengine/docs/standard/php7/…:# Defaults to "serve index.php" and "serve public/index.php". @GAEfan 我删除了除运行时之外的所有规则.. 它仍然是 302.... 【参考方案1】:

原来php72不支持直接在标准环境下执行脚本。一切都需要通过自定义的 FrontController。所以我创建了一个 FrontController,它基本上可以处理所有路由。下面是我放在 index.php 中的代码。它将处理所有指定的路由。

switch (@parse_url($_SERVER['REQUEST_URI'])['path']) 
    case '/':
        require 'login.php';
        break;
    case '/product.php';
        require 'product.php';
        break;
    default:
        break;

您可以在此处找到自定义 FrontController 和不同框架的 php72 示例。 https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/appengine

感谢大家的努力。希望这可以帮助像我这样迷失的灵魂。

【讨论】:

如果有人来这里,需要更多的背景信息,我有一个要点:gist.github.com/Mcgurk-Adam/c66b5c473e71d6e724b7e8e4329757f3【参考方案2】:

动态:

switch (@parse_url($_SERVER['REQUEST_URI'])['path']) 

    case @parse_url($_SERVER['REQUEST_URI'])['path']:
    $name_url_path = substr(@parse_url($_SERVER['REQUEST_URI'])['path'],1);
    $name_url_path = empty($name_url_path)?'home_page.php':$name_url_path;
    $name_url_path = ($name_url_path == 'index.php')?'home_page.php':$name_url_path;
    require $name_url_path;
    break;
    default:
    break;

【讨论】:

以上是关于Google Cloud App Engine app.yaml php72 路由问题的主要内容,如果未能解决你的问题,请参考以下文章

是否可以将 Google App Engine 与 Google Cloud *** 一起使用?

Google App Engine - 大查询 - Python 找不到库 google.cloud

Google App Engine 上的 Production App 突然无法访问 Google Cloud Storage

使用 Google Cloud Load Balancer 迁移 App Engine 会导致约 1 小时的停机时间

Google Cloud Platform:Cloud Functions 与 App Engine

使用 Google App Engine 签名的 Google Cloud Storage 网址