目录结构
sslTest
├── Dockerfile
├── docker-compose.yml
├── nginx
│ ├── conf
│ │ └── www_a5k3mo16_icu.conf
│ └── html
│ └── index.html
└── nodedemo
└── app.js
生成node服务器镜像
Dockerfile
FROM node
RUN mkdir -p /root/nodedemo
COPY ./nodedemo /root/nodedemo
WORKDIR /root/nodedemo
CMD ["node", "app.js"]
docker-compose.yml
version: '3'
services:
mynodejs:
restart: always
deploy:
replicas: 3
build:
context: ./
dockerfile: Dockerfile
mynginx:
image: nginx
restart: always
ports:
- 443:443
volumes:
- ./nginx/conf/:/etc/nginx/conf.d/
- ./nginx/html/:/usr/share/nginx/html/
deploy:
replicas: 3
resources:
limits:
cpus: "0.5"
memory: 500M
restart_policy:
condition: on-failure
nginx/conf/www_a5k3mo16_icu.conf
# 负载均衡
upstream whj {
ip_hash;
server mynodejs:8787 weight=1;
}
server {
listen 443 ssl;
server_name www.a5k3mo16.icu;
# SSL 配置
ssl_certificate /etc/nginx/conf.d/a5k3mo16.icu_1735989737/a5k3mo16.icu.crt;
ssl_certificate_key /etc/nginx/conf.d/a5k3mo16.icu_1735989737/a5k3mo16.icu.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE';
ssl_prefer_server_ciphers on;
# 静态文件服务(如果需要提供静态文件)
location /html/ {
root /usr/share/nginx;
index index.html index.htm;
}
# 负载均衡代理到 Node.js 服务
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_buffering off;
proxy_pass http://whj; # 代理到 Node.js 服务
}
}
nodedemo/app.js
const http = require('http');
// 创建服务器
const server = http.createServer((req, res) => {
if (req.method === 'GET' && req.url === '/') {
res.end('WELCOME HERE!');
}
if (req.method === 'GET' && req.url === '/user') {
// 设置响应头
res.writeHead(200, { 'Content-Type': 'text/html' });
// 返回 JSON 数据
res.end("<h1 style='color:red'>Hi</h1>");
} else {
// 处理其他路径
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not Found');
}
});
// 监听端口 8787
const PORT = 8787;
server.listen(PORT, () => {
console.log(`Server is running on :${PORT}`);
});