【问题标题】:HTTP to HTTPS redirect not working in Apache+Varnish for WordPressHTTP 到 HTTPS 重定向在 WordPress 的 Apache+Varnish 中不起作用
【发布时间】:2021-10-26 17:22:07
【问题描述】:

我面临的问题是 http 到 https 重定向不起作用。我目前的设置是清漆+apache。我正在使用 apache 来终止 SSL。按照本指南 (https://bash-prompt.net/guides/apache-varnish/) 执行此操作(还按照该指南中的“ERR_TOO_MANY_REDIRECTS 错误”部分,因为我收到了太多重定向错误)。 Varnish 在端口 80 上运行。Apache 在端口 8181 上运行并终止 SSL。尝试在 htaccess 中添加重定向规则,但它不起作用。在 wp-config 中添加了以下几行

if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
   $_SERVER['HTTPS'] = 'on';
}

if ( !isset( $_SERVER['HTTPS'] ) ) {
    $_SERVER['HTTPS'] = 'on';
}
define( 'WP_HOME', 'https://example.com' );
define( 'WP_SITEURL', 'https://example.com' );

我当前的 varnish vcl 配置:

vcl 4.0;

# Default backend definition. Set this to point to your content server.
backend default {
    .host = "127.0.0.1";
    .port = "8181";
}

sub vcl_recv {

if (req.url ~ "wp-admin|wp-login") {

return (pass);

}

sub vcl_backend_response {
    # Happens after we have read the response headers from the backend.
    #
    # Here you clean the response headers, removing silly Set-Cookie headers
    # and other mistakes your backend does.
}

sub vcl_deliver {
    # Happens when we have all the pieces we need, and are about to send the
    # response to the client.
    #
    # You can do accounting or modifying the final object here.
}

我的 htaccess 配置:

SetEnvIf X-Forwarded-Proto "https" HTTPS=on
Header append Vary: X-Forwarded-Proto

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP:X-Forwarded-Proto} !https [NC]
  RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

我几乎尝试了所有教程。甚至尝试通过 varnish vcl 配置本身将 HTTP 重定向到 HTTPS,但似乎没有任何效果。请帮忙!

【问题讨论】:

    标签: php wordpress .htaccess https varnish


    【解决方案1】:

    将所有用户从 HTTP 重定向到 HTTPS

    您可以使用以下任何解决方案。您只需要使用其中的一个或全部三个,由您自己决定。

    1. PHP解决方案

    if(!isset($_SERVER['HTTPS']) || strtolower($_SERVER['HTTPS']) != 'on') {
        header('Location: https://www.yourdomain.com');
        exit;
    }
    

    2。 Apache .htaccess 解决方案

    RewriteEngine On
    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]
    

    3. Nginx 解决方案

    server {
        listen 80 default_server;
        server_name _;
        return 301 https://$host$request_uri;
    }
    

    1. Varnish / Wordpress 配置 - 手动设置解决方案

    https://betterprogramming.pub/set-up-varnish-cache-for-wordpress-8e2ac92ce347

    2。 Varnish / Wordpress config - Wordpress 插件解决方案

    https://en-gb.wordpress.org/plugins/vcaching/

    【讨论】:

    • 我已经尝试了您上面提到的资源中的所有内容。你检查过我遵循的 Apache + Varnish 设置指南吗?
    • 尝试禁用 Nginx 和 Varnish 以开始并仅使用 apache 和 .htacess 或 php 方法,然后如果可以从那里开始并添加 nginx,然后像我通常那样添加清漆,如果是这样,请告诉我作品。但是在每个阶段测试它们中的每一个,如果你一次尝试它们,你最终会像我们大多数人通常做的那样陷入圈子哈哈。
    • 如果您使用 php 重定向,请将其作为 public/index.php 中的第一行。使用 .htaccess 方法,您可能会发现 virtualhost 文件禁用了一些选项来阻止 .htaccess 文件重定向工作。然而,只要它位于 public/index.php 的开头,无论 virtualhost 文件如何,php 方法都应该可以正常工作
    • 您也可以创建一个 public/index.php 的备份,然后创建一个新的,并且只将代码放入其中,以确保测试 php 解决方案。只需确保使用 php 方法添加我上面写的退出命令,否则它不会总是有效。
    • 嗯,发生了一件奇怪的事情。我刚刚使用 sudo apt-get upgrade 更新了我的系统并解决了问题(apache 或 varnish 没有任何更新)
    【解决方案2】:

    对于另一个 php 解决方案,您也可以在 public/index.php 的开头使用它 这在你的情况下应该更有效

    if($_SERVER['SERVER_PORT'] != 443) {
        header('Location: https://www.yourdomain.com');
        exit;
    }
    
    echo 'Your current server port is = '.$_SERVER['SERVER_PORT'].'<br />';
    

    【讨论】:

    • 并将 .htaccess 设置为 wordpress 默认设置,如果 wordpress 不需要,则将其删除,
    猜你喜欢
    • 2016-07-15
    • 2018-01-31
    • 2016-03-18
    • 2014-03-10
    • 2018-06-11
    • 2015-07-28
    • 2019-02-02
    • 2017-12-07
    • 2012-06-07
    相关资源
    最近更新 更多