在 AWS 上的 Docker 中运行 Nginx 和 FastApi 时出现 404 错误

问题描述 投票:0回答:1

我遵循这个教程
https://bentranz.medium.com/deploy-dockerized-application-to-aws-elastic-beanstalk-f8a3cf2944a7

成功调试并最终在 AWS 上运行我的服务器和 nginx 容器后,我开始收到以下日志
enter image description here

这是我的docker-compose.yml

version: '3.8'

services:
  server:
    image: **************.us-east-2.amazonaws.com/howdy-api-production:latest
    container_name: howdyserver
    platform: linux/amd64
    restart: always
    environment:
      - RDS_USER=${RDS_USER}
      - RDS_PASSWORD=${RDS_PASSWORD}
      - RDS_SERVER=${RDS_SERVER}
      - RDS_PORT=${RDS_PORT}
      - RDS_DB=${RDS_DB}
      - REDIS_PORT=${REDIS_PORT}
      - SECRET_KEY=${SECRET_KEY}
      - SMTP_SENDER_ADDRESS=${SMTP_SENDER_ADDRESS}
      - SENDGRID_API_KEY=${SENDGRID_API_KEY}    
      - AWS_KEY_ID=${AWS_KEY_ID}
      - AWS_SECRET_KEY=${AWS_SECRET_KEY}
      - AWS_SECRET_KEY=${AWS_SECRET_KEY}
    build: 
        context: ./server
        dockerfile: backend.Dockerfile
    volumes:
      - ./server/app/:/app/
      - public_assets:/app/shared/public
    ports:
      - 8000:8000
    networks:
      - howdynetwork

 nginx:
    image: nginx:alpine
    container_name: howdynginx
    platform: linux/amd64
    restart: always
    ports:
      - 80:80
    depends_on:
      - server
    volumes:
      - public_assets:/app/shared/public
      - ./nginx.conf:/etc/nginx/conf.d/default.conf
    networks:
      - howdynetwork

volumes:
   public_assets:

networks:
  howdynetwork:
    driver: bridge

这是我的 nginx.conf

upstream api {
  server server:8000;
}

server {
  listen 80;
  # Uncomment this line when you have your own domain for the api.
  # server_name example.org www.example.org;

  client_max_body_size 10m;
  keepalive_timeout 60s;

  access_log /dev/stdout;
  error_log stderr;

  location / {
    try_files $uri @api;
  }

  location @api {
    proxy_pass http://api;
    proxy_http_version 1.1;
    proxy_set_header Host $host;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Port $server_port;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_redirect off;
  }

  location /public {
    alias /app/public;
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  error_page 500 502 503 504 /500.json;
  location = /500.json {
    return 500 '{"statusCode": 500, "message": "Something went wrong."}';
  }
}

有什么想法为什么会发生这种情况吗?

谢谢!

我尝试了几个不同的 Nginx nginx.conf 但每个碰巧都有相同的结果。

amazon-web-services nginx docker-compose amazon-elastic-beanstalk fastapi
1个回答
0
投票

通过添加“/”API端点的路由解决了该问题。

nginx.conf 我有

location / {
    try_files $uri @api;
  }

告诉 Nginx 要 ping 哪个端点来检查运行状况。我在那里没有终点。

© www.soinside.com 2019 - 2024. All rights reserved.