【问题标题】:Angular app in google app engine redirection issue谷歌应用引擎重定向问题中的角度应用
【发布时间】:2019-08-10 18:46:40
【问题描述】:

我正在尝试在应用引擎中部署 Angular 7 应用 使用 ng deploy --prod 创建部署。现在使用 app.yaml 部署生成的dist。 app.yaml 的内容

runtime: python27
api_version: 1
threadsafe: yes

handlers:

  - url: /
    static_files: dist/index.html
    upload: dist/index.html
  - url: /
    static_dir: dist
  - url: /\w{3,}-?[^.\s]\w*
    static_files: dist/index.html
    upload: dist/index.html

如果 URL 未更改,即 domain.com,则此方法非常有效,但当 url 更改为 domain.com/logindomain.com/profile 时,它会生成 404 错误

Error: Not Found
The requested URL /admin-dashboard was not found on this server.

为了解决重定向问题,我在 app.yaml 中添加了 url 模式以匹配任何 url 并转发到 dist/index.html

- url: /\w{3,}-?[^.\s]\w*
    static_files: dist/index.html
    upload: dist/index.html

但这无济于事。我完全被困住了。请任何指针 @Christopher Peisert 非常感谢您的指点。这是适用于所有类型 URL 模式的最终 app.yaml

runtime: python27
api_version: 1
threadsafe: yes

handlers:
- url: /
  static_files: dist/index.html
  upload: dist/index.html
- url: /\w+$
  static_files: dist/index.html
  upload: dist/index.html
- url: /(\w+-\w+)$
  static_files: dist/index.html
  upload: dist/index.html  
- url: /(.*)$
  static_files: dist/\1
  upload: dist/(.*)

【问题讨论】:

    标签: google-app-engine


    【解决方案1】:

    如果您使用的是 PathLocationStrategy(html5 历史记录),您需要有一个处理程序来捕获这些动态路线

    runtime: python37
    
    handlers:
    # index files
    - url: /([^.]+)/?$  # urls with no dot in them
      static_files: dist/index.html
      upload: dist/index.html
    
    # site root
    - url: /
      static_files: dist/index.html
      upload: dist/index.html
    
    # everything else
    - url: /(.*)
      static_files: dist/\1
      upload: dist/(.*)
    

    【讨论】:

      【解决方案2】:

      尝试在默认路由之后添加一个包罗万象的 URL 处理程序:

      handlers:
      - url: /
        static_files: dist/index.html
        upload: dist/index.html
      
      - url: /(.*)$
        static_files: dist/\1
        upload: dist/(.*)
      
      

      请参阅Handlers Element 上的文档。

      【讨论】:

      • 您好克里斯托弗,感谢您的帮助。但这似乎不起作用。这是我更新的 app.yaml 文件 runtime: python27 api_version: 1 threadsafe: yes handlers: - url: /(.*\.(html|htm|map|js))$ static_files: dist/\1 upload: dist/.*\.(html|htm|map|js)$ - url: /assets/ static_dir: dist/assets 的网址 domain.com 它有效。但是对于domain.com/login,它会产生 404 错误。我想我需要一个正则表达式来匹配 /login 之类的目录以转发到 /index.html。任意指针
      • 谢谢。我终于更新了 app.yaml 以处理任何 url 模式。 @Christopher Peisert
      • 我正面临 php 文件的 page can’t be found 错误。这里是handler- url: /(.*)php script: www/mail.php php脚本非常存在,那为什么会产生page not found错误呢??
      • @CoolCoder 试试- url: (.*\.(php))$
      • @Christopher Peisert,我不认为它与 url 模式有关,因为当我在本地尝试使用 dev_appserver.py app.yaml 时它可以完美运行
      猜你喜欢
      • 1970-01-01
      • 2013-01-19
      • 2013-09-04
      • 1970-01-01
      • 1970-01-01
      • 2015-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多