0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Apache をバックエンドにして nginx をリバースプロキシにする

Last updated at Posted at 2025-04-02

nginx の問題点

高機能が故に重くなってしまった Apache HTTP Server(以下 Apache)と比べて、nginx は軽量な Web サーバーとして近年 Apache よりもシェアを伸ばしている。

しかし nginx の欠点として、これまで Apache でよく利用されてきた .htaccess ファイルによるリダイレクトやベーシック認証の設定が行えないという問題点がある。

この問題点を解決する方法として、Apache をリバースプロキシの参照先として利用することでフロントを nginx にしたまま .htaccess ファイルによるリダイレクトやベーシック認証の設定が行えるようになる。

Apache と nginx のセットアップ

環境

  • AlmaLinux 9.5
  • Apache 2.4.62
  • nginx 1.20.1

インストール

dnf install httpd nginx

サービスの確認

systemctl is-enabled httpd nginx
disabled
disabled

インストール時はサービスが有効(ブート時起動)になっていない。

systemctl status httpd nginx
○ httpd.service - The Apache HTTP Server
     Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; preset: disabled)
     Active: inactive (dead)
       Docs: man:httpd.service(8)

○ nginx.service - The nginx HTTP and reverse proxy server
     Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; preset: disabled)
     Active: inactive (dead)

インストール時はサービスが実行(起動)されていない。

Apache の設定

ポート番号変更設定

echo 'Listen 8080' >> /etc/httpd/conf.d/reverse-proxy.conf
echo 'ServerName localhost:8080' >> /etc/httpd/conf.d/reverse-proxy.conf

8080 ポートはサービス名 http-alt で HTTP Alternate として公式に登録されている。 1

80 番ポートを閉じる

sed --expression='/^Listen/ s/^/#/' --in-place /etc/httpd/conf/httpd.conf

デフォルトの conf/httpd.conf で指定されている Listen 80 をコメントアウトしないと Apache が80番ポートを占有してしまい、nginx が以下のエラーを返して起動しない。

nginx: [emerg] bind() to 0.0.0.0:80 failed (XX: Address already in use)

ServerName ディレクティブが指定されていないと以下のエラーメッセージが表示される。

httpd: Could not reliably determine the server's fully qualified domain name, using xxxx::xxx:xxxx:xxxx:xxxx%enp0s3. Set the 'ServerName' directive globally to suppress this message

ポートの開放

8080 ポートは firewalld に登録されていないので、 firewall-cmd --new-service コマンドで追加する。 2

firewall-cmd --permanent --new-service=http-alt
firewall-cmd --permanent --service=http-alt --set-short='WWW Alternate (HTTP)'
firewall-cmd --permanent --service=http-alt --set-description='HTTP Alternate. HTTP is the protocol used to serve Web pages.'
firewall-cmd --permanent --service=http-alt --add-port=8080/tcp
firewall-cmd --permanent --add-service=http-alt
firewall-cmd --reload
firewall-cmd --list-services

nginx の設定

conf.d/reverse-proxy.conf
server {
    listen 80;

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        real_ip_header X-Forwarded-For;
    }
}

proxy_pass 3

nginx はデフォルトでは HTTP/1.0 を使用してプロキシリクエストを行うため、proxy_http_version1.1 を指定してやる必要がある。 4

SELinux の設定変更

setsebool -P httpd_can_network_relay 1

SELinux が有効な場合、上記の設定を変更しないと以下のエラーが出て 502 Bad Gateway が返される。

connect() to 127.0.0.1:8080 failed (13: Permission denied) while connecting to upstream`

httpd_can_network_connect を on にする記事が多く、それで実際に動作するが、 httpd_can_network_relay の方が本来的でよりセキュア。56

setsebool -P httpd_can_network_connect 1

サービスの起動

systemctl --now start httpd nginx
  1. Service Name and Transport Protocol Port Number Registry - www.iana.org

  2. To add a new and empty service, use the --new-service altogether with the --permanent option:Documentation - HowTo - Add a Service | firewalld - firewalld.org

  3. Sets the protocol and address of a proxied server and an optional URI to which a location should be mapped. As a protocol, “http” or “https” can be specified. The address can be specified as a domain name or IP address, and an optional port:or as a UNIX-domain socket path specified after the word “unix” and enclosed in colons:Module ngx_http_proxy_module - nginx.org

  4. Sets the HTTP protocol version for proxying. By default, version 1.0 is used. Version 1.1 is recommended for use with keepalive connections and NTLM authentication.This directive appeared in version 1.1.4.Module ngx_http_proxy_module - nginx.org

  5. このブール値を無効にすると、HTTP スクリプトおよびモジュールは、ネットワークまたはリモートポートへの接続を開始できなくなります。このアクセスを許可するには、このブール値を有効にします。13.3. ブール値 | Red Hat Product Documentation docs.redhat.com

  6. httpd を正引きまたはリバースプロキシーとして使用する場合は、このブール値を有効にします。13.3. ブール値 | Red Hat Product Documentation docs.redhat.com

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?