使用nginx作为反向代理配置多个域名的https证书

偶然想到,自己还没有做好使用nginx作为反向代理,搭配多个域名的https,心血来潮,就问了下bing怎么做。

docker-compose.yml

version: '3.7'

services:
  nginx-proxy:
    image: nginx
    container_name: nginx-proxy
    restart: always
    ports:
      - 80:80
      - 443:443
    volumes:
      - ./nginx-proxy/certs:/etc/nginx/certs:ro
      - ./nginx-proxy/conf.d:/etc/nginx/conf.d # mount the local config directory


  nginx1:
    image: nginx
    container_name: nginx1
    restart: always
    environment:
      - VIRTUAL_HOST=swjnxyf.cc # change this to your first domain name
    volumes:
      - ./nginx1/html:/usr/share/nginx/html # mount the local html directory

  nginx2:
    image: nginx
    container_name: nginx2
    restart: always
    environment:
      - VIRTUAL_HOST=swjnxyf.cn # change this to your second domain name
    volumes:
      - ./nginx2/html:/usr/share/nginx/html # mount the local html directory


docker-compose up -d 就会自动创建目录了。
[root@localhost server]# tree
.
├── docker-compose.yml
├── nginx1
│   └── html
├── nginx2
│   └── html
└── nginx-proxy
    ├── certs
    └── conf.d

当然。html文件夹的静态网页需要自行创建。
随后,可以进入nginx-proxy/conf.d目录,修改配置文件,毕竟,没有配置,代理就不会运行。

nginx.conf

upstream swjnxyf.cc {
  least_conn;
  server nginx1;
  server nginx2;
}
#使用upstream可以做一些负载均衡的任务
upstream swjnxyf.cn {
  least_conn;
  server nginx1;
  server nginx2;
}

server {
  listen 80;
  server_name swjnxyf.cc swjnxyf.cn;

  location /.well-known/acme-challenge/ {
    root /usr/share/nginx/html;
    try_files $uri =404;
  }

  location / {
    return 301 https://$host$request_uri;
  }
}

server {
  listen 443 ssl;
  server_name swjnxyf.cc;

  ssl_certificate /etc/nginx/certs/swjnxyf.cc.crt;
  ssl_certificate_key /etc/nginx/certs/swjnxyf.cc.key;



  location / {
    proxy_pass http://swjnxyf.cc; # proxy to nginx2 container for other requests
  }
}

server {
  listen 443 ssl;
  server_name swjnxyf.cn;

  ssl_certificate /etc/nginx/certs/swjnxyf.cn.crt;
  ssl_certificate_key /etc/nginx/certs/swjnxyf.cn.key;


  location / {
    proxy_pass http://swjnxyf.cn; # proxy to nginx2 container for other requests
  }
}

这样,就可以完成基本的转发请求和对应域名的https证书了。

最后,需要进入certs文件使用openssl生产域名的证书
openssl genrsa -out swjnxyf.cn.key 2048
openssl req -new -key swjnxyf.cn.key -out swjnxyf.cn.csr
openssl x509 -req -days 365 -in swjnxyf.cn.csr -signkey swjnxyf.cn.key -out swjnxyf.cn.crt

openssl genrsa -out swjnxyf.cc.key 2048
openssl req -new -key swjnxyf.cc.key -out swjnxyf.cc.csr
openssl x509 -req -days 365 -in swjnxyf.cc.csr -signkey swjnxyf.cc.key -out swjnxyf.cc.crt

忘了,这些文件准备好以后,还需要重启nginx-proxy的服务。

我习惯于进入容器再加载,而不是重启容器

docker exec -it nginx-proxy /bin/sh

# nginx -t	//检测无误就这样
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

# nginx -s reload
2023/06/17 15:00:53 [notice] 36#36: signal process started

由于本人已经修改了win10de hosts文件,解析了所需要的域名到虚拟机的IP,所以才能实验顺利。
访问https://swjnxyf.cn/ 就OK了。提示证书风险,忽略即可。

和之前的单个服务器托管多个https证书相比,少了自动化,以及自动检测容器,生成证书以及证书续签。兴许后面可以完善。

posted @ 2023-06-17 23:15  念秋  阅读(1109)  评论(0编辑  收藏  举报