Python + FastAPI + Gunicorn + Nginx + Oracle Cloud VM,网站未显示

Python + FastAPI + Gunicorn + Nginx + Oracle Cloud VM, website not showing

我关注了这两篇文章:Article 1, Article 2 来设置我的 Python 快速 API 项目。

按照这 2 篇文章,我在 Oracle Cloud 的 VM 上安装了 Gunicorn 和 Ngnix。

这是我的配置文件:

/etc/systemd/system/gunicorn.service

[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
User=ubuntu
Group=ubuntu
WorkingDirectory=/home/ubuntu/maze
ExecStart=/home/ubuntu/.local/bin/gunicorn \
          --access-logfile - \
          --workers 4 \
          -k uvicorn.workers.UvicornWorker \
          -b unix:gunicorn.sock \
          -m 007 \
          --timeout 1200 \
          main:app

[Install]
WantedBy=multi-user.target

/etc/nginx/conf.d/gunicorn.conf

server {
listen 8000;
server_name 168.138.12.192;

        location / {
            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;
            proxy_pass http://unix:/run/gunicorn.sock;
        }
}

/etc/systemd/system/gunicorn.socket

[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target

然后我运行以下命令,以确保更改生效

sudo systemctl daemon-reload
sudo systemctl restart gunicorn
sudo systemctl restart gunicorn.service
sudo systemctl restart gunicorn.socket
sudo systemctl restart nginx

sudo systemctl enable gunicorn
sudo systemctl enable gunicorn.service
sudo systemctl enable gunicorn.socket
sudo systemctl enable nginx

还有运行

sudo systemctl status gunicorn.service

确保服务正常运行 运行。我觉得不错:

● gunicorn.service - gunicorn daemon
     Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2021-05-23 02:47:59 UTC; 10s ago
TriggeredBy: ● gunicorn.socket
   Main PID: 4142 (gunicorn)
      Tasks: 13 (limit: 1107)
     Memory: 339.2M
     CGroup: /system.slice/gunicorn.service
             ├─4142 /usr/bin/python3 /home/ubuntu/.local/bin/gunicorn --access-logfile - --workers 4 -k uvicorn.workers.UvicornWorker -b unix:/run/gunicorn.sock -m 007 --timeout 1200 m>
             ├─4153 /usr/bin/python3 /home/ubuntu/.local/bin/gunicorn --access-logfile - --workers 4 -k uvicorn.workers.UvicornWorker -b unix:/run/gunicorn.sock -m 007 --timeout 1200 m>
             ├─4154 /usr/bin/python3 /home/ubuntu/.local/bin/gunicorn --access-logfile - --workers 4 -k uvicorn.workers.UvicornWorker -b unix:/run/gunicorn.sock -m 007 --timeout 1200 m>
             ├─4157 /usr/bin/python3 /home/ubuntu/.local/bin/gunicorn --access-logfile - --workers 4 -k uvicorn.workers.UvicornWorker -b unix:/run/gunicorn.sock -m 007 --timeout 1200 m>
             └─4159 /usr/bin/python3 /home/ubuntu/.local/bin/gunicorn --access-logfile - --workers 4 -k uvicorn.workers.UvicornWorker -b unix:/run/gunicorn.sock -m 007 --timeout 1200 m>

May 23 02:48:08 instance-20210520-2215 gunicorn[4154]: [2021-05-23 02:48:08 +0000] [4154] [INFO] Application startup complete.
May 23 02:48:08 instance-20210520-2215 gunicorn[4157]: [2021-05-23 02:48:08 +0000] [4157] [INFO] Started server process [4157]
May 23 02:48:08 instance-20210520-2215 gunicorn[4157]: [2021-05-23 02:48:08 +0000] [4157] [INFO] Waiting for application startup.
May 23 02:48:08 instance-20210520-2215 gunicorn[4157]: [2021-05-23 02:48:08 +0000] [4157] [INFO] Application startup complete.
May 23 02:48:08 instance-20210520-2215 gunicorn[4153]: [2021-05-23 02:48:08 +0000] [4153] [INFO] Started server process [4153]
May 23 02:48:08 instance-20210520-2215 gunicorn[4153]: [2021-05-23 02:48:08 +0000] [4153] [INFO] Waiting for application startup.
May 23 02:48:08 instance-20210520-2215 gunicorn[4153]: [2021-05-23 02:48:08 +0000] [4153] [INFO] Application startup complete.
May 23 02:48:08 instance-20210520-2215 gunicorn[4159]: [2021-05-23 02:48:08 +0000] [4159] [INFO] Started server process [4159]
May 23 02:48:08 instance-20210520-2215 gunicorn[4159]: [2021-05-23 02:48:08 +0000] [4159] [INFO] Waiting for application startup.
May 23 02:48:08 instance-20210520-2215 gunicorn[4159]: [2021-05-23 02:48:08 +0000] [4159] [INFO] Application startup complete.

然后我运行这个命令来查看我是否能够在虚拟机上看到正确的网站

curl http://localhost:8000

我确实看到我的网站被退回了。

但是,当我尝试通过以下 IP 地址从我的浏览器访问该网站时:

http://http://168.138.12.192/

我可以看到默认的nginx网站

http://168.138.12.192:8000/

Firefox 显示无法连接

我错过了什么??

事实证明,我还需要更改防火墙规则以打开端口 8000,尽管我已将 Ingress 规则添加到 pen 端口 8000

sudo iptables -I INPUT -p tcp -s 0.0.0.0/0 --dport 8000 -j ACCEPT