gpt4 book ai didi

nginx - Starlette 的 url_for 不会在 Nginx 后面创建带有 https 方案的链接(通过 uvicorn)

转载 作者:行者123 更新时间:2023-12-04 14:08:57 28 4
gpt4 key购买 nike

我已经尝试了一切:
@星星:

routes = [
Mount("/static/", StaticFiles(directory=parent+fs+"decoration"+fs+"static"), name="static"),
Route(....),
Route(....),
]
@乌维康:
--forwarded-allow-ips=domain.com
--proxy-headers
@url_for:
_external=True
_scheme="https"
@nginx:
proxy_set_header Subdomain $subdomain;
proxy_set_header Host $http_host;
proxy_pass http://localhost:7000/;
proxy_set_header X-Forwarded-For $remote_addr;
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_set_header X-Forwarded-Host $server_name;
proxy_redirect http://$http_host/ https://$http_host/;
include proxy_params;
server {
if ($host = sub.domain.com) {
return 301 https://$host$request_uri;
} # managed by Certbot


listen 80 ;
listen [::]:80 ;
server_name sub.domain.com;
return 404; # managed by Certbot

}
如果我打开 .css 或 .js 链接,nginx 会将其呈现为 https。
当我允许 Firefox 忽略不安全的内容时,整个页面都会在生产服务器上正确呈现。
Let's encrypt 与整个域完美配合,证书没有问题。

最佳答案

我认为 uvicorn 的 --forwarded-allow-ips=domain.com部分需要有你的 Nginx 服务器的 IP,因为它是做转发的。 (即将“domain.com”更改为您的 nginx 服务器的 IP) 注意您也可以使用环境变量 FORWARDED_ALLOW_IPS=*FORWARDED_ALLOW_IPS=1.2.3.4相反(如果在 nginx 后面的 gunicorn 后面运行 uvicorn 很有用)
对于登陆这里的其他读者:我遇到了同样的问题,因为我没有配置我的“服务器”配置部分来实际转发 X-Forwarded-ProtoX-Forwarded-For标题,以便 uvicorn 可以获取它们。这是我需要的一个例子:

 server {
server_name example.com
location / {
proxy_redirect off;
# These are the critical headers needed by uvicorn to honor HTTPS in url_for :
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# These are just some other headers you may find useful
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Host $server_name;

...
}


listen 443 ssl;

关于nginx - Starlette 的 url_for 不会在 Nginx 后面创建带有 https 方案的链接(通过 uvicorn),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66051867/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com