赞
踩
使用nginx可以快速搭建http的服务,结合自签名证书则能很容易地提供https服务,这篇文章以容器方式介绍如何快速搭建nginx的https服务。
启动nginx服务,在8443端口映射443的https服务,启动容器提供服务。
liumiaocn:nginx liumiao$ docker run -d -p 8443:443 --name=nginx nginx:latest
db67c95583d6b4e889680d08d9e4473bb3b961a1a01cdde5fb39f5967a27b808
liumiaocn:nginx liumiao$ docker ps |grep nginx
db67c95583d6 nginx:latest "nginx -g 'daemon of…" 9 seconds ago Up 8 seconds 80/tcp, 0.0.0.0:8443->443/tcp nginx
liumiaocn:nginx liumiao$
进入到镜像之中,进行如下nginx配置
nginx缺省配置文件路径:/etc/nginx/conf.d/default.conf
liumiaocn:nginx liumiao$ docker exec -it nginx sh
# cd /etc/nginx/conf.d
# ls
default.conf
#
cat >>default.conf <<EOF server { listen 443 ssl http2; server_name localhost; ssl_certificate /etc/nginx/ssl/server.crt; ssl_certificate_key /etc/nginx/ssl/server.key; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; location / { root /usr/share/nginx/html; index index.html index.htm; } } EOF
执行日志如下所示:
# cat >>default.conf <<EOF server { listen 443 ssl http2; server_name localhost; ssl_certificate /etc/nginx/ssl/server.crt; ssl_certificate_key /etc/nginx/ssl/server.key; ssl_session_timeout 5m; ssl_> ciphers HIGH:!aNULL:!MD5; ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; location / { root /usr/share/nginx/html; > index index.html index.htm; } } EOF> > > > > > > > > > > > > > > > > # cat default.conf server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } server { listen 443 ssl http2; server_name localhost; ssl_certificate /etc/nginx/ssl/server.crt; ssl_certificate_key /etc/nginx/ssl/server.key; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; location / { root /usr/share/nginx/html; index index.html index.htm; } } #
# pwd
/etc/nginx/conf.d
# cd ..
# mkdir ssl
#
其实非常简单,就是在nginx中添加ssl的配置,然后创建配置内容中指定的目录/etc/nginx/ssl
使用easypack上提供的一键证书生成,只需要提供CN即可生成所需要的服务器端私钥(server.key)和服务器端证书文件(server.crt)。使用如下脚本即可
这里假设CN为www.hellohttps.com为例进行生成
liumiaocn:nginx liumiao$ export ENV_NAME_DN_CN=www.hellohttps.com liumiaocn:nginx liumiao$ ls create_https_certs.sh liumiaocn:nginx liumiao$ sh create_https_certs.sh ## Prepare for DN and v3 extension setting files ## Create CA private key with name : ca.key Generating RSA private key, 2048 bit long modulus .............................................+++ ...............+++ e is 65537 (0x10001) ## Create CA certificate with name : ca.crt ## Create server private key with name : server.key Generating RSA private key, 2048 bit long modulus ..+++ .................+++ e is 65537 (0x10001) ## Create server CSR file with name : server.csr ## Create server certificate with name : server.crt Signature ok subject=/C=CN/ST=LiaoNing/L=DaLian/O=devops/OU=unicorn/CN=www.hellohttps.com Getting CA Private Key liumiaocn:nginx liumiao$ ls ca.crt ca.srl server.crt server.key ca.key create_https_certs.sh server.csr v3_extfile.conf liumiaocn:nginx liumiao$
设置指定名称的证书与私钥拷贝至配置文件中指定的目录/etc/nginx/ssl下
liumiaocn:nginx liumiao$ docker cp server.key nginx:/etc/nginx/ssl
liumiaocn:nginx liumiao$ docker cp server.crt nginx:/etc/nginx/ssl
liumiaocn:nginx liumiao$ docker exec nginx ls /etc/nginx/ssl
server.crt
server.key
liumiaocn:nginx liumiao$
liumiaocn:nginx liumiao$ docker restart nginx
nginx
liumiaocn:nginx liumiao$ docker ps |grep nginx
db67c95583d6 nginx:latest "nginx -g 'daemon of…" 20 minutes ago Up 4 seconds 80/tcp, 0.0.0.0:8443->443/tcp nginx
liumiaocn:nginx liumiao$
本文使用容器进行示例,直接在本机验证,此处设定/etc/hosts即可,添加如下内容
liumiaocn:nginx liumiao$ sudo vi /etc/hosts
Password:
liumiaocn:nginx liumiao$ grep hello /etc/hosts
127.0.0.1 www.hellohttps.com
liumiaocn:nginx liumiao$
添加证书之后并设定Trust
保存之后即可显示正常
使用https://www.hellohttps.com:8443/访问,可以看到如下页面信息,说明nginx的https服务已经正常可用。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。