当WP在反向代理之后时,我如何正确地处理SSL?

时间:2021-05-10 作者:Octopus

我正在代理Wordpress。wp\\u includes/load中的is\\u ssl()函数。php永远无法在这样的环境中工作,因为$\\u服务器[\'HTTPS\']不知道浏览器如何查看页面。所有请求都由代理规范化。

我可以通过更改is\\u ssl()函数来让我的站点正常工作,但现在,Wordpress会定期;“修复”;自动更新时的修复。

处理这种情况的首选方法是什么?我目前使用的是v5.7.1,我甚至没有找到禁用更新的方法。无论如何,我宁愿不禁用更新。

How can I tell Wordpress that is_ssl() is always true, and keep it permanently throughout updates?

1 个回复
最合适的回答,由SO网友:Rup 整理而成

You can\'t hook is_ssl() to override the result, and as you\'ve noticed you can\'t edit WordPress Core itself or your changes will get lost if you\'re using built-in automatic updates.

So the usual approach - see the WordPress documentation - is to set $_SERVER[\'HTTPS\'] = \'on\';, which is the property is_ssl() tests. Add the following block to wp-config.php (which is preserved during updates), somewhere before the final require_once:

if (strpos($_SERVER[\'HTTP_X_FORWARDED_PROTO\'], \'https\') !== false) {
    $_SERVER[\'HTTPS\'] = \'on\';
}

This tests whether your reverse proxy added a header X-Forwarded-Proto: https to the proxied request and if it did it sets the HTTPS flag for WordPress, so that the SSL flag does try and reflect the original request. The Really Simple SSL plugin has a more comprehensive version of this that I\'ve used too that tests more values from other proxies:

//Begin Really Simple SSL Load balancing fix
if ((isset($_ENV["HTTPS"]) && ("on" == $_ENV["HTTPS"]))
  || (isset($_SERVER["HTTP_X_FORWARDED_SSL"]) && (strpos($_SERVER["HTTP_X_FORWARDED_SSL"], "1") !== false))
  || (isset($_SERVER["HTTP_X_FORWARDED_SSL"]) && (strpos($_SERVER["HTTP_X_FORWARDED_SSL"], "on") !== false))
  || (isset($_SERVER["HTTP_CF_VISITOR"]) && (strpos($_SERVER["HTTP_CF_VISITOR"], "https") !== false))
  || (isset($_SERVER["HTTP_CLOUDFRONT_FORWARDED_PROTO"]) && (strpos($_SERVER["HTTP_CLOUDFRONT_FORWARDED_PROTO"], "https") !== false))
  || (isset($_SERVER["HTTP_X_FORWARDED_PROTO"]) && (strpos($_SERVER["HTTP_X_FORWARDED_PROTO"], "https") !== false))
  || (isset($_SERVER["HTTP_X_PROTO"]) && (strpos($_SERVER["HTTP_X_PROTO"], "SSL") !== false))
) {
  $_SERVER["HTTPS"] = "on";
}
//END Really Simple SSL

And there\'s an alternative approach here on StackOverflow where you can use Apache configuration to set HTTPS=1 instead if that\'s easier:

<IfModule mod_setenvif.c>
  SetEnvIf X-Forwarded-Proto "^https$" HTTPS
</IfModule>

rather than editing wp-config.

相关推荐

当我的远程产品在HTTPS上时,如何最好地将本地主机保留在Http上

我正在使用插件**Really Simple SSL** 配置为使用SSL的。我使用WP Migrate Pro将数据库从本地移动到远程。但我通常会遇到pulling 从productions到localhost的DB在开发过程中,我在localhost上的大多数链接都使用https。我不想在本地开发上设置SSL。目前,我停用plugin Really Simple SSL 使用“wp cli”。接下来,我必须在本地主机上找到并将https替换为http。这使得更新使用不安全的回购和我的字体,css和cd