小编Bra*_*len的帖子

带有 nginx 的 FastAPI 不提供 HTTPS 中的静态文件

我有一个小型的测试 FastAPI Web 应用程序,它提供一个简单的 HTML 页面,该页面需要位于静态文件夹中的 css 样式表。它安装在 Linode 服务器(Ubuntu 20.04 LTS)、nginx、gunicorn、uvicorn 工作人员和supervisorctl 上。我已经使用 certbot 添加了证书。

该应用程序在 http 中工作正常,但无法访问 https 中的静态文件。当通过 http 访问时,所有基于静态的功能都可以工作,但是当通过 https 访问时,它缺少 css 样式表中的所有样式。我需要让它工作,这样我就可以加载一个需要 css 和其他静态文件夹存储功能的更复杂的应用程序。

文件结构为:

/home/<user_name>/application
- main.py
- static
   |_ css
   |_ bootstrap
- templates
   |_ index.html
Run Code Online (Sandbox Code Playgroud)

主要.py:

import fastapi
import uvicorn
from fastapi import Request
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates


api = fastapi.FastAPI()

api.mount('/static', StaticFiles(directory='static'), name='static')
templates = Jinja2Templates(directory="templates")


@api.get('/')
@api.get('/index', response_class=HTMLResponse)
def index(request: Request):
    message …
Run Code Online (Sandbox Code Playgroud)

https nginx fastapi uvicorn

5
推荐指数
1
解决办法
4291
查看次数

标签 统计

fastapi ×1

https ×1

nginx ×1

uvicorn ×1