zuntan02のはてなブログ

備忘録的なものです。時々職場の技術者ブログにも転記してますが、メインはこちらで。

【AWS+ELB+SSL+Wordpres】ELB配下のWordpressでサイト全体をSSL化しようとしてハマった

AWS Certificate ManagerでSSL取ってELB配下のEC2でWordpressをサイト全体SSL(所謂”常時SSL”)で動かそうとしてはまったのでメモ。

【結論】
wp-config.phpの上部に

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

を追記した上で、Wordpressの管理画面でURLをSSLにしてやる。

□公式ドキュメント
Function Reference/is ssl « WordPress Codex



【原因】
こちらに詳しい。とても分かりやすい解説。
snickerjp.blogspot.jp


曰く
SSLの処理を別サーバーに任せる(SSLオフロード)と『is_ssl関数』が効かない
→function is_ssl() では判定が「Port 443」だったらHTTPS、となっているが、ELB等SSLオフロードのために内部的にPort 80で受けていると、これが動作しない

勉強になりました。。。

追記:/etc/nginx/conf.d/hogefuga.conf

server {
    listen 80;

    server_name hoge.fuga.jp;
    root /var/www/html/hogefugajp/;
    index   index.php index.html index.htm;

    if ($http_x_forwarded_proto != https) {
      return 301 https://$host$request_uri;
    }

    access_log  /var/log/nginx/hoge.fuga.jp.access.log  main;
    error_log   /var/log/nginx/hoge.fuga.jp.error.log   warn;
    charset utf-8;


    # Deny all access to the wp-config.php
    set $deny_f 0;
    if ($request_uri ~* .*\/wp-config\.php$ ){
        set $deny_f 1;
    }
    if ( $deny_f = 1) {
        return 403;
    }


    location / {
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

}