WordPress 予約投稿の失敗
こんにちは。今回は「アリバイのために予約投稿してたのにまったく投稿されていなかったw」な件でございます。(嘘です)
今回も相変わらずなレアケース
なのであまり役に立たないと思いますが…先日の記事でさくらインターネットのSNISSLを使って常時SSL化しようとした後、WordPressでの予約投稿が失敗するようになってしまったので、原因をたどりました。
おさらい
※姉妹サイト http://gt-four.net のお話です。http://gt-four.net にアクセスするとhttp[s]://gt-four.net へ転送されるようにしたかったという件です。前回の記事では、以下をhtaccessに追記することで、全てのhttpアクセスをhttpsに転送することを実現しました。一般のユーザのアクセスはこれで問題なく動作します。
#これ効かない RewriteCond %{HTTPS} off #こっちがちゃんと反応する RewriteCond %{ENV:HTTPS} !^on$ RewriteCond %{HTTP:X-SAKURA-FORWARDED-FOR} ^$ RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
予約投稿が失敗するようになった原因
WPの予約投稿は、予約した時間以降にアクセスがあったときに、ローカルサーバから、http://example.com/wp-cron.php へアクセスし処理をすることによって実現してい(ると思い)ます。
↑のhtaccessを書いたことによって、その処理がうまく走らなかったようです。
実験
さくらインターネットのサーバにSSHでログインします。
FreeBSD 9.1-RELEASE-p24 (SAKURA17) #0: Thu Feb 5 10:03:29 JST 2015 Welcome to FreeBSD!
wgetでおもむろにwp-cron.php にアクセスしてみます。
※200が返ってくれば正常に処理は完了します。
結果
% wget http://gt-four.net/wp-cron.php --2017-05-18 10:34:52-- http://gt-four.net/wp-cron.php Resolving gt-four.net... 219.94.***.*** Connecting to gt-four.net|219.94.***.***|:80... connected. HTTP request sent, awaiting response... 302 Found Location: https://gt-four.net/wp-cron.php [following] --2017-05-18 10:34:52-- https://gt-four.net/wp-cron.php Connecting to gt-four.net|219.94.***.***|:443... connected. HTTP request sent, awaiting response... 302 Found Location: https://gt-four.net/wp-cron.php [following] --2017-05-18 10:34:52-- https://gt-four.net/wp-cron.php Reusing existing connection to gt-four.net:443. HTTP request sent, awaiting response... 302 Found Location: https://gt-four.net/wp-cron.php [following] --2017-05-18 10:34:52-- https://gt-four.net/wp-cron.php Reusing existing connection to gt-four.net:443. HTTP request sent, awaiting response... 302 Found Location: https://gt-four.net/wp-cron.php [following] --2017-05-18 10:34:52-- https://gt-four.net/wp-cron.php Reusing existing connection to gt-four.net:443. HTTP request sent, awaiting response... 302 Found Location: https://gt-four.net/wp-cron.php [following] --2017-05-18 10:34:52-- https://gt-four.net/wp-cron.php Reusing existing connection to gt-four.net:443. HTTP request sent, awaiting response... 302 Found Location: https://gt-four.net/wp-cron.php [following] --2017-05-18 10:34:52-- https://gt-four.net/wp-cron.php Reusing existing connection to gt-four.net:443. HTTP request sent, awaiting response... 302 Found Location: https://gt-four.net/wp-cron.php [following] --2017-05-18 10:34:52-- https://gt-four.net/wp-cron.php Reusing existing connection to gt-four.net:443. HTTP request sent, awaiting response... 302 Found Location: https://gt-four.net/wp-cron.php [following] --2017-05-18 10:34:52-- https://gt-four.net/wp-cron.php Reusing existing connection to gt-four.net:443. HTTP request sent, awaiting response... 302 Found Location: https://gt-four.net/wp-cron.php [following] --2017-05-18 10:34:52-- https://gt-four.net/wp-cron.php Reusing existing connection to gt-four.net:443. HTTP request sent, awaiting response... 302 Found Location: https://gt-four.net/wp-cron.php [following] --2017-05-18 10:34:52-- https://gt-four.net/wp-cron.php Reusing existing connection to gt-four.net:443. HTTP request sent, awaiting response... 302 Found Location: https://gt-four.net/wp-cron.php [following] --2017-05-18 10:34:52-- https://gt-four.net/wp-cron.php Reusing existing connection to gt-four.net:443. HTTP request sent, awaiting response... 302 Found Location: https://gt-four.net/wp-cron.php [following] --2017-05-18 10:34:52-- https://gt-four.net/wp-cron.php Connecting to gt-four.net|219.94.***.***|:443... connected. HTTP request sent, awaiting response... 302 Found Location: https://gt-four.net/wp-cron.php [following] --2017-05-18 10:34:52-- https://gt-four.net/wp-cron.php Reusing existing connection to gt-four.net:443. HTTP request sent, awaiting response... 302 Found Location: https://gt-four.net/wp-cron.php [following] --2017-05-18 10:34:52-- https://gt-four.net/wp-cron.php Reusing existing connection to gt-four.net:443. HTTP request sent, awaiting response... 302 Found Location: https://gt-four.net/wp-cron.php [following] --2017-05-18 10:34:52-- https://gt-four.net/wp-cron.php Reusing existing connection to gt-four.net:443. HTTP request sent, awaiting response... 302 Found Location: https://gt-four.net/wp-cron.php [following] --2017-05-18 10:34:52-- https://gt-four.net/wp-cron.php Reusing existing connection to gt-four.net:443. HTTP request sent, awaiting response... 302 Found Location: https://gt-four.net/wp-cron.php [following] --2017-05-18 10:34:52-- https://gt-four.net/wp-cron.php Reusing existing connection to gt-four.net:443. HTTP request sent, awaiting response... 302 Found Location: https://gt-four.net/wp-cron.php [following] --2017-05-18 10:34:52-- https://gt-four.net/wp-cron.php Reusing existing connection to gt-four.net:443. HTTP request sent, awaiting response... 302 Found Location: https://gt-four.net/wp-cron.php [following] --2017-05-18 10:34:52-- https://gt-four.net/wp-cron.php Reusing existing connection to gt-four.net:443. HTTP request sent, awaiting response... 302 Found Location: https://gt-four.net/wp-cron.php [following] --2017-05-18 10:34:52-- https://gt-four.net/wp-cron.php Reusing existing connection to gt-four.net:443. HTTP request sent, awaiting response... 302 Found Location: https://gt-four.net/wp-cron.php [following] 20 redirections exceeded.
あっ…(察し)
1回目のアクセスで rewrite されてhttps:// に転送されていますが、443への接続を試行されたあと、延々と302リダイレクトをかまされて、ループしちゃってます(>_<)
たぶん、さくらのSNISSLの仕組みが原因なやつで、前回無理矢理対処したやつの弊害でしょう。(´・ω・`)知らんがな
原因はわかりましたが、どういう対処をしよう…?
wp-cronだけ、httpsにしない?いや、それは不自然すぎるだろう…?
しばらく考えたあげく、ローカル(さくらインターネットのレン鯖自身)からのアクセスの際はrewriteしないようにしました。これが一番自然…かな?と
RewriteCond に1行追加。
<IfModule mod_rewrite.c> #自分からのアクセス「以外」を対象とする RewriteCond %{REMOTE_ADDR} !^219\.94\.xxx\.xxx$ #これは効かない RewriteCond %{HTTPS} off #謎のヘッダ RewriteCond %{ENV:HTTPS} !^on$ RewriteCond %{HTTP:X-SAKURA-FORWARDED-FOR} ^$ RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L] </IfModule>
このように記述することにより、ローカルからのアクセスはrewrite対象外になり、httpのままアクセスされますので素直に動いてくれました。
再度実験
% wget http://gt-four.net/wp-cron.php --2017-05-18 12:01:38-- http://gt-four.net/wp-cron.php Resolving gt-four.net... 219.94.162.44 Connecting to gt-four.net|219.94.162.44|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 0 Saving to: 'wp-cron.php' wp-cron.php [ <=> ] 0 --.-KB/s in 0s
無事にアクセス完了し、予約投稿が動作しました(>_<)
というか、さくらインターネットでWordPress、めちゃくちゃ重いですね、引っ越しも考えているけれど