【问题标题】:Wordpress with ssl and reverse proxy (nginx)带有 ssl 和反向代理 (nginx) 的 Wordpress
【发布时间】:2016-03-31 14:17:48
【问题描述】:

我在我的 wordpress 网站前面添加了一个 nginx 服务器。我还在 nginx 上添加了我的 ssl 证书,以便通过 https 保护我的网站。

使用我在此处粘贴的配置,如果我使用二十六主题访问前端,我会在 chrome JS 控制台中收到以下错误:

Mixed Content: The page at 'https://my.website.it/' was loaded over HTTPS, but requested an insecure script 'http://my.website.it/wp-content/themes/twentysixteen/js/functions.js?ver=20150825'. This request has been blocked; the content must be served over HTTPS.

如果我访问https://my.website.it/wp-admin,我将被重定向到https://wp-admin/

这是我的wordpress的配置:

Wordpress 服务器的配置如下:

wp-config.php

$_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST'];
define('FORCE_SSL_ADMIN','true');
define('WP_SITEURL','https://my.website.it');
define('WP_HOME','https://my.website.it');

我的 .htaccess

RewriteEngine On
RewriteCond %{HTTP_HOST} !^wploadbalance$ [NC]
RewriteRule ^(.*)$ https://my.website.it/$1 [L,R=301]

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

这里是我的 nginx 服务器的配置:

server {
       listen 80;
       server_name    my.website.it;
       return         301 https://$server_name$request_uri;
}

server {
       listen 443;
       server_name my.website.it;

       ssl                  on;
       ssl_certificate      /etc/ssl/certificate/certificate.crt;
       ssl_certificate_key  /etc/ssl/private/mprivate.key;

       ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
       ssl_ciphers    EECDH+AESGCM:EDH+AESGCM:ECDHE-RSA-AES128-GCM-SHA256:AES256+EECDH:DHE-RSA-AES128-GCM-SHA256:AES256+EDH:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4;

       ssl_prefer_server_ciphers  on;
       ssl_session_timeout        10m;
       ssl_session_cache          shared:SSL:10m;

       location / {
           proxy_pass http://wploadbalance;
           client_max_body_size 20M;

           proxy_set_header Host $host;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

           proxy_set_header X-Forwarded-Proto https;

           proxy_redirect off;

           proxy_buffers 64 4k;
    }
}

upstream wploadbalance{
        least_conn;
        server wordpress;
}

【问题讨论】:

    标签: wordpress .htaccess ssl nginx


    【解决方案1】:

    将这些行添加到wp-config.php:

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

    【讨论】:

      【解决方案2】:

      您可以在 Apache VirtualHost 配置或 .htaccess 中直接添加以下行,而不是向 wp-config.php 添加行:

      SetEnvIf X-Forwarded-Proto https HTTPS=on
      

      这一行到 Apache2 配置:

      LoadModule setenvif_module libexec/apache22/mod_setenvif.so
      

      【讨论】:

        猜你喜欢
        • 2023-03-04
        • 2018-02-09
        • 2020-06-06
        • 2021-05-21
        • 1970-01-01
        • 2014-09-06
        • 2017-09-27
        • 2019-05-12
        • 2018-05-31
        相关资源
        最近更新 更多