Docker-Nginx意外重定向(304)

时间:2019-01-08 00:00:03

标签: docker nginx docker-compose

我在使用docker-composenginx时遇到麻烦。首先,我有这个docker-compose.yml

services:
  nginx:
    build: ./nginx
    ports:
      - '8080:80'
    depends_on:
      - web
      - api

  web:
    build: ./web
    depends_on:
      - api

  api:
    build: ./api

web(端口3000)和api(端口8000)都是快递服务器,分别返回 WEB API 。现在,在./nginx内:

# Dockefile

FROM nginx:alpine

COPY ["default.conf", "/etc/nginx/conf.d/"]

EXPOSE 80





# default.conf

server {
  location / {
    proxy_pass  http://web:3000;
  }

  location /api {
    proxy_pass  http://api:8000;
  }
}

现在,当我进入http://localhost:8080时,我会得到 WEB ,但是当我进入http://localhost:8080/api时,它会重定向到http://localhost:1337/api/,但我什么也没得到(顺便说一句,它抛出304错误)

但是,当我编写此default.conf时(将api中的/放入

# default.conf

server {
  location / {
    proxy_pass  http://api:8000;
  }

  location /api {
    proxy_pass  http://web:3000;
  }
}

我得到相同的结果,但是在/中我得到了 API ,因此两台服务器都可以工作。

1 个答案:

答案 0 :(得分:0)

我不知道它是否对您有帮助。我在docker-compose中直接使用nginx docker镜像。

例如。

docker-compose.yml

version: '3'
services:
  jobsaf-server:
    build: 
      context: .
      dockerfile: Dockerfile.production
    container_name: jobsaf-server
    ports:
     - "3000:3000"
     - "5858:5858"
     - "35729:35729"
     - "6379:6379"
    environment:
     - NODE_ENV=production
    networks:
      - front-tier
      - back-tier
    depends_on:
      - "redis"
      - "mongo"
    links:
      - mongo
      - redis
    volumes:
     - ./server:/var/www/app/jobsaf-website/server
  nginx:
    image: nginx:stable
    depends_on:
      - jobsaf-server
    links:
      - jobsaf-server
    volumes:
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf  
    ports:
     - "0.0.0.0:80:80"
  mongo:
    image: mongo:latest
    container_name: mongo
    volumes:
      - "db-data:/data/db"
    environment: 
      - MONGO_INITDB_ROOT_USERNAME=${DB_USER}
      - MONGO_INITDB_ROOT_PASSWORD=${DB_PASS}
      - MONGO_INITDB_DATABASE=admin
    ports:
      - "0.0.0.0:27017:27017"
    networks:
      - back-tier
  redis:
    image: redis
    container_name: redis
    networks:
      - back-tier   
volumes:
  db-data:
    # - /data/db
networks:
  front-tier:
  back-tier: