Vite React不支持Docker Nginx,抛出连接拒绝

wljmcqd8  于 2023-08-03  发布在  Nginx
关注(0)|答案(1)|浏览(122)

我正在尝试将应用程序放入容器中。目前,我正在使用Vite,Django后端和Nginx作为服务器。Django部分工作正常,我能够通过Nginx发出请求并提供响应。但是,当我尝试向Vite发出请求时,Nginx抛出错误:2023/07/27 21:37:00 [error] 10#10: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.25.0.1, server: _, request: "GET / HTTP/1.1", upstream: "http://172.25.0.2:5137/", host: "localhost"
我的docker-compose.yml

version: '3.8'

services:
  api:
    container_name: api
    build:
      context: ./api
      dockerfile: Dockerfile
    expose:
      - 8000
    env_file:
      - .env.dev

  app:
    container_name: app
    build:
      context: ./app
      dockerfile: Dockerfile
    expose:
      - 5137

  nginx:
    container_name: server
    image: nginx:latest
    volumes:
      - ./nginx/:/etc/nginx/conf.d/
    command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"
    ports:
      - "80:80"
    depends_on:
      - api
      - app

字符串
我的vite.config.js

import {defineConfig} from 'vite'
import react from '@vitejs/plugin-react'

// https://vitejs.dev/config/
export default defineConfig({
    plugins: [react()],
    server: {
        watch: {
            usePolling: true,
        },
        host: true,
        strictPort: true,
        port: 5173,
    }
})


我的Dockerfile

FROM node:latest

WORKDIR /usr/src/app

COPY package.json /usr/src/app/
COPY package-lock.json /usr/src/app/
RUN npm ci

COPY . /usr/src/app/

CMD ["npm", "run", "dev"]


我的nginx.conf

upstream api {
    server api:8000;
}

upstream app {
    server app:5137;
}

server {

    server_name _;

    listen 80;
    listen [::]:80;

    location /api/ {
        proxy_pass http://api;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
        client_max_body_size 25M;
    }

    location / {
        proxy_pass http://app;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
        client_max_body_size 25M;
    }

}


我尝试打开Vite容器中的5137端口到容器网络外部,并在浏览器中使用http://localhost:5137访问它,但它不起作用。
如果你能帮忙的话,我将不胜感激。

4ngedf3f

4ngedf3f1#

在您的代码中,您提供了5173端口,但是您暴露并查询了5137端口。
更改您的代码。
希望有帮助!

相关问题