局域网部署时实现基于https的业务系统访问

局域网部署时实现基于https的业务系统访问

最近在做项目集成时,门户的系统因为客户安全要求,启动了https的访问,导致我们的系统无法正常访问。简单测试了一下如何在企业内网,如何将自己的Web应用部署为https的访问。

证书管理

安装mkcert

复制代码
sudo apt install libnss3-tools -y
curl -L https://github.com/FiloSottile/mkcert/releases/download/v1.4.4/mkcert-v1.4.4-linux-amd64 -o mkcert
chmod +x mkcert
sudo mv mkcert /usr/local/bin/

mkcert -install

制作证书

复制代码
mkcert localhost 127.0.0.1 192.168.2.62

演示如下

复制代码
(base) jihui@rz-62-exchange:~$ mkcert localhost 127.0.0.1 192.168.2.62

Created a new certificate valid for the following names 📜
 - "localhost"
 - "127.0.0.1"
 - "192.168.2.62"

The certificate is at "./localhost+2.pem" and the key at "./localhost+2-key.pem" ✅

It will expire on 27 April 2028 🗓

(base) jihui@rz-62-exchange:~$ 

制作好的证书,在当前目录下。

至此,证书准备完成。

网站部署

演示网站的结构如下:

复制代码
(base) jihui@rz-62-exchange:~/deploy$ tree
.
├── conf
│   ├── cert
│   │   ├── localhost+2-key.pem
│   │   └── localhost+2.pem
│   └── nginx
│       └── default.conf
├── data
│   └── web
│       └── index.html
└── docker-compose.yaml

6 directories, 5 files
(base) jihui@rz-62-exchange:~/deploy$ 

配置网站文件

为了演示,网站只有一个文件index.html,内容如下:

复制代码
$ cat index.html 

Hello world.

准备nginx配置文件

  • 使用https,需要监听443端口,server_name使用生成证书时的服务器地址。

  • 需要配置ssl_certificatessl_certificate_key,两个参数的值为上面证书的文件地址。这个地址是容器内容的地址。

    (base) jihui@rz-62-exchange:~/deploy/conf/nginx cat default.conf map http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
    }

    server {
    # listen 80;
    listen 443 ssl;
    server_name localhost 127.0.0.1 192.168.2.62;

    复制代码
      ssl_certificate /etc/nginx/cert/localhost+2.pem;
      ssl_certificate_key /etc/nginx/cert/localhost+2-key.pem;
    
      error_page 500 502 503 504  /50x.html;
    
      location = /50x.html {
          root   /usr/share/nginx/html;
      }
      
      location / {
          root /web;
          index index.html;
          try_files $uri $uri/ =404;
      }

    }
    (base) jihui@rz-62-exchange:~/deploy/conf/nginx$

配置docker-compose.yaml

在这个文件中,需要配置如下:

  • 监听端口为443,这个端口和default.conf中的端口要对应。

  • 需要将宿主机上的证书目录,映射到容器的/etc/nginx/cert/目录。

  • 将宿主机上的网站文件,映射到容器的/web。

    (base) jihui@rz-62-exchange:~/deploy$ cat docker-compose.yaml
    services:

    复制代码
    web:
      image: nginx:1.28
      container_name: web
      restart: unless-stopped
      ports:
        # - 8989:80
        - 8989:443
      volumes:
        - ./conf/nginx/:/etc/nginx/conf.d/
        - ./conf/cert/:/etc/nginx/cert/
        - ./data/web:/web

    (base) jihui@rz-62-exchange:~/deploy$

测试

使用浏览器测试

使用curl命令测试

复制代码
(base) user@rz-61-exchange:~$ curl https://192.168.2.62:8989/
curl: (60) SSL certificate problem: unable to get local issuer certificate
More details here: https://curl.se/docs/sslcerts.html

curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the web page mentioned above.
(base) user@rz-61-exchange:~$ 

直接访问会失败。

复制代码
(base) user@rz-61-exchange:~$ curl -k https://192.168.2.62:8989/
Hello world.
(base) user@rz-61-exchange:~$ 

复制代码
(base) user@rz-61-exchange:~$ curl --insecure https://192.168.2.62:8989/
Hello world.
(base) user@rz-61-exchange:~$ 
相关推荐
q5673152335 分钟前
人工智能训练数据采集:稳定代理IP高并发方案全解析
人工智能·爬虫·网络协议·tcp/ip·代理模式·代理ip
云栖梦泽在6 小时前
稳如狗网络工具箱:IP、DNS、WebRTC、测速、连通性和全球延迟检测工具简明介绍
网络·网络协议·tcp/ip·网络安全·性能优化·webrtc
她说可以呀9 小时前
WebSocket 和 SSE 简单使用及区别
网络·websocket·网络协议
tiantianuser10 小时前
NVME-oF IP 设计11 : 控制面与数据面干什么用?
网络协议·rdma·高速传输·roce v2·nvme of
ShineWinsu10 小时前
对于Linux:自定义协议(基于TCP)实现网络计算器的解析
linux·网络·c++·网络协议·tcp/ip·面试·网络计算器
q5673152311 小时前
Scrapy 框架集成稳定 HTTP 代理:中间件配置与断线重试实战
爬虫·网络协议·scrapy·http·中间件·http代理
完美火龙篇 四月的友11 小时前
SpringBoot 即时聊天 IM 完整实现(HTTP会话管理 \+ WebSocket实时推送 \+ 离线消息)
spring boot·websocket·http
9527出列12 小时前
一次 HttpClient 连接池泄漏的完整排查与修复
http
Shell运维手记12 小时前
交换机(二层交换机)完整工作原理
运维·网络·网络协议·macos·交换机
星马梦缘12 小时前
如何内网穿透用手机远控电脑?
http·电脑·内网穿透·远程控制