更新

更新

我正在使用 docker 容器来托管一个 web 应用程序。我有三个主要容器:MySQL、flask 和 Nginx。前两个容器按预期工作,后者似乎工作正常,因为 docker-compose 启动时没有显示任何错误。

Nginx 容器初始化输出:

nginx  | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
nginx  | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
nginx  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
nginx  | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist
nginx  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
nginx  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
nginx  | /docker-entrypoint.sh: Configuration complete; ready for start up
nginx  | 2022/04/07 13:09:13 [notice] 1#1: using the "epoll" event method
nginx  | 2022/04/07 13:09:13 [notice] 1#1: nginx/1.21.6
nginx  | 2022/04/07 13:09:13 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
nginx  | 2022/04/07 13:09:13 [notice] 1#1: OS: Linux 4.19.130-boot2docker
nginx  | 2022/04/07 13:09:13 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
nginx  | 2022/04/07 13:09:13 [notice] 1#1: start worker processes
nginx  | 2022/04/07 13:09:13 [notice] 1#1: start worker process 21

Nginx dockerfile

# Dockerfile-nginx
FROM nginx:latest
# Nginx will listen on this port
# EXPOSE 80
# Remove the default config file that
# /etc/nginx/nginx.conf includes
RUN rm /etc/nginx/conf.d/default.conf
# We copy the requirements file in order to install
# Python dependencies
COPY nginx.conf /etc/nginx/conf.d/

部署后的容器及各自端口:

CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS                 PORTS                               NAMES
bffffcfe2f70   sc_server_nginx   "/docker-entrypoint.…"   14 seconds ago   Up 13 seconds          0.0.0.0:80->80/tcp                  nginx
a73d958c1407   sc_server_flask   "uwsgi app.ini"          9 hours ago      Up 9 hours             8080/tcp                            flask
d273db5f80ef   mysql:5.7         "docker-entrypoint.s…"   21 hours ago     Up 9 hours (healthy)   0.0.0.0:3306->3306/tcp, 33060/tcp   mysql

我是 Nginx 服务器的新手,所以我猜这可能是新手错误。我试图将所有流量从主机的 80 端口重定向到 docker 的 80 端口,后者通过套接字将流量重定向到 WSGI 容器。

我正在使用以下 Nginx 配置(我猜没什么特别的):

server {
  listen 80;
  location / {
    include uwsgi_params;
    uwsgi_pass flask:8080;
  }
}

如您所见,服务器在端口 80 上监听,并通过套接字将所有流量重定向uwsgi_pass flask:8080;到托管应用程序的 WSGI 容器。

但是,每当我在浏览器中输入 127.0.0.1:80 或 0.0.0.0:80 时,连接都会被拒绝。我没有部署防火墙,所以我猜想端口 80 关闭没有问题。

这是我的 app.ini 配置文件,其中指明了初始化和部署参数:

[uwsgi]
wsgi-file = wsgi.py
; This is the name of the variable
; in our script that will be called
callable = app
; We use the port 8080 which we will
; then expose on our Dockerfile
socket = :8080
; Set uWSGI to start up 4 workers
processes = 4
threads = 2
master = true
chmod-socket = 660
vacuum = true
die-on-term = true

此外,我还包括了 docker-compose.yml(我想它可能会有帮助):

docker-compose.yml

services:
  flask:
    build: ./flask
    container_name: flask
    restart: always
    environment:
      - APP_NAME=MyFlaskApp
      - YOURAPPLICATION_SETTINGS=docker_config.py
    expose:
      - 8080
    depends_on:
      mysql:
          condition: service_healthy
  mysql:
    image: mysql:5.7
    container_name: mysql
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD:
      MYSQL_DATABASE:
      MYSQL_USER:
      MYSQL_PASSWORD:
    volumes:
      - ./db/init.sql:/data/application/init.sql
    healthcheck:
      test: mysql -u -p --database -e "show tables;"
      interval: 3s
      retries: 5
      start_period: 30s
  nginx:
    build: ./nginx
    container_name: nginx
    restart: always
    depends_on:
      - mysql
      - flask
    ports:
      - "80:80"

有人可以帮忙吗?

更新

我使用 Wireshark 扫描环回接口以查看服务器对 0.0.0.0:80 的响应(我怀疑端口 80 可能有问题)并且得到了以下有效负载:

wireshark

相关内容