【发布时间】:2023-03-14 11:08:01
【问题描述】:
我有一个 Web 应用程序,它使用 Django 作为后端,使用 Nginx 和 Gunicorn 作为反向代理。我已经设置了 Nginx 配置,当通过 HTTPS 发送请求时它可以正常工作。但是在发出 HTTP 请求时它会失败。
我希望应用程序同时适用于 HTTP 和 HTTPS 请求。
我的一些 Django 视图包含通过 HTTP 发出的内部请求,因为它们必须与开发服务器和生产服务器兼容
class LoginView(APIView):
...
http_host = request.META["HTTP_HOST"]
url = f"http://{http_host}/o/token/"
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
data = {
'username': req_data['username'],
'password': req_data['password'],
}
response = requests.post(url, headers=headers, data=data)
...
我的nginx配置如下
server {
listen 8000 ssl;
server_name backend.test.com www.backend.test.com;
access_log /var/log/nginx/app_backend.access.log;
error_log /var/log/nginx/app_backend.error.log;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/testuser/app_backend/app_backend;
}
location /media/ {
root /home/testuser/app_backend/app_backend;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/testuser/app_backend/app_backend/app_backend.sock;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/backend.test.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/backend.test.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
我已经在数字海洋上托管了 VPS。
如何使服务器也为后端视图内部发出的 http 请求工作
注意:我尝试通过这样做return 301 https://$host$request_uri; 将所有 HTTP 请求重定向到仅在 Nginx 中的生产环境中的 HTTPS,但这不起作用
【问题讨论】:
-
浏览器将阻止来自 https 页面的 http 请求...。您应该使用“url for”帮助程序或其他任何名称...或将其更改为
url='/o/token/'...但请查看http 重定向 nginx 以查看如何将 http 请求重写为 https ... -
当我尝试得到错误
Invalid URL '/o/token/': No schema supplied. Perhaps you meant http:///o/token/。所以它不会自动添加主机和架构 -
我也尝试使用
return 301 https://$host$request_uri;将任何 http 请求转换为 Nginx 中的 https,但没有成功 -
啊,你对 o/token 的事情是正确的...对不起,我想给予足够的关注...并且重定向可能会因发布数据而中断...。您为什么将其发布到 http?
-
@JoranBeasley 我可以直接 POST 到 HTTPS,但这只能在生产中使用。当我在本地主机上的开发中运行应用程序时,POST 到 HTTPS 将不起作用
标签: python django nginx https server