大网数据广告活动
服务器运维问答

wordpress ERR_TOO_MANY_REDIRECTS through varnish, but not through apache

· 52CE 我爱测

I'm having this weird problem with a varnish-wordpress setup. I get TOO_MANY_REDIRECTS when I try to divert traffic through varnish, but if I configure apache directly with a vhost listening on 443, it works just fine. Relevant p…

问题原文

原帖链接 · Lethargos · CC BY-SA 4.0

I'm having this weird problem with a varnish-wordpress setup. I get TOO_MANY_REDIRECTS when I try to divert traffic through varnish, but if I configure apache directly with a vhost listening on 443, it works just fine. Relevant parts of default.vcl in varnish:

    if (std.port(local.ip) == 80 && req.http.host ~ "example.com(:[0-9]+)?$") {
    set req.http.x-redir = "https://" + req.http.host + req.url;
    return(synth(850, "Moved permanently"));

    [...]


   else if (req.http.host ~ "(www\.)?example.com(:[0-9]+)?$") {
    set req.backend_hint = web227;
        return (pipe);
        }
sub vcl_synth {
    if (resp.status == 850) {
        set resp.http.Location = req.http.x-redir;
        set resp.status = 301;
        return (deliver);
    }

I'm using pipe temporarily, in order to make sure I don't bump into more caching at this stage where I'm still testing.

On this varnish there are several wordpress backends that work without any issues, on which the same .htaccess resides, which the "classic" wordpress .htaccess:

    <IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

And here is my apache site configuration:

<VirtualHost *:80>
        ServerAdmin [email omitted]
        ServerName  digital.ringier.ro
        ErrorLog "/var/log/apache2/example.com.error.log"
        CustomLog "/var/log/apache2/example.com.access.log" common
        #Redirect / https://example.com/
        DocumentRoot "/var/www/example.com"
        <Directory "/var/www/example.com">
        AllowOverride all
        Options -Indexes +FollowSymLinks +MultiViews
        </Directory>
</VirtualHost>

<VirtualHost *:443>
        ServerAdmin [email omitted]
        ServerName  example.com
        DocumentRoot "/var/www/example.com"
        <Directory "/var/www/example.com">
        AllowOverride all
        Options -Indexes +FollowSymLinks +MultiViews
        </Directory>
        ErrorLog "/var/log/apache2/example.com.error.log"
        CustomLog "/var/log/apache2/example.com.access.log" common
        SSLEngine on
        SSLCertificateFile /etc/apache2/ssl/apache.crt
        SSLCertificateKeyFile /etc/apache2/ssl/apache.key
</VirtualHost>

I can't, for the life of me, understand why it works when I access apache directly (I'm changing the ip in hosts in order to do that), but not through varnish. This is the response I get in the browser (which is repeated dozens of times):

HTTP/1.1 301 Moved Permanently
Date: Tue, 02 Oct 2018 17:36:28 GMT
Server: Apache/2.4.18 (Ubuntu)
Location: https://example.com/
Content-Length: 0
Connection: close
Content-Type: text/html; charset=UTF-8

This is clearly a response coming from apache, not from varnish, as far as I can tell (because of the 'server' header and because 'via' header is missing) Any ideas how I can fix this?

已采纳解答

原帖链接 · Lethargos · CC BY-SA 4.0

Basically the problem was related to wp-config. I inserted this code:

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

if ( !isset( $_SERVER['HTTPS'] ) ) {
    $_SERVER['HTTPS'] = 'on';
}

And it worked. Initially I simply tried $_SERVER['HTTPS'] = 'on';, but I received a 50x error. Not sure exactly why. On other wp installation, the simple directly did work. I'll have to do some more tests in the future.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-10-02)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。