8
6

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

GitLabAdvent Calendar 2017

Day 6

既存のNginxを使用しつつLet’s EncryptのSSLを使用してGitLabへアクセスできるようにする

Last updated at Posted at 2017-12-06

変更

2018/11/26 - CentOS 7.5 & 11.5.0 (b7b1e8e)に対応させました

ゴール

既存のNginxを使用しつつLet’s EncryptのSSLを使用してGitLabへアクセスできるようにする

環境

ConoHaでCentOS 7.5
GitLabは、11.5.0 (b7b1e8e)を使用

注意

gitlab.example.comは、適時、自分のドメインに読み替えてください
これは、備忘録です。詳しい説明は、殆どありません。設定を列挙している程度だと思ってください

インストール

GitLabインストール

通常通りGitLabをhttps://about.gitlab.com/installation/#centos-7を参考にインストールするが、Postfixは、CentOS 7に標準でインストールされているのでインストールを省いた。
注意: インストールの項目でgitlab-eeと書かれているが、CEをインストールしたいので全てgitlab-ceとしてインストールしました

GitLabの設定 ~その壱~

内蔵のNginxを使用しないように設定する

/etc/gitlab/gitlab.rb
nginx['enable'] = false
web_server['external_users'] = ['nginx']

設定を反映させる

sudo gitlab-ctl reconfigure

Nginx設定 ~その壱~

とりあえず、httpでアクセスできるNginx設定ファイルを作る

/etc/nginx/conf.d/gitlab.conf
upstream gitlab-workhorse {
   server unix:/var/opt/gitlab/gitlab-workhorse/socket;
}

server {
   listen 80;
   server_name gitlab.example.com;

   access_log /var/log/nginx/gitlab.access.log;
   error_log /var/log/nginx/gitlab.error.log;

   location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_pass http://gitlab-workhorse;
   }

   location ~ ^/(assets)/ {
      root /opt/gitlab/embedded/service/gitlab-rails/public;
      gzip_static on;
      expires max;
      add_header Cache-Control public;
   }

   location ^~ /.well-known {
      alias /var/letsencrypt/gitlab/.well-known;
   }
}

Let’s Encrypt用にディレクトリ作成とNginxのConfigチェック

sudo mkdir /var/letsencrypt/
sudo mkdir /var/letsencrypt/gitlab/
sudo nginx -t

以下の様に出れば成功

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Nginxの設定をリロード

sudo systemctl reload nginx.service

Let’s Encryptで証明書取得

詳しい説明はこちら

sudo certbot certonly --webroot -w /var/letsencrypt/gitlab -d gitlab.example.com

Nginx設定 ~その弐~

SSL関係の設定を追加

/etc/nginx/conf.d/gitlab.conf
upstream gitlab-workhorse {
   server unix:/var/opt/gitlab/gitlab-workhorse/socket;
}

server {
    listen 80;
    server_name gitlab.example.com;
    return 301 https://$host$request_uri;
}

server {
   listen 443 ssl http2;
   server_name gitlab.example.com;

   access_log /var/log/nginx/gitlab.access.log;
   error_log /var/log/nginx/gitlab.error.log;

   ssl_certificate /etc/letsencrypt/live/gitlab.example.com/fullchain.pem;
   ssl_certificate_key /etc/letsencrypt/live/gitlab.example.com/privkey.pem;
   ssl_trusted_certificate /etc/letsencrypt/live/gitlab.example.com/fullchain.pem;

   ssl_session_timeout 1d;
   ssl_session_cache shared:SSL:50m;
   ssl_session_tickets on;

   ssl_dhparam /etc/ssl/private/dhparam.pem;

   ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
   ssl_ciphers "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128:AES256:AES:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK";
   ssl_prefer_server_ciphers on;

   add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains;';

   location / {
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-Proto https;
      proxy_set_header X-Forwarded-Host $host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

      proxy_pass http://gitlab-workhorse;
      proxy_redirect http:// https://;
   }

   location ~ ^/(assets)/ {
      root /opt/gitlab/embedded/service/gitlab-rails/public;
      gzip_static on;
      expires max;
      add_header Cache-Control public;
   }

   location ^~ /.well-known {
      alias /var/letsencrypt/gitlab/.well-known;
   }

}

再度、Configのチェック

sudo nginx -t

以下の様に出れば成功

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Nginxの設定をリロード

sudo systemctl reload nginx.service

GitLabの設定 ~その弐~

GitLabのexternal_urlを書き換える

external_url 'https://gitlab.example.com'

設定を反映させる

sudo gitlab-ctl reconfigure

おまけ : CronでLet’s Encryptの自動更新

Let’s Encryptの自動更新を設定する
自動更新したあとNginxをreloadしないと証明書でエラーが発生するためreloadを入れる

crontab -e
0 0 * * * /bin/certbot renew && /bin/systemctl reload nginx.service
8
6
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
8
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?