#
ドキュメント

Document

自分のための備忘録です。

リバースプロキシー

ref. https://httpd.apache.org/docs/2.4/urlmapping.html#proxy

リバースプロキシ Apache は遠隔地にあるドキュメントをローカルのサーバの URL 空間に 持ってくることもできます。この手法はリバースプロキシと呼ばれています。 ウェブサーバが遠隔地のドキュメントを取得してクライアントに送り返すのが プロキシサーバの動作のように見えるからです。クライアントにはドキュメントが リバースプロキシサーバから送られてきているように見える点が通常の プロキシとは異なります。

次の例では、クライアントが /foo/ ディレクトリの下にある ドキュメントをリクエストすると、サーバが internal.example.com の /bar/ ディレクトリから取得して、さもローカルサーバからの ドキュメントのようにしてクライアントに返します。

ProxyPass /foo/ http://internal.example.com/bar/ ProxyPassReverse /foo/ http://internal.example.com/bar/ ProxyPassReverseCookieDomain internal.example.com public.example.com ProxyPassReverseCookiePath /foo/ /bar/

ProxyPass ディレクティブは サーバが適切なドキュメントを取得するように設定し、 ProxyPassReverse ディレクティブは internal.example.com からのリダイレクトがローカルサーバの 適切なディレクトリを指すように書き換えます。 同様に ProxyPassReverseCookieDomain と ProxyPassReverseCookiePath でバックエンド側サーバの発行した Cookie を書き換えることができます。

ただし、ドキュメントの中のリンクは書き換えられない、 ということは知っておいてください。 ですから、internal.example.com への絶対パスによるリンクでは、 クライアントがプロキシサーバを抜け出して internal.example.com に 直接リクエストを送る、ということになります。 サードパーティ製モジュールの mod_proxy_html は、HTML と XHTML 中のリンクを書き換えることができます。

ディレクティブ

  • ProxyPass
  • ProxyPassReverse

https://cacoo.com/diagrams/90I5WWkeyYDqAiMs/0F15E

ProxyPassReverse

Ubuntu 設定

Apacheインストール(Dockerでインストール)

$ docker run -d -p 8080:80 --name my-app -v "$PWD":/var/www/html php:7.2-apache

SSHログイン

$ docker exec -it my-app /bin/bash

Apacheバージョン確認

apache2 -v
Server version: Apache/2.4.38 (Debian)
Server built:   2020-08-25T20:08:29

リバースプロキシー設定

  • apt-get update
  1. mod_proxyproxy_httpを有効化
  2. /etc/apache2/mods-availables/proxy.confに設定
  3. $ apache2ctl configtest
  4. $ service apache2 restart

$ service apache2 restartを実行したら、毎回なぜかコンテナが停止するので $ docker startで再開。

http://localhost:8080にアクセスするとhttp://example.comの内容が表示された。

mod_proxyproxy_httpを有効化

mod_proxyはデフォルトで有効なのでproxy_httpを有効化。

$ a2enmod proxy_http

proxy.confに設定追加

        # If you want to use apache2 as a forward proxy, uncomment the
        # 'ProxyRequests On' line and the <Proxy *> block below.
        # WARNING: Be careful to restrict access inside the <Proxy *> block.
        # Open proxy servers are dangerous both to your network and to the
        # Internet at large.
        #
        # If you only want to use apache2 as a reverse proxy/gateway in
        # front of some web application server, you DON'T need
        # 'ProxyRequests On'.

        #ProxyRequests On
        #<Proxy *>
        #   AddDefaultCharset off
        #   Require all denied
        #   #Require local
        #</Proxy>

        # Enable/disable the handling of HTTP/1.1 "Via:" headers.
        # ("Full" adds the server version; "Block" removes all outgoing Via: headers)
        # Set to one of: Off | On | Full | Block
        #ProxyVia Off
ProxyRequests Off
    <Proxy *>
        Require all granted
    </Proxy>
    ProxyPass / http://example.com/
    ProxyPassReverse / http://example.com/

</IfModule>

上記のように記載して保存すると自動で先頭に<IfModule mod_proxy.c>が追加されるよう?

ref. https://www.server-world.info/query?os=Ubuntu_16.04&p=httpd2&f=5