【httpd】リダイレクトの正体を探る

公開日時:2017年12月02日 / 最終更新日時:2019年02月11日

とあるサーバーの話ですが

http://localhost 

にアクセスすると、

https://localhost

にリダイレクトして且つエラーを出力するサーバーがありました。

 

しかも以下のような状況です。

 

 

このようなエラーになる

curlコマンドを実行すると、https://localhost にリダイレクトをされます。

[root@SAKURA_VPS www]# curl http://localhost
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="https://localhost/">here</a>.</p>
</body></html>
[root@SAKURA_VPS www]#

 

 

 

GUIログインをして「http://localhost」にアクセスしても「https://localhost」にリダイレクトをしてエラーになります。

 

 

 

 

httpd はどこでリダイレクトをしているのか

 

 

 

どこにも設定がないという謎の状態

「httpd.conf」か「.htaccess」ファイルでリダイレクトの設定をしているとみて調査をしましたが、どこにもリダイレクトが見つからず・・・

しかし動作を見たところ確実にリダイレクトはしているはず。

 

httpd はどこのコンフィグを読んでいるのか?どこでリダイレクトの設定がされているのか?

 

 

.htaccess を洗い出してみた

サーバー上の .htaccess をすべて洗い出してみます。

[root@SAKURA_VPS log]# find / -name .htaccess
find: ‘/run/user/1004/gvfs’: 許可がありません
/var/www/html/wordpress/wp-content/plugins/akismet/.htaccess
/var/www/html/wordpress/wp-content/database/.htaccess
/var/www/html/.htaccess
/home/xxx/wordpress/wp-content/plugins/akismet/.htaccess
[root@SAKURA_VPS log]#

 

 

■/var/www/html/.htaccessの中身

[root@SAKURA_VPS log]# cat /var/www/html/.htaccess
#Options -Indexes
#teset
#RewriteEngine On
#RewriteCond %{HTTPS} off
#RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

 

リダイレクトの設定がありましたが、「#」でコメントアウトされています。

そのため、この設定は効いていないはず。

しかし念には念を入れて、/var/www/html/.htaccess ファイルを削除して、httpd を再起動します。

[root@SAKURA_VPS log]# rm /var/www/html/.htaccess
rm: 通常ファイル `/var/www/html/.htaccess' を削除しますか? yes
[root@SAKURA_VPS log]#

 

httpd を再起動します。

[root@SAKURA_VPS log]# systemctl restart httpd

 

 

動作確認をします。

[root@SAKURA_VPS log]# curl http://localhost
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="https://localhost/">here</a>.</p>
</body></html>
[root@SAKURA_VPS log]#

 

やはりリダイレクトされます。

しかし、.htaccess が原因ではないので、httpd の設定(conf ファイル)が原因とみてさらに調査をします。

 

 

httpd の詳しい環境を調査する

httpd がどのような環境なのか?

httpd はどの設定ファイルを読み込んでいるのか?

httpd の環境変数は?

「httpd -V」コマンドで詳しい環境を確認することができます。

[root@SAKURA_VPS log]# httpd -V
Server version: Apache/2.4.6 (CentOS)
Server built:   Oct 19 2017 20:39:16
Server's Module Magic Number: 20120211:24
Server loaded:  APR 1.4.8, APR-UTIL 1.5.2
Compiled using: APR 1.4.8, APR-UTIL 1.5.2
Architecture:   64-bit
Server MPM:     prefork
  threaded:     no
    forked:     yes (variable process count)
Server compiled with....
 -D APR_HAS_SENDFILE
 -D APR_HAS_MMAP
 -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
 -D APR_USE_SYSVSEM_SERIALIZE
 -D APR_USE_PTHREAD_SERIALIZE
 -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
 -D APR_HAS_OTHER_CHILD
 -D AP_HAVE_RELIABLE_PIPED_LOGS
 -D DYNAMIC_MODULE_LIMIT=256
 -D HTTPD_ROOT="/etc/httpd"
 -D SUEXEC_BIN="/usr/sbin/suexec"
 -D DEFAULT_PIDLOG="/run/httpd/httpd.pid"
 -D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
 -D DEFAULT_ERRORLOG="logs/error_log"
 -D AP_TYPES_CONFIG_FILE="conf/mime.types"
 -D SERVER_CONFIG_FILE="conf/httpd.conf"
[root@SAKURA_VPS log]#

 

 

原因が判明「le-redirect-xx.com.conf」が原因だった

関連性があるとは全く考えないファイルが原因でした。

le-redirect-xxx.com.conf」の内容を確認すると、https にリダイレクトしています。

[root@SAKURA_VPS httpd]# cat le-redirect-xxx.com.conf
<VirtualHost _default_:80>
ServerName box-cm.com

ServerSignature Off

RewriteEngine On
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

ErrorLog /var/log/httpd/redirect.error.log
LogLevel warn

[root@SAKURA_VPS httpd]#

 

 

そもそも「le-redirect-xxx.com.conf」は何のファイルか?

調べたところ certbot(Let's Encrypt)の設定で作成されたファイルでした。

 

ちなみに以前「Let's Encrypt」で SSL 証明書を作成して https 通信ができるように設定をしています。

 

レンタルサーバーのサービスを利用せずに、自分で Let's Encrypt を導入する手順

 

【無料 独自SSL証明書】 なぜ無料でSSL証明書を提供できるのか? Let's Encrypt とは何か?

 

 

つまり、以前自分が「Let's Encrypt」をインストールして設定したことが原因でした。。

 

以前、xxx.com ドメインで SSL 証明書を作成しました。

その時の設定が以下のコマンドですが、確かに

# certbot run --apache -d www.xxxx.com ←再度certbotコマンドを実行します。
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Obtaining a new certificate
Performing the following challenges:
tls-sni-01 challenge for www.xxxx.com

We were unable to find a vhost with a ServerName or Address of www.xxxx.com.
Which virtual host would you like to choose?
(note: conf files with multiple vhosts are not yet supported)
-------------------------------------------------------------------------------
1: ssl.conf                       |                       | HTTPS | Enabled
-------------------------------------------------------------------------------
Press 1 [enter] to confirm the selection (press 'c' to cancel): 1 ←1を入力します。
Waiting for verification...
Cleaning up challenges
Generating key (2048 bits): /etc/letsencrypt/keys/0001_key-certbot.pem
Creating CSR: /etc/letsencrypt/csr/0001_csr-certbot.pem

We were unable to find a vhost with a ServerName or Address of www.xxxx.com.
Which virtual host would you like to choose?
(note: conf files with multiple vhosts are not yet supported)
-------------------------------------------------------------------------------
1: ssl.conf                       |                       | HTTPS | Enabled
-------------------------------------------------------------------------------

Press 1 [enter] to confirm the selection (press 'c' to cancel): 1 ←1を入力します。
Deploying Certificate to VirtualHost /etc/httpd/conf.d/ssl.conf

Please choose whether HTTPS access is required or optional.
-------------------------------------------------------------------------------
1: Easy - Allow both HTTP and HTTPS access to these sites
2: Secure - Make all requests redirect to secure HTTPS access
-------------------------------------------------------------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2 ←2を入力します。
Created redirect file: le-redirect-www.xxxx.com.conf ← 以前 Let's Encrypt を設定した際に「le-redirect-www.xxxx.com.conf」を作成していたことを忘れていました。
Rollback checkpoint is empty (no changes made?)

-------------------------------------------------------------------------------
Congratulations! You have successfully enabled https://www.xxxx.com

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=www.xxxx.com
-------------------------------------------------------------------------------

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at
   /etc/letsencrypt/live/www.xxxx.com/fullchain.pem. Your cert
   will expire on 2017-07-26. To obtain a new or tweaked version of
   this certificate in the future, simply run certbot again with the
   "certonly" option. To non-interactively renew *all* of your
   certificates, run "certbot renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

#

 

 

一旦「le-redirect-xxx.com.conf」ファイルを削除して復旧

現在は「Let's Encrypt」を使用していないので一旦「le-redirect-xxx.com.conf」ファイルを削除(移動)します。

必要になったら再度設定を入れます。

 

一旦ホームディレクトリにファイルを移動します。

[root@SAKURA_VPS conf.d]# mv le-redirect-xxx.com.conf ~/le-redirect-xxx.com.conf_20171202

 

 

httpdを再起動します。 

[root@SAKURA_VPS httpd]# systemctl restart httpd

 

 

curlコマンドで動作確認をします。

リダイレクトされず、「Hello World!」が出力されました。

[root@SAKURA_VPS httpd]# curl http://localhost
test
Hello World!
[root@SAKURA_VPS httpd]#

 

 

GUI上からブラウザを起動して動作確認をします。

ブラウザ上からも「Hello World!」が確認できました。

 

 

まとめ「httpd は /etc/httpd/conf.d ディレクトリ配下の conf ファイル読み込むので注意」

今回は、yum コマンドで httpd をインストールしました。

その場合、httpd は /etc/httpd/conf.d ディレクトリ配下の conf ファイルを読み込む(インクルードする)ので注意です。

 

■/etc/httpd/conf/httpd.conf ファイルの抜粋

# Supplemental configuration
#
# Load config files in the "/etc/httpd/conf.d" directory, if any.
IncludeOptional conf.d/*.conf

 

IncludeOptional で、conf.d ディレクトリ配下の「*.conf」をインクルードしています。

 

「IncludeOptional」ディレクトティブは、「Include」ディレクティブとほぼ一緒です。

 

「Include」ディレクトティブと「IncludeOptional」ディレクトティブの違い

 

 

参考にしたサイト

Apache 2.4 設定ファイルの記述例

https://qiita.com/100/items/ab31e57fcc66ac661d5c

 

 

 

 

Posted by 100%レンタルサーバーを使いこなすサイト管理人

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

AlphaOmega Captcha Medica  –  What Do You See?
     
 

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください

Secured By miniOrange