Nginx只显示使用HTTPS访问时运行服务器的索引页

szqfcxe2  于 2023-05-16  发布在  Nginx
关注(0)|答案(1)|浏览(192)

我有两个Docker容器:一个运行nginx,另一个运行flask中的服务器应用程序。当使用HTTP发送请求时,所有服务器的端点都可以直接或通过nginx访问。然而,在使用mkcert添加证书(并允许浏览器中的异常)后,只有我的服务器的索引页是可访问的。
下面是我的nginx.conf和docker-compose.yml的相关部分
nginx.conf

server {
    listen 80;
    server_name example.com;

    # Redirect all HTTP traffic to HTTPS
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate /etc/nginx/certs/server.crt;
    ssl_certificate_key /etc/nginx/certs/server.key;

    location / {
        proxy_pass http://server:5000;
        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;
    }
}

docker-compose.yml

services:
  server:
    build:
      context:  ./server
      dockerfile: Dockerfile.prod
    container_name: flask
    networks:
      backend:
    restart: always
    command: gunicorn --bind 0.0.0.0:5000 run:app
    volumes:
      - ./server/:/usr/src/server/
    expose:
      - 5000
    env_file:
      - ./server/.env.prod

  nginx:
    build: ./nginx
    container_name: nginx
    networks:
      backend:
      frontend:
    restart: always
    ports:
      - "8080:80"
      - "443:443"
    volumes:
      - ./server/certs:/etc/nginx/certs

尝试访问任何端点都会导致nginx响应404 not found错误。使用地址http://example.com重定向我'欢迎来到nginx!成功的安装站点,让我相信nginx.conf的重定向部分也有问题。
我已经使用了端口8080,因为端口80正在使用中。lsof -i:80没有返回任何内容。

hfwmuf9z

hfwmuf9z1#

这个配置对我来说很有用:

events {
  worker_connections  4096;  ## Default: 1024
}

http {
  
  server {
      listen 80;
      server_name example.internal.test;

      # Redirect all HTTP traffic to HTTPS
      return 301 https://$host$request_uri;
  }

  server {
    listen 443 ssl;
    server_name example.internal.test;

    ssl_certificate /etc/nginx/certs/server.crt;
    ssl_certificate_key /etc/nginx/certs/server.key;

    location / {
        proxy_pass http://server:8080;
        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;
    }
  }
}

以及:

services:
  server:
    image: adminer
    restart: always
    networks:
      - frontend

  nginx:
    image: nginx
    container_name: nginx
    depends_on:
      - server
    restart: always
    command: [nginx-debug, "-g", "daemon off;"] // add this for debugging
    networks:
      - frontend
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./server/certs:/etc/nginx/certs
      - ./nginx.conf:/etc/nginx/nginx.conf:ro

networks:
  frontend:
    name: frontend

我使用了adminer服务器而不是flask应用程序,但概念保持不变。

您可以尝试检查您的/etc/hosts是否为example.com正确配置。同时确保刷新缓存dscacheutil -flushcache。最后尝试使用curl而不是浏览器。curl https://example.com --insecure
请确保将command: [nginx-debug, "-g", "daemon off;"]添加到docker文件中,以便您可以调试请求是否到达nginx服务器以及它们是否被正确重定向。

相关问题