【发布时间】:2021-10-08 00:07:58
【问题描述】:
我正在学习如何使用 Python 和 Flask 构建和托管我自己的网站,但我无法让我的网站正常运行,因为当我尝试通过我的域名访问我的网站时,我不断收到无限重定向循环。
我使用 Python、Flask 和 Flask-Flatpages 制作了我的网站。我将代码上传到 GitHub 并将其拉到我家的 Raspberry Pi 4 上。我在 RasPi 上安装了 gunicorn 来为网站提供服务,并设置了两个工作人员来监听请求。我还设置了 nginx 作为反向代理并监听来自外部的请求。这是我的 nginx 配置:
server {
if ($host = <redacted>.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
# listen on port 80 (http)
listen 80;
server_name <redacted>.com www.<redacted>.com;
location ~ /.well-known {
root /home/pi/<redacted>.com/certs;
}
location / {
# redirect any requests to the same URL but on https
return 301 https://$host$request_uri;
}
}
server {
# listen on port 443 (https)
listen 443;
ssl on;
server_name <redacted>.com www.<redacted>.com;
# location of the SSL certificate
ssl_certificate /etc/letsencrypt/live/<redacted>.com/fullchain.pem; # m$
ssl_certificate_key /etc/letsencrypt/live/<redacted>.com/privkey.pem; #$
# write access and error logs to /var/log
access_log /var/log/blog_access.log;
error_log /var/log/blog_error.log;
location / {
# forward application requests to the gunicorn server
proxy_pass http://localhost:8000;
proxy_redirect off;
proxy_set_header X_Forwarded_Proto $scheme;
proxy_set_header Host $host;
location /static {
# handle static files directly, without forwarding to the application
alias /home/pi/<redacted>.com/blog/static;
expires 30d;
}
}
当我通过输入 RasPi 的本地 IP 访问该网站时(我在 /etc/dhcpcd.conf 中设置了一个静态 IP 地址),该网站的服务很好,尽管我的浏览器似乎无法识别SSL 证书即使当我点击旁边的 Not Secure > Certificate 时 Chrome 说证书是有效的。
为了公开网站,我将路由器上的端口 80 转发到 RasPi 并设置 ufw 以仅允许来自端口 80、443 和 22 的请求。我使用 GoDaddy 购买了一个域名,然后添加通过更改 GoDaddy 中的名称服务器将域添加到 CloudFlare(我计划稍后设置 cloudflare-ddns,这就是我首先将域添加到 CloudFlare 的原因)。作为临时解决方案,我已将路由器的当前 IP 添加到 CloudFlare DNS 设置中的 A 记录中,我希望在接下来的几天内保持不变。
当我尝试通过我的公共域名访问我的网站时,我的问题出现了。当我这样做时,我得到ERR_TOO_MANY_REDIRECTS,我怀疑这是由于我的 nginx 配置存在问题。我已经阅读了this post 并尝试将我的 CloudFlare SSL/TLS 设置从Flexible 更改为Full (strict)。然而,这导致了一个不同的问题,我得到一个CloudFlare error 522: connection timed out。 CloudFlare help page 中的解决方案似乎都不适用于我的情况,因为我已经确认:
- 我没有阻止
ufw中的任何 CloudFlare IP - 服务器没有过载(我现在是唯一一个访问它的人)
- Keepalive 已启用(我没有更改任何默认设置,虽然我不确定它是否默认启用)
- DNS 表 A 记录中的 IP 地址与我的路由器的公共 IP 匹配(通过在 google 上搜索“我的 IP 是什么”找到)
如果这里有很多关于一个问题的问题,我们深表歉意,但我们将不胜感激!
【问题讨论】:
标签: ssl flask raspberry-pi gunicorn nginx-reverse-proxy