用Docker创建Nginx,自动申请免费的域名证书,并且配置重定向或反向代理
更新环境
apt update -y && apt upgrade -y && apt install -y curl wget sudo socat
安装 Docker
curl -fsSL https://get.docker.com | sh
搭建nginx
创建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 kjlion.gq --standalone
下载证书
~/.acme.sh/acme.sh --installcert -d kjlion.gq --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.kjlion.gq;
return 301 https://www.baidu.com$request_uri;
}
server {
listen 443 ssl http2;
server_name a.kjlion.gq;
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.kjlion.gq;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
server_name b.kjlion.gq;
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.kjlion.gq;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
server_name c.kjlion.gq;
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
Docker常用命令
https://kejilion.blogspot.com/2023/02/docker.html
版权声明:
作者:KEJILION
链接:https://blog.kejilion.pro/nginx01/
来源:科技lion官方博客【国内版】
文章版权归作者所有,未经允许请勿转载。
共有 0 条评论