0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

MailHog をリバースプロキシに通したら Error during WebSocket handshake: Unexpected response code: 400 と怒られたときに読むページ

Last updated at Posted at 2020-02-10

私はメール送信機能を持つアプリの開発時にダミーSMTPサーバとして Mailhog を利用しています。シングルバイナリながらも Web UI まで備える等お手軽・便利な点が気に入っていて、ISO-2022-JP を文字化けさせないための PR をマージして生成したバイナリを配布しているくらいです。

そんな Mailhog ですが、Web UI をリバプロ経由で利用したい場合には WebSocket が貫通するように調整する必要があります。

debian 用 Apache の場合

2つのモジュールを有効化する必要があります。HTTPのリバースプロキシを担当する proxy_http と WebSocket のリバースプロキシを担当する proxy_wstunnel です:

a2enmod proxy_http proxy_wstunnel

次に、ProxyPass を設定します。apache2.confsites-available/000-default.conf など、状況に応じて適切なものを選択してください。ここでは後者を利用します。

000-default.conf
<VirtualHost *:80>

    # 中略

    ProxyPass "/mailhog/api/v2/websocket" ws://localhost:8025/api/v2/websocket
    ProxyPassReverse "/mailhog/api/v2/websocket" ws://localhost:8025/api/v2/websocket

    ProxyPass "/mailhog/" http://localhost:8025/
    ProxyPassReverse "/mailhog/"  http://localhost:8025/
</VirtualHost>

ポイントは、設定を記載する順番です。制限が厳しい順に記すルールとなっていますので、アプリ本体より API のエンドポイントを先に記載しなければなりません。エンドポイントの URL は ws://localhost:8025/api/v2/websocket/ でないので注意しましょう。

おまけ:Dockerfile

FROM debian:latest

USER root
RUN apt update
RUN apt install -y wget apache2
RUN a2enmod proxy_http proxy_wstunnel
RUN mkdir /opt/mailhog \
 && cd /opt/mailhog \
 && wget https://github.com/mailhog/MailHog/releases/download/v1.0.0/MailHog_linux_amd64
RUN chmod +x /opt/MailHog_linux_amd64
ADD 000-default.conf /etc/apache2/sites-available/
EXPOSE 80

ADD run.sh /bin/run.sh
CMD ["sh","/bin/run.sh"]
run.sh
/etc/init.d/apache2 start
/opt/mailhog/MailHog_linux_amd64

参考リンク

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?