nginx docker容器配置https(ssl)

證書生成

首先須要有https的證書文件,若是你已經向證書受權中心購買了證書,能夠跳過這步,這裏介紹如何生成自簽名證書,自簽名證書是指不是證書受權中心(Certificate Authority)頒發的證書,而是在我的計算機上經過相關工具本身生成的證書,通常用於測試,不可用於生產環境。html

爲了方便管理證書(證書生成過程當中會產生不少文件),咱們能夠單首創建一個目錄用於存放證書文件,下面是經過openssl工具生成證書的過程。nginx

1. 建立目錄

$ cd ~
$ mkdir ssl
$ cd ssl

2. 建立祕鑰文件

建立祕鑰文件definesys.key,名稱能夠自定義,須要指定密碼(隨意密碼便可)git

$ openssl genrsa -des3 -out definesys.key 1024
Generating RSA private key, 1024 bit long modulus
.......++++++
..................++++++
e is 65537 (0x10001)
Enter pass phrase for definesys.key:
Verifying - Enter pass phrase for definesys.key:

3. 建立csr證書

須要輸入相關信息,比較重要的是Common Name,這個是訪問nginx的地址docker

$ openssl req -new -key definesys.key -out definesys.csr

Enter pass phrase for definesys.key:
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) [AU]:CN
State or Province Name (full name) [Some-State]:Shanghai
Locality Name (eg, city) []:Shanghai
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Definesys
Organizational Unit Name (eg, section) []:Definesys
Common Name (e.g. server FQDN or YOUR name) []:www.definesys.com
Email Address []:jianfeng.zheng@definesys.com

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

#此時文件
$  ssl ll
total 16
-rw-r--r--   1 asan  staff  733  1  3 23:57 definesys.csr
-rw-r--r--   1 asan  staff  963  1  3 23:55 definesys.key

4. 去除祕鑰密碼

nginx使用私鑰時須要去除密碼,執行如下命令時須要輸入祕鑰的密碼shell

$ cp definesys.key definesys.key.bak
$ openssl rsa -in definesys.key.bak -out definesys.key

Enter pass phrase for definesys.key.bak:
writing RSA key

5. 生成crt證書

$ openssl x509 -req -days 3650 -in definesys.csr -signkey definesys.key -out definesys.crt

Signature ok
subject=/C=CN/ST=Shanghai/L=Shanghai/O=Definesys/OU=Definesys/CN=www.definesys.com/emailAddress=jianfeng.zheng@definesys.com
Getting Private key

#此時文件列表

$  ssl ll
total 32
-rw-r--r--   1 asan  staff  1017  1  4 00:03 definesys.crt
-rw-r--r--   1 asan  staff   733  1  3 23:57 definesys.csr
-rw-r--r--   1 asan  staff   887  1  4 00:02 definesys.key
-rw-r--r--   1 asan  staff   963  1  4 00:01 definesys.key.bak

nginx容器配置

1. 證書文件上傳

definesys.crt文件和definesys.key文件拷貝到服務器上,假設你服務器上nginx的配置文件在/etc/nginx/目錄下,能夠在該目錄下建立一個文件夾,這裏命名certs,將文件拷貝至該文件夾下。安全

2. 配置文件修改

修改配置文件nginx.conf服務器

server {
    listen       443 ssl;
    server_name  www.definesys.com;

    ssl_certificate      /etc/nginx/certs/definesys.crt;
    ssl_certificate_key  /etc/nginx/certs/definesys.key;
    
    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;
    
    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;

    location / {
    root /usr/share/nginx/html;
    index  index.html index.htm;
    }
}

若是server配置不在nginx.conf文件上,能夠在conf.d文件夾下找.conf後綴的文件,通常有個default.conf文件。session

3. 啓動容器

docker run -d --restart=unless-stopped -p 443:443 -v /etc/nginx/:/etc/nginx -v /var/run/docker.sock:/tmp/docker.sock:ro -v /u01/application:/usr/share/nginx/html nginx

訪問https://localhost驗證配置是否正確,若是可以正常訪問說明配置成功,因爲是自簽名證書,打開時會提示證書不安全,忽略便可。app

相關文章
相關標籤/搜索