我正在使用 Gunicorn + Flask + Python 开发一个 Heroku 应用程序,我希望能够与工头一起在本地运行它。它工作正常,但是当我为我的站点切换为使用 SSL 时,它不再能够在/static 下找到 javascript 文件。我怎样才能使这些在 HTTPS 下可用?
最佳答案
gunicorn前端可以用nginx吗?如果是这样,那么您可以通过向 nginx.conf 添加如下位置 block 来通过 SSL 提供 javascript 等静态内容:
server {
listen 443 ssl;
# other normal ssl stuff you seem to already have working
location / {
root /path/to/your/static/stuff;
try_files $uri /index.html; # this will match your static content
}
location /api {
# your normal proxy stuff to gunicorn
}
}
另外,您可以通过 http 提供与 API 分开的静态内容以提高效率,如下所示:
server { listen 80; location / { try_files $uri /index.html; } }
server {
listen 443 ssl;
# other normal ssl stuff you seem to already have working
location /api {
# your normal proxy stuff to gunicorn
}
}
关于python - 你如何让 Gunicorn + Flask 通过 https 提供静态文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22874125/