Python+;FastAPI+;OracleCloud:如何将Oracle云上的python fastapi端点公开到Internet

Python+;FastAPI+;OracleCloud:如何将Oracle云上的python fastapi端点公开到Internet,python,nginx,gunicorn,fastapi,oracle-cloud-infrastructure,Python,Nginx,Gunicorn,Fastapi,Oracle Cloud Infrastructure,我有一个Python+FastAPI restful API项目运行Oracle云VM实例的免费层 我使用Gunicorn来提供api,还安装了Nginx,以备需要 我已经测试了我正在运行的项目 curlhttp://localhost:8000 我可以看到我的API响应 现在我的问题是:如何在Internet上公开此api端点 更新1 我使用以下命令启动了Python API项目: gunicorn -w 4 -k uvicorn.workers.UvicornWorker main:app

我有一个Python+FastAPI restful API项目运行Oracle云VM实例的免费层

我使用Gunicorn来提供api,还安装了Nginx,以备需要

我已经测试了我正在运行的项目

curlhttp://localhost:8000

我可以看到我的API响应

现在我的问题是:如何在Internet上公开此api端点


更新1

我使用以下命令启动了Python API项目:

gunicorn -w 4 -k uvicorn.workers.UvicornWorker main:app --timeout 1200 -b 0.0.0.0
我看到了下面的信息:

[2021-05-23 00:40:28 +0000] [3850] [INFO] Starting gunicorn 20.0.2
[2021-05-23 00:40:28 +0000] [3850] [INFO] Listening at: http://0.0.0.0:8000 (3850)
[2021-05-23 00:40:28 +0000] [3850] [INFO] Using worker: uvicorn.workers.UvicornWorker
[2021-05-23 00:40:28 +0000] [3853] [INFO] Booting worker with pid: 3853
[2021-05-23 00:40:28 +0000] [3854] [INFO] Booting worker with pid: 3854
[2021-05-23 00:40:28 +0000] [3857] [INFO] Booting worker with pid: 3857
[2021-05-23 00:40:28 +0000] [3858] [INFO] Booting worker with pid: 3858
[2021-05-23 00:42:04 +0000] [3853] [INFO] Started server process [3853]
[2021-05-23 00:42:04 +0000] [3857] [INFO] Started server process [3857]
[2021-05-23 00:42:04 +0000] [3857] [INFO] Waiting for application startup.
[2021-05-23 00:42:04 +0000] [3858] [INFO] Started server process [3858]
[2021-05-23 00:42:04 +0000] [3858] [INFO] Waiting for application startup.
[2021-05-23 00:42:04 +0000] [3858] [INFO] Application startup complete.
[2021-05-23 00:42:04 +0000] [3853] [INFO] Waiting for application startup.
[2021-05-23 00:42:04 +0000] [3853] [INFO] Application startup complete.
[2021-05-23 00:42:04 +0000] [3857] [INFO] Application startup complete.
[2021-05-23 00:42:04 +0000] [3854] [INFO] Started server process [3854]
[2021-05-23 00:42:04 +0000] [3854] [INFO] Waiting for application startup.
[2021-05-23 00:42:04 +0000] [3854] [INFO] Application startup complete.
然后,我从Compute>>Instances>>实例详细信息面板复制了IP地址,并从Chrome访问了它。我马上就明白了

无法连接

还通读了几篇关于使用Nginx的文章,并在没有任何运气的情况下进行了尝试


更新2

使用
curl
从本地计算机访问网站

$ curl http://168.138.12.192:8000/
curl: (7) Failed to connect to 168.138.12.192 port 8000: No route to host
然而,当直接使用
curl
访问IP时,我能够获得默认的Nginx网站。
$curl

您是否更改了/var/www/html目录下的默认html页面?如果没有,请尝试根据您的要求定制html页面,看看它是否适合您,否则,当使用公共IP从浏览器访问时,它只会显示默认的nginx页面

除此之外,还要检查安全列表和操作系统防火墙中是否允许使用端口8000。http请求的默认端口是80,您需要在配置文件中将默认端口从80更改为8000,以使其正常工作。请参阅本页,这可能很有用

非常感谢!!
最后,我发现我错过了什么:

sudo iptables -I INPUT -p tcp -s 0.0.0.0/0 --dport 8000 -j ACCEPT 
我必须运行此命令才能打开端口8000(是的,我的网站正在使用端口8000)

我以为我已经添加了入口规则来接受TCP8000,但结果证明我仍然需要运行前面提到的命令


我不太明白为什么我需要这样做,但它解决了问题。

您是否尝试在
0.0.0
ip地址上运行应用程序?hi@BrownBear是的,我在更新1中添加了更多详细信息。你能帮忙吗?试着通过
curl
检查你的机器ip地址hi@BrownBear-okie,我已经更新了我的更新中的详细信息2I从我的本地机器测试了命令
curlhttp://168.138.12.192:8000
给我html代码。