CentOS 7.9 64位
1. 拉取Nginx镜像
docker pull nginx
2. 创建nginx挂载目录
mkdir nginx && cd $_ && mkdir -p {cert,conf,logs}
# cert 存放域名对应证书文件
# conf 存放nginx配置文件
3. 配置nginx.conf
cd conf
vim nginx.conf
nginx.conf
配置文件写入
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
client_max_body_size 100m;
include mime.types;
server {
listen 80;
server_name xxx.com;
rewrite ^(.*) https://$host$1;
location / {
index index.html index.htm;
}
}
server {
listen 443 ssl; # 1.1版本后这样写
server_name xxx.com; #填写绑定证书的域名
ssl_certificate /etc/nginx/cert/xxx.com.pem; # 指定证书的位置,绝对路径
ssl_certificate_key /etc/nginx/cert/xxx.com.key; # 指定证书key绝对路径
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
#自定义设置使用的TLS协议的类型以及加密套件(以下为配置示例,请您自行评估是否需要配置)
#TLS协议版本越高,HTTPS通信的安全性越高,但是相较于低版本TLS协议,高版本TLS协议对浏览器的兼容性较差。
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
#表示优先使用服务端加密套件。默认开启
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:1m;
fastcgi_param HTTPS on;
fastcgi_param HTTP_SCHEME https;
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;
try_files $uri $uri/ /index.html;
}
#location / {
# root html;
# index index.html index.htm;
#}
}
# 引入扩展配置(可以细分服务nginx)
# include /etc/nginx/conf.d/*.conf;
}
4. 运行启动nginx镜像
docker run --name nginx-web \
-p 443:443 \
-p 80:80
-v /nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /nginx/conf/cert:/etc/nginx/cert \
-v /nginx/logs:/var/log/nginx/ \
-d nginx
📍 启动完成后浏览器访问域名或者ip即可