CentOS Linux 8 + Apache mod_ssl で https サーバへのリバースプロキシを設定する


概要

  • CentOS Linux 8 に Apache HTTP Server と mod_ssl をインストールして https サーバへのリバースプロキシを設定する

環境

  • CentOS Linux 8
  • Apache HTTP Server 2.4.37
$ cat /etc/centos-release
CentOS Linux release 8.1.1911 (Core)

Apache のインストール

dnf install コマンドで httpd パッケージをインストールする。

$ sudo dnf install httpd

インストールされたのを確認する。

$ dnf list --installed httpd
インストール済みパッケージ
httpd.x86_64  2.4.37-16.module_el8.1.0+256+ae790463  @AppStream

$ which httpd
/usr/sbin/httpd

$ httpd -v
Server version: Apache/2.4.37 (centos)
Server built:   Dec 23 2019 20:45:34

$ httpd -V
Server version: Apache/2.4.37 (centos)
Server built:   Dec 23 2019 20:45:34
Server's Module Magic Number: 20120211:83
Server loaded:  APR 1.6.3, APR-UTIL 1.6.1
Compiled using: APR 1.6.3, APR-UTIL 1.6.1
Architecture:   64-bit
Server MPM:     event
  threaded:     yes (fixed thread count)
    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.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"

systemd の httpd サービスを有効にする。

$ sudo systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.

Apache の起動

systemctl start コマンドで Apache を起動する。

$ sudo systemctl start httpd

Apache の状態を確認する。

$ systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2020-03-23 21:55:04 JST; 7min ago
     Docs: man:httpd.service(8)

Apache の設定ファイル

/etc/httpd/ 以下に Apache の設定ファイルがある。
メインの設定ファイルは /etc/httpd/conf/httpd.conf で、このファイルが他の設定ファイルを Include ディレクティブで追加している。

$ tree /etc/httpd/
/etc/httpd/
├── conf
│   ├── httpd.conf
│   └── magic
├── conf.d
│   ├── README
│   ├── autoindex.conf
│   ├── userdir.conf
│   └── welcome.conf
├── conf.modules.d
│   ├── 00-base.conf
│   ├── 00-dav.conf
│   ├── 00-lua.conf
│   ├── 00-mpm.conf
│   ├── 00-optional.conf
│   ├── 00-proxy.conf
│   ├── 00-systemd.conf
│   ├── 01-cgi.conf
│   ├── 10-h2.conf
│   ├── 10-proxy_h2.conf
│   └── README
├── logs -> ../../var/log/httpd
├── modules -> ../../usr/lib64/httpd/modules
├── run -> /run/httpd
└── state -> ../../var/lib/httpd

7 directories, 17 files

mod_ssl のインストール

dnf install コマンドで mod_ssl パッケージをインストールする。

$ sudo dnf install mod_ssl

設定ファイル ssl.conf と 00-ssl.conf が増えているので、必要に応じて Include ディレクティブで追加する。

$ find /etc/httpd | grep ssl
/etc/httpd/conf.d/ssl.conf
/etc/httpd/conf.modules.d/00-ssl.conf

リバースプロキシの設定

例えば以下のような内容を /etc/httpd/conf/httpd.conf の設定ファイルに記述する。
今回は Include ディレクティブを使わず1つの設定ファイルにまとめた。

ServerRoot "/etc/httpd"

LoadModule authz_core_module modules/mod_authz_core.so
LoadModule mpm_event_module modules/mod_mpm_event.so
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule unixd_module modules/mod_unixd.so
LoadModule ssl_module modules/mod_ssl.so
LoadModule systemd_module modules/mod_systemd.so

Listen 80
User apache
Group apache

ErrorLog "logs/error_log"

ProxyRequests Off
SSLProxyEngine On
ProxyPass /foobar/ https://hogehoge.example.com/foobar/
ProxyPassReverse /foobar/ https://hogehoge.example.com/foobar/

設定ファイルが間違っていないか apachectl configtest コマンドで確認できる。

$ apachectl configtest
Syntax OK

設定が間違っていなければ Apache を再起動して、設定を反映する。

$ sudo systemctl restart httpd

curl 等でアクセスしてコンテンツ生成元のサーバからレスポンスが返ってきているのを確認する。

$ curl --include --silent http://localhost/foobar/ | head
HTTP/1.1 200 OK
Date: Mon, 23 Mar 2020 13:05:09 GMT
Server: Foobar Frontend
Content-Type: text/html;charset=utf-8
Content-Length: 9876

<!DOCTYPE html>
<html>
<head>
<title>Hello, world.</title>

参考資料