1.首先安装nginx镜像,主要是复制里面的config配置文件
docker pull nginx:latest
安装好可以选择测试(也可以跳过):测试命令两行
docker run --name nginx-test -p 8080:80 -d nginx
访问IP地址测试
2.将nginx关键目录映射到本机
首先在本机创建nginx的一些文件存储目录,根据需求增删
mkdir -p /usr/local/nginx/www /usr/local/nginx/logs /usr/local/nginx/conf.d /usr/local/nginx/html
- www: nginx存储网站网页的目录
- logs: nginx日志目录
- conf.d Nginx配置文件
docker ps -a #查看容器id
#停止nginx-test服务 docker stop nginx-test
将nginx-test容器配置文件copy到本地
docker cp 容器id:/etc/nginx/nginx.conf /usr/local/nginx/ docker cp 容器id:/etc/nginx/conf.d /usr/local/nginx/ docker cp 容器id:/usr/share/nginx/html /usr/local/nginx
#删除nginx-test服务 docker rm nginx-test
3. 创建容器
创建新nginx容器nginx-web,并将www,logs,conf目录映射到本地
docker run -d --name nginx --restart always -p 8080:80 -v /usr/local/nginx/nginx.conf:/etc/nginx/nginx.conf -v /usr/local/nginx/conf.d:/etc/nginx/conf.d -v /usr/local/nginx/html:/usr/share/nginx/html -v /usr/local/nginx/logs:/var/log/nginx nginx #如果需要安装https docker run -d --name nginx --restart always -p 443:443 -v /usr/local/nginx/nginx.conf:/etc/nginx/nginx.conf -v /usr/local/nginx/conf.d:/etc/nginx/conf.d -v /usr/local/nginx/html:/usr/share/nginx/html -v /usr/local/nginx/ssl:/ssl/:rw\ -v /usr/local/nginx/logs:/var/log/nginx nginx
4. 设置反向代理
进入到/usr/local/conf/nginx.conf 添加如下即可
server{
listen 80;
charset utf-8;
server_name 192.168.112.135;
location / {
proxy_pass http://192.168.112.135:8080;
proxy_redirect default;
}
}
如果需要https:
server {
listen 443 ssl;
server_name EVCharger.topdon.com; #域名
# 增加ssl
ssl_certificate /ssl/8148077_evcharger.topdon.com.pem;
ssl_certificate_key /ssl/8148077_evcharger.topdon.com.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
# 指定密码为openssl支持的格式
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
# 密码加密方式
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
location ~^/access/(.*)$ {
proxy_pass http://172.16.50.112:9091/$1;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_read_timeout 3600s;
proxy_connect_timeout 86400;
proxy_ignore_client_abort on;
}
原文链接:https://blog.csdn.net/sunjavateam/article/details/124634461