apt update -y && apt upgrade -y && apt install -y curl wget sudo socat
curl -fsSL https://get.docker.com | sh
创建nginx目录结构
mkdir -p /home/nginx
touch /home/nginx/nginx.conf
mkdir -p /home/nginx/certs
申请证书
curl https://get.acme.sh | sh
~/.acme.sh/acme.sh --register-account -m [email protected]
~/.acme.sh/acme.sh --issue -d weihangapi.cn --standalone
下载证书
~/.acme.sh/acme.sh --installcert -d weihangapi.cn --key-file /home/nginx/certs/key.pem --fullchain-file /home/nginx/certs/cert.pem
进入目录编辑文件
cd /home/nginx/ && nano nginx.conf
重定向配置,跳转到域名
events {
worker_connections 1024;
}
http {
server {
listen 80;
server_name a.weihangapi.cn;
return 301 https://www.baidu.com$request_uri;
}
server {
listen 443 ssl http2;
server_name a.weihangapi.cn;
ssl_certificate /etc/nginx/certs/cert.pem;
ssl_certificate_key /etc/nginx/certs/key.pem;
return 301 https://www.baidu.com$request_uri;
}
}
反向代理配置,代理指定IP加端口
events {
worker_connections 1024;
}
http {
client_max_body_size 1000m;
server {
listen 80;
server_name b.weihangapi.cn;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
server_name b.weihangapi.cn;
ssl_certificate /etc/nginx/certs/cert.pem;
ssl_certificate_key /etc/nginx/certs/key.pem;
location / {
proxy_pass http://127.0.0.1:5212;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
代理静态网页的配置方式
events {
worker_connections 1024;
}
server {
listen 80;
server_name c.weihangapi.cn;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
server_name c.weihangapi.cn;
ssl_certificate /etc/nginx/certs/cert.pem;
ssl_certificate_key /etc/nginx/certs/key.pem;
charset utf-8; # 添加这行来指定编码
location / {
root /usr/share/nginx/html;
index index.html;
}
}
部署容器
docker run -d --name nginx -p 80:80 -p 443:443 -v /home/nginx/nginx.conf:/etc/nginx/nginx.conf -v /home/nginx/certs:/etc/nginx/certs -v /home/nginx/html:/usr/share/nginx/html nginx:latest
docker ps -a
docker update --restart=always nginx