【问题标题】:How to permanently redirect `http://` and `www.` URLs to `https://`?如何将 `http://` 和 `www.` URL 永久重定向到 `https://`?
【发布时间】:2016-02-26 00:26:55
【问题描述】:

我有一个 Google App Engine 项目。在这个项目中,我设置了一个自定义域和一个 SSL 证书。因此,我可以使用https://www.mysite.xxxhttp://www.mysite.xxx 和裸域mysite.xxx

是否可以使用开发人员控制台永久重定向最后两个以始终使用安全的https:// 域,还是只需要在代码中重定向?

【问题讨论】:

    标签: google-app-engine https url-redirection


    【解决方案1】:

    因此您可以将“安全:始终”添加到您的 yaml 文件中

    https://cloud.google.com/appengine/docs/python/config/appconfig?hl=en#Python_app_yaml_Secure_URLs

    【讨论】:

    • 我已阅读文档,但运气不佳。有关我的 app.yaml,请参见下文。你看有什么不对吗?运行时:nodejs env:flex 处理程序:- url:/.* 脚本:/public/index.js 安全:总是
    • 对于 java - 你可以参考以下 URL - cloud.google.com/appengine/docs/standard/java/config/…
    • 我试过了,还是不行。根据这篇帖子groups.google.com/forum/#!topic/google-appengine/mVRvsySeef8,重定向过程应该在您的应用程序中完成,而不是在 yaml 中。
    • 以上答案对于 AppEngine 标准是正确的。在 Flex 上,您必须采取不同的方法。
    【解决方案2】:

    (至少对于 Node,)在您的 app.yaml 中,添加以下内容:

    handlers:
    - url: /.*
      secure: always
      redirect_http_response_code: 301
      script: auto
    

    参考:https://cloud.google.com/appengine/docs/standard/nodejs/config/appref

    【讨论】:

      【解决方案3】:

      为了完整起见。 Java的方式就是像这样将传输保证设置为机密。

      <security-constraint>
        <web-resource-collection>
          <web-resource-name>profile</web-resource-name>
          <url-pattern>/profile/*</url-pattern>
        </web-resource-collection>
        <user-data-constraint>
          <transport-guarantee>CONFIDENTIAL</transport-guarantee>
        </user-data-constraint>
      </security-constraint>
      

      你也可以找到这个here in the documentation

      【讨论】:

        【解决方案4】:

        以防万一,App Engine Flexible 上的 app.yaml 中无法包含安全处理程序,不支持它们:

        对于 App Engine 柔性环境,处理程序下的安全设置现已弃用。如果您需要 SSL 重定向,您可以更新您的应用程序代码并使用 X-Forwarded-Proto 标头来重定向 http 流量。 (参考:https://cloud.google.com/appengine/docs/flexible/java/upgrading#appyaml_changes

        引用来自 Java,但对于 Node.js 来说似乎是一样的。我尝试包含处理程序,但没有成功。

        如您所见,一个可能的解决方案是“使用 X-Forwarded-Proto 标头重定向 http 流量”。我没有尝试过,因为我将转向 App Engine Standard,但有人已经这样做并解释了 here

        【讨论】:

          【解决方案5】:

          应该在您的应用程序中完成。请查看此帖https://stackoverflow.com/a/54289378/5293578

          我试过下面的代码,它对我有用(你必须把它放在默认请求和错误处理程序之前):

          /**==== File: server.js =======**/
          
          /** Express configuration **/
          
          // HTTPS Redirection
          if (process.env.NODE_ENV === 'production') {
            app.use (function (req, res, next) {
              var schema = (req.headers['x-forwarded-proto'] || '').toLowerCase();
              if (schema === 'https') {
                next();
              } else {
                res.redirect('https://' + req.headers.host + req.url);
              }
            });
          }
          
          /** ... more configuration **/
          
          // Default request handler
          app.use(function(req, res, next) {
            // ... your code
          });
          
          // Default error handler
          app.use(function(err, req, res, next) {
            // ... your code
          });
          

          【讨论】:

          • 仅当您的环境不灵活时,接受的答案才更适合标准环境。即使是灵活的事情可能发生变化,请参阅stackoverflow.com/a/54833147/4495081 上的 cmets
          【解决方案6】:

          如果您的域名被购买或transferredGoogle Domain,那么您可以在G-SuiteSynthetic records 部分下进行:

          【讨论】:

            【解决方案7】:

            在我在 app.yml 中的每个处理程序上始终添加安全之后,它对我有用。我部署在 GCP 上的节点服务器为客户端和 express API 提供角度服务,因此为了使角度路由正常工作,我必须在 API 端点上添加“api”,所以这是它的工作原理

            runtime: nodejs14
            handlers:
            - url: /(.*\.(gif|png|jpg|JPG|css|js|ttf|map)(|\.map))$
              static_files: public/dist/\1
              upload: public/dist/(.*)(|\.map)
              secure: always
              redirect_http_response_code: 301
            - url: /api/.*  
              secure: always
              script: auto
              redirect_http_response_code: 301
            
            - url: /(.*)
              static_files: public/dist/index.html
              upload: public/dist/index.html
              secure: always
              redirect_http_response_code: 301
            
            - url: /.*
              secure: always
              script: auto
              redirect_http_response_code: 301
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2014-08-29
              • 2016-10-14
              • 2014-06-11
              • 2018-09-26
              • 2016-08-31
              • 2014-05-08
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多