Nginx+apache反向代理上的WordPress重定向循环

时间:2019-03-10 作者:Capy

我正在尝试在nginx+apache反向代理配置上安装新的Wordpress。我的安装过程如下:

安装的nginx和apache服务器将nginx(下面的conf)配置为代理apache服务器(在端口8080上侦听)

  • 生成了让我们使用带有nginx插件的certbot加密SSL证书。com,setup file ownership/permissions运行wordpress安装,生成配置并添加HTTPS详细信息(下面的conf)
  • 我打算只允许通过HTTPS访问此站点,因此我已设置nginx conf将所有流量重定向到端口443。

    /etc/nginx/mysite.com:

    server {
    
        root /var/www/mysite.com;
        index index.php index.html index.htm;
    
        server_name mysite.com;
    
        location / {
        try_files $uri $uri/ /index.php;
        }
    
        location ~ \\.php$ {
    
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Proto $scheme; # scheme: https
        proxy_set_header Host $host;
        proxy_pass http://127.0.0.1:8080;
    
         }
    
         location ~ /\\.ht {
                deny all;
        }
    
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/mysite.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mysite.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    
    }
    server {
    if ($host = mysite.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot
    
    
        listen   80;
    
        server_name mysite.com;
    return 404; # managed by Certbot
    
    
    }
    

    wp-config.php:

    <?php
    
    define( \'DB_NAME\', \'x\' );
    ...
    define( \'NONCE_SALT\',       \'x\' );
    
    $table_prefix = \'wp_\';
    
    define( \'WP_DEBUG\', false );
    
    if ( $_SERVER[\'HTTP_X_FORWARDED_PROTO\'] == \'https\' )
    {
        $_SERVER[\'HTTPS\']       = \'on\';
        $_SERVER[\'SERVER_PORT\'] = \'443\';
        define(\'FORCE_SSL_ADMIN\', true);
    }
    
    if ( isset($_SERVER[\'HTTP_X_FORWARDED_HOST\'])) {
        $_SERVER[\'HTTP_HOST\'] = $_SERVER[\'HTTP_X_FORWARDED_HOST\'];
    }
    
    if ( ! defined( \'ABSPATH\' ) ) {
        define( \'ABSPATH\', dirname( __FILE__ ) . \'/\' );
    }
    
    require_once( ABSPATH . \'wp-settings.php\' );
    
    使用此安装,我可以访问/wp admin/而没有任何问题,但是当我尝试访问主站点时,我遇到了重定向循环。Chrome网络控制台显示:Wordpress front page produces endless redirect loop

    有人能解决这个问题吗?我在各种网站上尝试了许多解决方案,包括一些声称可以为您管理SSL页面设置的插件,但都没有成功。如果您需要更多详细信息,请随时询问!

    EDIT:来自每个重定向的原始响应:

    HTTP/1.1 301 Moved Permanently
    Server: nginx/1.14.0 (Ubuntu)
    Date: Sun, 10 Mar 2019 16:35:06 GMT
    Content-Type: text/html; charset=UTF-8
    Content-Length: 0
    Connection: keep-alive
    Expires: Wed, 11 Jan 1984 05:00:00 GMT
    Cache-Control: no-cache, must-revalidate, max-age=0
    X-Redirect-By: WordPress
    Location: https://example.com/
    
    当我尝试请求/时,我得到一个301永久移动响应,告诉我资源已移动到/,从而导致重定向循环。

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

    Issue was caused by nginx serving example.com/index.php while Wordpress was redirecting to example.com/, thus causing a redirect loop.

    这是我用来修复重定向循环的工作配置:

    server {
    server_name example.com;
    root /var/www/example.com;
    
    index index.php;
    
    listen 443 ssl;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
    
    location / {
            try_files $uri @apache;
    }
    
    location ~[^?]*/$ { # proxy directories
            proxy_set_header X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header Host $host;
            proxy_pass http://127.0.0.1:8080;
    }
    
    location ~ \\.php$ { # serve php files
            proxy_set_header X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header Host $host;
            proxy_pass http://127.0.0.1:8080;
    }
    
    location @apache { # used by location /
            proxy_set_header X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header Host $host;
            proxy_pass http://127.0.0.1:8080;
    }
    
    location ~ /\\.ht { # Deny access to .htaccess, .htpassword 
            deny all;
    }
    }
    server {
        listen 80;
        server_name _;
        return 301 https://$host$request_uri;
    }
    

    Another thread with more details.

    相关推荐

    导致Chrome上的SSL中断的主题

    当访问我的网站designopacity时。firefox上的com没有出现任何问题,它表示视线是安全的。当在chrome上访问时,会出现错误消息,指出im使用的主题不安全的网站。有没有办法绕过这个?我尝试了内容SSL不安全的内容,但错误仍然存在。非常感谢。