Nginx中配置SSL证书

目录结构


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}`);
});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值