2
2

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 3 years have passed since last update.

haproxyにSSL証明書を設置

Last updated at Posted at 2020-03-11

概要

haproxyにSSL証明書を設置する方法。

環境

  • HA-Proxy version 1.5.18 2016/05/10

証明書を用意する

オレオレ証明書を使う場合

keyファイル(秘密鍵)を作成

$ openssl genrsa 2048 > server.key

csr(証明書署名要求)を作成。入力項目は全て空欄でEnterで問題ない。

$ openssl req -new -key server.key > server.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:
State or Province Name (full name) []:
Locality Name (eg, city) [Default City]:
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

crt(拡張子をcerにすることもある)(サーバー証明書)を作成

$ openssl x509 -days 730 -req -signkey server.key < server.csr > server.crt

pemファイルを用意

haproxyでは、SSL証明書はpemファイルにする必要がある。
crtファイルとkeyファイルを結合して拡張子pemとして1つのファイルにするが、以下の順番になっている必要がある。

SSL証明書 -> 中間証明書(ある場合) -> 秘密鍵

$ cat server.crt server.cer server.key > server.pem

すると以下のようなpemファイルが出来上がる。(中間証明書がない場合はCERTIFICATEは1つのみになる)

-----BEGIN CERTIFICATE-----
xxxxxxxxxxxxxxxxxxxxxxxxxxx
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
xxxxxxxxxxxxxxxxxxxxxxxxxxx
-----END CERTIFICATE-----
-----BEGIN RSA PRIVATE KEY-----
xxxxxxxxxxxxxxxxxxxxxxxxxxx
-----END RSA PRIVATE KEY-----

server.pemは秘密鍵情報を含むので、chmodで適切な権限にすること。

$ sudo chmod 400 server.pem

設定ファイルを編集

/etc/haproxy/haproxy.cfg

以下の箇所にpemファイルのパスを設定する。

# http -> https転送
frontend http-in
    bind *:80
    redirect scheme https code 301 if !{ ssl_fc }

# SSL設定
frontend main
    bind    *:443 ssl crt /etc/haproxy/server.pem
    mode    http
    option forwardfor
    reqadd X-Forwarded-Proto:\ https
    default_backend  app

haproxyを再起動

$ sudo systemctl restart haproxy

参考

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?