LoginSignup
12
12

More than 5 years have passed since last update.

Nginxで同じサーバーに複数のIP address用の証明書を配置する

Posted at

Nginxで同じサーバーに複数のIP address用の証明書を配置する

1つのサーバーに対しlocal addressとglobal addressを割り振り、
どちらもHTTPSでアクセスしたい場合の設定です。

例:

local address:xxx.xxx.xxx.xxx
global address:yyy.yyy.yyy.yyy

1.まずxxx.xxx.xxx.xxx用とyyy.yyy.yyy.yyy用の証明書を配置します。

yyy.yyy.yyy.yyy用

/etc/nginx/ssl_certfile/key_yyy_yyy_yyy_yyy/server_pem.crt
/etc/nginx/ssl_certfile/key_yyy_yyy_yyy_yyy/server_pem.key

xxx.xxx.xxx.xxx用

/etc/nginx/ssl_certfile/key_xxx_xxx_xxx_xxx/server_pem.crt
/etc/nginx/ssl_certfile/key_xxx_xxx_xxx_xxx/server_pem.key

2.次のように両アドレス用の設定を作成します。
(冗長な書き方になっていますが)

sample_app.conf

upstream sample_app {
  server unix:/var/www/sample_app/tmp/sockets/unicorn.sock;
}

server {
  listen       yyy.yyy.yyy.yyy:443;
  server_name  sample_app;

  ssl                 on;
  ssl_certificate     /etc/nginx/ssl_certfile/key_yyy_yyy_yyy_yyy/server_pem.crt;
  ssl_certificate_key /etc/nginx/ssl_certfile/key_yyy_yyy_yyy_yyy/server_pem.key;
  ssl_protocols       SSLv3 TLSv1;
  ssl_ciphers         HIGH:!ADH:!MD5;

  location / {
    try_files $uri $uri/index.html $uri.html @sample_app;
    client_max_body_size 2M;
  }

  location @sample_app {
    proxy_read_timeout 30;
    proxy_connect_timeout 30;
    proxy_redirect     off;
    proxy_set_header   X-Forwarded-Proto https;
    proxy_set_header   Host              $http_host;
    proxy_set_header   X-Real-IP         $remote_addr;

    proxy_pass http://sample_app;

    proxy_send_timeout 30;
  }
}

server {
  listen       xxx.xxx.xxx.xxx:443;
  server_name  sample_app;

  ssl                 on;
  ssl_certificate     /etc/nginx/ssl_certfile/key_xxx_xxx_xxx_xxx/server_pem.crt;
  ssl_certificate_key /etc/nginx/ssl_certfile/key_xxx_xxx_xxx_xxx/server_pem.key;
  ssl_protocols       SSLv3 TLSv1;
  ssl_ciphers         HIGH:!ADH:!MD5;

  location / {
    try_files $uri $uri/index.html $uri.html @sample_app;
    client_max_body_size 2M;
  }

  location @sample_app {
    proxy_read_timeout 30;
    proxy_connect_timeout 30;
    proxy_redirect     off;
    proxy_set_header   X-Forwarded-Proto https;
    proxy_set_header   Host              $http_host;
    proxy_set_header   X-Real-IP         $remote_addr;

    proxy_pass http://sample_app;

    proxy_send_timeout 30;
  }

3.Nginxがglobal addressをbindできるようにします

/etc/sysctl.conf
に次の設定を追加します。

net.ipv4.ip_nonlocal_bind = 1

システムに設定を反映します。

$ sysctl -p /etc/sysctl.conf

※設定がない場合、Nginxを起動する時に次のようなエラーが出ます。

$ sudo /sbin/service nginx start
Starting nginx: [emerg]: bind() to yyy.yyy.yyy.yyy failed (99: Cannot assign requested address)
12
12
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
12
12