docker部署nginx配置阿里云证书实现https访问
1、申请阿里云免费证书
2、nginx.conf配置
2.1、nginx宿主机目录结构
注意:
conf:配置文件及证书所在位置
conf.d:https配置信息
html:静态文件所造目录
logs:nginx日志信息
2.2、将下载下来的证书文件放入到宿主机conf/目录下
2.3、nginx.conf内容
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
2.4、conf.d/default.conf内容
server {
listen 80;
listen [::]:80;
charset 'utf-8';
server_name 域名;
rewrite ^(.*) https://$server_name$1 permanent;
# 404页面跳转
error_page 404 /404.html;
location = /404.html{
root /usr/share/nginx/html;
}
}
server {
listen 443 ssl;
server_name 域名;
# 注意证书文件名字和位置,是从/etc/nginx/下开始算起的
ssl_certificate /etc/nginx/xxx.pem;
ssl_certificate_key /etc/nginx/xxx.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
#access_log /var/log/nginx/host.access.log main;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
root /usr/share/nginx/html;
index index.html index.htm;
}
# 404页面跳转
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
# 500页面跳转
# 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;
#}
}
3、启动nginx
docker run -d --restart=always
-p 80:80 -p 443:443
--name nginx80
-v /home/achen/nginx/conf/nginx.conf:/etc/nginx/nginx.conf
-v /home/achen/nginx/html/dist/:/usr/share/nginx/html
-v /home/achen/nginx/conf/:/etc/nginx
-v /home/achen/nginx/conf.d:/etc/nginx/conf.d
-v /home/achen/nginx/logs:/var/log/nginx
nginx:1.19.10
4、访问页面即可
https://域名