【发布时间】:2021-02-26 09:34:02
【问题描述】:
这是我第一次使用 NGINX 部署 FastAPI + gunicorn Web 应用程序,这是真实的。
我的 Web 应用程序正在使用 Docker 的 AWS EC2 机器上运行。
到目前为止我所做的:
- 向 godaddy 注册一个域 - 我们将域称为 example.club
- 设置指向我服务器公共 IP 地址的 DNS A 记录(通过 godaddy DNS 管理页面)。
- 创建了一个 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;
}
}
-
使用以下命令将我的 .conf 文件链接到 /etc/nginx/sites-enabled:
sudo ln -s /etc/nginx/sites-available/site-conf.conf /etc/nginx/sites-enabled/site-conf.conf -
使用以下命令重新启动我的 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 设置正确。
- 现在,我只想将我的 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"]
- 最后,我构建我的镜像 (
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 代理无法识别我的路由?
【问题讨论】:
-
在
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