【问题标题】:getting NGINX to work with gunicorn web application让 NGINX 与 gunicorn Web 应用程序一起工作
【发布时间】:2021-02-26 09:34:02
【问题描述】:

这是我第一次使用 NGINX 部署 FastAPI + gunicorn Web 应用程序,这是真实的。

我的 Web 应用程序正在使用 Docker 的 AWS EC2 机器上运行。
到目前为止我所做的:
  1. godaddy 注册一个域 - 我们将域称为 example.club
  2. 设置指向我服务器公共 IP 地址的 DNS A 记录(通过 godaddy DNS 管理页面)。
  3. 创建了一个 site-conf.conf 文件并将其放入 /etc/nginx/sites-available。该文件具有以下配置:
server {
    listen 80;
    listen 443;
    server_name example.club;

    location / {
            proxy_pass http://127.0.0.1:8080/;
            proxy_set_header Host $http_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;
    }
}
  1. 使用以下命令将我的 .conf 文件链接到 /etc/nginx/sites-enabledsudo ln -s /etc/nginx/sites-available/site-conf.conf /etc/nginx/sites-enabled/site-conf.conf

  2. 使用以下命令重新启动我的 NGINX 服务:sudo systemctl restart nginx。 查看 journalctl 日志时,一切正常:

-- Unit nginx.service has begun starting up.
Nov 14 19:14:42 <server-address> nginx[16558]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Nov 14 19:14:42 <server-address> nginx[16558]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Nov 14 19:14:42 <server-address> systemd[1]: Failed to read PID from file /run/nginx.pid: Invalid argument
Nov 14 19:14:42 <server-address> sudo[16537]: pam_unix(sudo:session): session closed for user root
Nov 14 19:14:42 <server-address> systemd[1]: Started The nginx HTTP and reverse proxy server.
-- Subject: Unit nginx.service has finished start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit nginx.service has finished starting up.
--
-- The start-up result is done.

此时,打开 http://example.club 我可以看到“感谢使用 Amazon Linux”页面
对我来说,这证实了我的域正确地指向了我的服务器的 IP 地址 + NGINX 设置正确。

  1. 现在,我只想将我的 NGINX 代理与我的 dockerized GUNICORN + FastAPI 应用程序“连接”起来(以便向全世界公开我的 API)。
    为此,我使用以下 Dockerfile:李>
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7
WORKDIR /app
COPY . /app
# add files to Docker environment
...
# run app
RUN pip install -r requirements.txt
EXPOSE 8080
CMD ["gunicorn", "-b", "127.0.0.1:8080", "-k", "uvicorn.workers.UvicornWorker", "main:app"]
  1. 最后,我构建我的镜像 (sudo docker build -t myimage .) 并运行容器:
sudo docker run --name mycontainer -e PYTHONUNBUFFERED=1 -p 8080:8080 -d myimage

同时确保我正确发布端口 8080。
容器的日志看起来很正常:

[2020-11-14 17:32:34 +0000] [1] [INFO] Starting gunicorn 20.0.4
[2020-11-14 17:32:34 +0000] [1] [INFO] Listening at: http://127.0.0.1:8080 (1)
[2020-11-14 17:32:34 +0000] [1] [INFO] Using worker: uvicorn.workers.UvicornWorker
[2020-11-14 17:32:34 +0000] [8] [INFO] Booting worker with pid: 8
[2020-11-14 17:32:34 +0000] [8] [INFO] Started server process [8]
2020-11-14 17:32:34,783 Started server process [8]
[2020-11-14 17:32:34 +0000] [8] [INFO] Waiting for application startup.
2020-11-14 17:32:34,783 Waiting for application startup.
[2020-11-14 17:32:34 +0000] [8] [INFO] Application startup complete.
2020-11-14 17:32:34,784 Application startup complete.

我还应该提到,我通过更新 EC2 机器的安全组中的入站规则,从任何 IP 地址为 TCP 开放了端口 8080。

不幸的是,当我尝试访问我的一个 API (http://example.club/)时,我得到了NGINX default 404 page

我做错了什么?为什么 NGINX 代理无法识别我的路由?

【问题讨论】:

  • CMD中,需要将绑定选项设置为特殊的“所有接口”地址"-b", "0.0.0.0:8080";如果您将其设置为 127.0.0.1 ,它将无法从其自己的容器外部访问。另见how to run gunicorn on docker
  • @DavidMaze 谢谢,但我已经尝试过使用"-b", "0.0.0.0:8080",但也没有用:(
  • 顺便说一句,我在我的 site-conf.conf 文件中使用的 shell 变量未填充(例如 echo $http_host 没有显示任何内容),我只是从一些在线教程中复制了该文件。这可能是问题吗?

标签: docker nginx amazon-ec2 gunicorn fastapi


【解决方案1】:

尝试删除 /etc/nginx/sites-enabled/ 和 /etc/nginx/sites-available/ 中的默认配置文件,并在 @987654322 中的当前配置中添加 default_server @线。

server {
    listen 80 default_server;
    listen 443;
    server_name example.club;
    ...
}

【讨论】:

    【解决方案2】:

    为了将来参考,我最终做的是绑定到“0.0.0.0”:

    FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7
    WORKDIR /app
    COPY . /app
    # add files to Docker environment
    ...
    # run app
    RUN pip install -r requirements.txt
    EXPOSE 8080
    CMD ["gunicorn", "-b", "0.0.0.0:8080", "-k", "uvicorn.workers.UvicornWorker", "main:app"]
    

    我还稍微修改了我的 NGINX site-conf.conf(删除了 listens):

    server {
        server_name example.*;
    
        location / {
                proxy_pass http://127.0.0.1:8080/;
                proxy_set_header Host $http_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;
        }
    }
    

    最后,我安装了 python-certbot-nginx:

    sudo yum install python-certbot-nginx
    

    然后运行它:

    sudo certbot --nginx -d example.club -d www.example.club
    

    此设置允许我使用 HTTPS 连接到我的域,但我还有一个问题:我仍然可以通过显式访问端口 8080 来使用 HTTP 访问我的网络服务器 (curl http://example.club:8080/&lt;api-endpoint&gt;)。
    为了解决这个问题,我前往 AWS 网站的安全组菜单,只允许从我的机器 IP 访问端口 8080:
    screenshot

    【讨论】:

      猜你喜欢
      • 2021-07-17
      • 1970-01-01
      • 1970-01-01
      • 2019-09-08
      • 1970-01-01
      • 2012-05-22
      • 2017-02-12
      • 2011-04-08
      • 1970-01-01
      相关资源
      最近更新 更多