问题背景:使用docker,部署nginx,配置子域名,映射不同web项目,比如a.baicu.com访问 /home/a,而b.baidu.com访问 /home/b 

# docker部署nginx报错

2024/06/12 09:44:34 [error] 22#22: *1 connect() failed (111: Connection refused) while connecting to upstream, client: xx.59.18.xx, server: console.xxx.top, request: "GET / HTTP/1.1", upstream: "http://xx.75.181.xx:8001/", host: "console.xxx.top"

先看nginx配置

server {
        listen  8001;
        server_name  localhost;
        location / {
            # root   html;
            root  /home/ruoyi/console;
            index  index.html;
            try_files $uri $uri/ /index.html;
        }
    }
    
    server {
        listen  80;
        # 你的域名
        server_name  a.baicu.com;

        location / {
            # 公网IP
            proxy_pass http://xx.x.xx.98:8001;
            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_set_header X-Forwarded-Proto $scheme;
        }
    }

问题一、

需要开启端口 8001的端口,包括防火墙和云服务器的8001端口

问题二、

因为使用的是docker部署,所以dockerfile文件也要映射端口

version: '3.1'

services:
  nginx:
    image: nginx
    restart: always
    container_name: nginx
    hostname: nginx
    ports:
      - 80:80
      - 443:443
      - 8001:8001
    environment:
      TZ: Asia/Shanghai
    volumes:
      - /data/nginx/conf:/etc/nginx
      - /data/nginx/logs:/var/log/nginx
      - /data/nginx/html:/usr/share/nginx/html
      - /home/ruoyi/console:/home/ruoyi/console/

问题三、服务器的防火墙要开启端口

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐