V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
yangzh
V2EX  ›  问与答

有人用 wordpress 吗,能帮忙说说我不能正确调用 https 资源是什么原因?

  •  
  •   yangzh · 2014-07-06 12:10:16 +08:00 · 5098 次点击
    这是一个创建于 3581 天前的主题,其中的信息可能已经有所发展或是发生改变。
    ssl 证书已经正确部署。比如说直接访问 https://example.com/wp-content/themes/twentytwelve/style.css 是没有问题的,浏览器绿色小锁什么的应有尽有。

    settings 里面的 WordPress Address (URL) 和 Site Address (URL) 都已经设置为 https://example.com

    但是直接访问 https://example.com,网站是很多资源载入不正确的。具体来说,打开源代码,所有<link>资源都是 http 的!比如说style.css <link rel=stylesheet href="http://example.com/wp-content/themes/twentytwelve/style.css">

    吊诡的是,主题文件的 header 是这样子引入各种 css js 的: <link rel=stylesheet href="<?php echo get_template_directory_uri(); ?>/style.css">

    难道 wordpress 不是会根据使用的 protocol 或者 WordPress Address Site Address 从而在各种函数中返回 ‘https’ 的吗???!!!

    求助可能出现了什么问题?我觉得要可能是某些 wordpress 设置问题。
    第 1 条附言  ·  2014-07-06 19:44:43 +08:00
    问题知道出在什么问题了。

    就是服务器架构是前段 nginx 反代后端 apache 带来的复杂性。发出的是 https 请求,但是后端的 apache 或者 php 不知道是 https 请求:对于 <?php echo $_SERVER['SERVER_PORT']; ?>,即使在 https 网址访问这个 php 文件, echo 出来的端口依然是 80。所以 wordpress 不会自动转换网址。

    根据 9 楼 的兄弟说 apache 也要配置 https,但是我这里完全不理解怎样做。

    最后有一个 workaround:http://roger.steneteg.org/128/nginx-reverse-proxy-ssl-issues/

    nginx 添加一个头部:proxy_set_header X-Forwarded_Proto https;
    然后wordpress 的 wp-config.php 添加上:
    if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') $_SERVER['HTTPS']='on';

    就是用一个 workaround 把“这是https请求”传到 wordpress 那里。。。这个办法其实我觉得挺丑陋的。
    第 2 条附言  ·  2014-07-06 21:18:06 +08:00
    不行。。。崩溃了。。。完全不懂不行。。。
    第 3 条附言  ·  2014-07-06 21:27:35 +08:00
    最后的最后!ok 了!http://uwot.eu/blog/2013/07/wordpress-admin-ssl-apache-nginx/
    14 条回复    2014-08-16 00:05:11 +08:00
    yangqi
        1
    yangqi  
       2014-07-06 12:14:48 +08:00
    装了缓存插件没?
    yangzh
        2
    yangzh  
    OP
       2014-07-06 12:20:18 +08:00
    @yangqi 刷新了。
    yangzh
        3
    yangzh  
    OP
       2014-07-06 12:32:13 +08:00
    好像还是不能 append。我这里追加评论。
    用的是 lnmpa 的模式:前 nginx -> 后 apache 那样。

    我在php 文件中加入一句 <?php echo $_SERVER['SERVER_PORT']; ?>,即使在 https 网址访问这个 php 文件, echo 出来的端口依然是 80。估计这就是 wordpress 不能自动转换 https 的原因了。我猜测是 nginx 的配置有问题。有没有哪位大神指出 nginx 怎样配置才对啊。

    nginx 的与 apache 有关的 server 段:
    server{
    #...
    location / {
    try_files $uri @apache;
    }

    location @apache {
    internal;
    proxy_pass http://127.0.0.1:88;
    include proxy.conf;
    }

    location ~ [^/]\.php(/|$)
    {
    proxy_pass http://127.0.0.1:88;
    include proxy.conf;
    }
    }
    yangqi
        4
    yangqi  
       2014-07-06 12:41:37 +08:00
    那肯定是nginx没配置好了

    https的配置基本和http的一样, 除了证书部分, 你给出的server是http还是https的?
    ovear
        5
    ovear  
       2014-07-06 12:51:24 +08:00
    @yangzh 你的proxy_pass需要改为https才能让php判断到你是以https方式访问。
    yangzh
        6
    yangzh  
    OP
       2014-07-06 13:14:58 +08:00
    @yangqi
    listen 443 ssl;


    @ovear

    试试
    yangzh
        7
    yangzh  
    OP
       2014-07-06 13:17:17 +08:00
    @ovear

    proxy_pass https://127.0.0.1:88; 会导致 502 哦。apache 要怎样配置?
    yangzh
        8
    yangzh  
    OP
       2014-07-06 13:18:38 +08:00
    @yangqi
    nginx配置的证书肯定是好的。直接访问 https://example.com/wp-content/themes/twentytwelve/style.css 是没有问题的,浏览器绿色小锁什么的应有尽有。
    就是什么 https http 等信息 php 或 apache 不能正确获取。
    ovear
        9
    ovear  
       2014-07-06 15:25:23 +08:00
    @yangzh 后端apache要开启https
    zhouzb889
        10
    zhouzb889  
       2014-07-06 18:38:57 +08:00
    yangzh
        11
    yangzh  
    OP
       2014-07-06 19:19:46 +08:00
    @zhouzb889 无关这个
    panlilu
        12
    panlilu  
       2014-07-06 19:21:56 +08:00
    试一下把设置-常规选项里面的两个地址改成https打头的地址
    yangzh
        13
    yangzh  
    OP
       2014-07-06 19:38:58 +08:00
    @panlilu 设置了
    soome
        14
    soome  
       2014-08-16 00:05:11 +08:00
    同是lnmpa的。
    最近在弄ssl的问题,百思不得其姐。
    看了很多文章,用下面的配置,已经接近完全成功。

    nginx配置一个443的server,一个80的server(我这个用来强制跳转到https)。

    nginx的配置文件:

    server
    {
    listen 443;
    #listen [::]:80;
    server_name ca.net www.ca.net;
    index index.html index.htm index.php default.html default.htm default.php;
    root /home/xxxxx/xxxxx;

    ssl on;
    ssl_certificate crt; #修改具体文件
    ssl_certificate_key key; #修改具体文件
    ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers RC4:HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;
    keepalive_timeout 60;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;

    location / {
    try_files $uri @apache;
    }

    location @apache {
    internal;
    proxy_pass http://127.0.0.1:88;
    include proxy.conf;
    }

    location ~ [^/]\.php(/|$)
    {
    proxy_pass http://127.0.0.1:88;
    include proxy.conf;
    }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
    expires 30d;
    }

    location ~ .*\.(js|css)?$
    {
    expires 12h;
    }

    access_log off;
    }
    server {
    listen 80;
    server_name sinoca.net www.sca.net;
    rewrite ^/(.*) https://www.ca.net/$1 permanent;
    }


    apache要怎么弄?
    默认不设置的话,混合内容直接被chrome和火狐过滤掉。也就是引入的http链接的css,js等不会加载,网页框架就不正常了。

    apache也需要开启ssl。

    apache配置文件:
    <VirtualHost *:88>
    ServerAdmin [email protected]
    php_admin_value open_basedir "/xxxx/xxxx/xxxx:/tmp/:/var/tmp/:/proc/"
    DocumentRoot "/xxxx/xxxx/xxxx"
    ServerName ca.net
    ServerAlias www.ca.net
    #SSL Engine Switch:
    #Enable/Disable SSL for this virtual host.
    SSLEngine on

    # Server Private Key:
    # If the key is not combined with the certificate, use this
    # directive to point at the key file. Keep in mind that if
    # you've both a RSA and a DSA private key you can configure
    # both in parallel (to also allow the use of DSA ciphers, etc.)
    SSLCertificateFile ".crt"
    SSLCertificateKeyFile "/usr/.......key"
    #ErrorLog "logs/-error_log"
    #CustomLog "logs/-access_log" common
    </VirtualHost>

    重启服务就可以解决大部分问题了。主题和wordpress自带的css以及js这时候都是https链接了。
    因为wordpress的主题和插件太随意,还是有部分资源以http开头,这个需要自己去修改了。
    这些资源多半是设置的绝对链接。

    http://v2.freep.cn/3tb_14081523585682cu512293.png


    目前的疑问是,apache(2.2.27)是否支持单ip多证书的问题,有知道的么?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   982 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 21:18 · PVG 05:18 · LAX 14:18 · JFK 17:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.