docker镜像的nginx配置证书SSL,单独配置单个localtion使用证书,其他nginx配置不影响

docker镜像的nginx配置证书SSL,单独配置单个localtion使用证书,其他nginx配置不影响

原本的地址是http://test.dev.net/mobile

想要的效果是https://test.dev.net/mobile

但是其他地址还是http://test.dev.net/smart_mobile或者http://test.dev.net/不变

一、原本的nginx配置

javascript 复制代码
server {
    listen       80;
    server_name  test.dev.net;
    client_max_body_size 500m;
    #access_log  /var/log/nginx/host.access.log  main;
    
    location / {
           # 不缓存html,防止程序更新后缓存继续生效
      if ($request_filename ~* .*\.(?:htm|html)$) {
         add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
         access_log on;
      }
      root   /etc/nginx/vue_page/boyue/dist;
      try_files $uri $uri/ /index.html;
      index index.html index.htm;
   }
	location /smart_mobile {
             add_header 'Access-Control-Allow-Origin' '*';
                 add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                 alias    /etc/nginx/vue_page/boyue/smart_mobile;
                 try_files $uri $uri/ /boyue/index.html;
                 index   index.html;
        }

    location /mobile {
         add_header 'Access-Control-Allow-Origin' '*';
         add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
         alias  /etc/nginx/vue_page/boyue/mobile;
         try_files $uri $uri/ /boyue/index.html;
         index   index.html;
    }
}   

现在需要修改/mobile模块使用https来访问,但是不影响其他地址。

二、修改原本server块,下面再增加一个server块写ssl证书配置

javascript 复制代码
server {
    listen 443 ssl;
    server_name test.dev.net;

    # 全局 SSL 配置(必须)
    ssl_certificate /etc/nginx/ssl/fkp.pem;
    ssl_certificate_key /etc/nginx/ssl/fkp.key;

    # SSL 配置
    #ssl_protocols TLSv1.2 TLSv1.3;
    #ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384;
    #ssl_prefer_server_ciphers off;

    # 默认所有 443 端口的请求都需要 SSL

    # 只有这个 location 实际处理请求
    location /mobile {

        # 你的应用配置
         add_header 'Access-Control-Allow-Origin' '*';
         add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
         alias  /etc/nginx/vue_page/boyue/mobile;
         try_files $uri $uri/ /boyue/index.html;
         index   index.html;
    }

}

只要生成证书后配置证书地址即可。

实际配置中遇到的错误:因为用到了docker镜像配置nginx,导致出现端口只映射了80,未映射443端口,所以配置完效果未出现

原始nginx镜像:

最后增加了个新的nginx的docker镜像。为什么新加,因为不想影响旧nginx的运行,

新增的nginx镜像:

新增docker命令如下:主要映射nginx配置文件目录、ssl证书目录和前端包的目录

javascript 复制代码
docker run -d   
--name nginx-ssl   
-p 443:443   
-v /home/zxd/docker_mounts/nginx/nginx.conf:/etc/nginx/nginx.conf   
-v /home/zxd/docker_mounts/nginx/conf.d:/etc/nginx/conf.d   
-v /home/zxd/docker_mounts/nginx/ssl:/etc/nginx/ssl   
-v /home/zxd/docker_mounts/nginx/vue_page:/etc/nginx/vue_page   
nginx:latest

注意:ssl证书获取方式直接网上找就行,这里不做标注了

相关推荐
gs8014023 分钟前
告别 CI/CD 误伤与红条:Docker 镜像智能清理与优雅防冲突实战
ci/cd·docker·容器
wjcroom41 分钟前
FileBrowser的docker运行了改变密码长度的做法
运维·docker·容器
流光D1 小时前
AI Era: Building Web Sites and Configuring Nginx Reverse Proxy Process
前端·人工智能·nginx
瓦格哈儿1 小时前
删掉旧 SSL 证书会影响线上业务吗?风险点梳理
网络协议·https·ssl
dazhong20123 小时前
Docker 进阶篇(一) CentOS 7 离线升级 Docker 完整实战
docker·容器·centos
Lsetea4 小时前
Cloudflare报526 Invalid SSL certificate:源站证书与Nginx回源TLS排查
nginx·https·ssl证书·cloudflare·tls
雾隐隐o4 小时前
Docker Compose 多容器编排实战
运维·docker·容器
姚不倒4 小时前
负载均衡架构设计:F5 ↔ Nginx/HAProxy ↔ Keepalived 全链路映射
运维·网络·nginx
姚不倒6 小时前
Tengine 实战:主动健康检查 + 动态 Upstream 配置
运维·网络·nginx
梦之美丽生活6 小时前
【本地部署及docker部署】yolov8_detect
yolo·docker·容器