Django正在从HTTPS重定向到HTTP(Django is redirecting from HTTPS to HTTP)

编程入门 行业动态 更新时间:2024-10-28 04:18:16
Django正在从HTTPS重定向到HTTP(Django is redirecting from HTTPS to HTTP)

我有一个Django电子商务网站正在运行,并购买并安装了SSL证书。

我添加了一个VirtualHost条目:

<VirtualHost *:443> #Basic setup ServerAdmin blah@test.com ServerName test.com ServerAlias www.test.com Alias /media/admin/ /home/test/public_html/test/release/env/lib/python2.6/dist-packages/django/contrib/admin/media/ Alias /static/ /home/test/public_html/test/release/static/ Alias /media/ /home/test/public_html/test/release/media/ <Directory /home/test/public_html/test/release/> Order deny,allow Allow from all </Directory> RewriteEngine On LogLevel warn ErrorLog /home/test/public_html/test/logs/error.log CustomLog /home/test/public_html/test/logs/access.log combined WSGIDaemonProcess test user=www-data group=www-data threads=20 processes=2 WSGIProcessGroup test_ssl WSGIScriptAlias / /home/test/public_html/test/release/apache/test.wsgi SSLEngine On SSLCertificateFile /etc/apache2/ssl/test.com.crt SSLCertificateChainFile /etc/apache2/ssl/gs_root.pem SSLCertificateKeyFile /etc/apache2/ssl/www.test.com.key </VirtualHost>

这是urls.py文件:

from django.conf.urls.defaults import patterns, include, url from django.contrib import admin from django.conf import settings from gallery.models import LOCATIONS, Photo admin.autodiscover() from satchmo_store.urls import urlpatterns as satchmo_urls from satchmo_store.shop.views.sitemaps import sitemaps from cms.sitemaps import CMSSitemap sitemaps['pages'] = CMSSitemap urlpatterns = patterns('', url(r'^admin/', include(admin.site.urls)), url(r'^search/', include('haystack.urls')), # Include satchmo urls. Unfortunately, this also includes it's own # /admin/ and everything else. url(r'^shop/', include(satchmo_urls)), url(r'^sitemap\.xml/?$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}), url(r'events/gallery/(.*)/(.*)/$', 'gallery.views.events_image'), url(r'locations/view-all/(.*)/$', 'gallery.views.locations_image'), url(r'locations/view-all/$', 'gallery.views.locations_view_all',{ 'queryset':Photo.objects.filter(gallery__category=LOCATIONS).distinct()}), url(r'^contact-us/', include('contact_form.urls')), url(r'^', include('cms.urls')), ) if settings.DEBUG: urlpatterns = patterns('', (r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}), (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}), (r'^404/$', 'django.views.defaults.page_not_found'), (r'^500/$', 'django.views.defaults.server_error'), ) + urlpatterns

还有一个非ssl的conf,工作正常。

每当我请求网站的HTTPS版本时,我都会得到一个302头响应,它会重定向到HTTP版本。

apache conf中没有明确表示转到端口80的重定向。

我一直在反对这一点,任何帮助都会很棒!

谢谢

I have a Django ecommerce site running, and have purchases and installed an SSL cert for it.

I have added a VirtualHost entry:

<VirtualHost *:443> #Basic setup ServerAdmin blah@test.com ServerName test.com ServerAlias www.test.com Alias /media/admin/ /home/test/public_html/test/release/env/lib/python2.6/dist-packages/django/contrib/admin/media/ Alias /static/ /home/test/public_html/test/release/static/ Alias /media/ /home/test/public_html/test/release/media/ <Directory /home/test/public_html/test/release/> Order deny,allow Allow from all </Directory> RewriteEngine On LogLevel warn ErrorLog /home/test/public_html/test/logs/error.log CustomLog /home/test/public_html/test/logs/access.log combined WSGIDaemonProcess test user=www-data group=www-data threads=20 processes=2 WSGIProcessGroup test_ssl WSGIScriptAlias / /home/test/public_html/test/release/apache/test.wsgi SSLEngine On SSLCertificateFile /etc/apache2/ssl/test.com.crt SSLCertificateChainFile /etc/apache2/ssl/gs_root.pem SSLCertificateKeyFile /etc/apache2/ssl/www.test.com.key </VirtualHost>

Here is the urls.py file:

from django.conf.urls.defaults import patterns, include, url from django.contrib import admin from django.conf import settings from gallery.models import LOCATIONS, Photo admin.autodiscover() from satchmo_store.urls import urlpatterns as satchmo_urls from satchmo_store.shop.views.sitemaps import sitemaps from cms.sitemaps import CMSSitemap sitemaps['pages'] = CMSSitemap urlpatterns = patterns('', url(r'^admin/', include(admin.site.urls)), url(r'^search/', include('haystack.urls')), # Include satchmo urls. Unfortunately, this also includes it's own # /admin/ and everything else. url(r'^shop/', include(satchmo_urls)), url(r'^sitemap\.xml/?$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}), url(r'events/gallery/(.*)/(.*)/$', 'gallery.views.events_image'), url(r'locations/view-all/(.*)/$', 'gallery.views.locations_image'), url(r'locations/view-all/$', 'gallery.views.locations_view_all',{ 'queryset':Photo.objects.filter(gallery__category=LOCATIONS).distinct()}), url(r'^contact-us/', include('contact_form.urls')), url(r'^', include('cms.urls')), ) if settings.DEBUG: urlpatterns = patterns('', (r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}), (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}), (r'^404/$', 'django.views.defaults.page_not_found'), (r'^500/$', 'django.views.defaults.server_error'), ) + urlpatterns

There is also a conf for non ssl which is working fine.

Whenever I request the HTTPS version of the site, I get a 302 header response which redirects to the HTTP version.

There are no redirects in the apache conf that explicitly state go to port 80.

Ive been banging my head against this for a while, any help would be great!

Thanks

最满意答案

你可能已经解决了它,这可能是一个完全不同的问题,但我只是发现了一些听起来有些类似的东西,因为我没有找到解决你问题的答案,我认为发布回复可能是值得的(尽管我有301而你有302)。

我在nginx后面运行一个Django站点(Django 1.6.1)和gunicorn。 所以nginx会做SSL。 环境变量HTTPS设置为on 。

当我设置一个没有http到https重定向的测试服务器时,我注意到有些请求最终被重定向到一个http地址 - 类似于你所描述的,但在我的情况下它只是针对一个特定的链接。 在查看了请求和响应标头后,我发现:初始请求https://example.org/test被Django / gunicorn重定向,并且301 MOVED PERMANENTLY永远301 MOVED PERMANENTLY到http://exmaple.org/test/ 。 然后nginx以400 Bad Request - The plain HTTP request was sent to HTTPS port响应400 Bad Request - The plain HTTP request was sent to HTTPS port 。

我很快就遇到了一个我以前没注意过的设置: APPEND_SLASH ( https://docs.djangoproject.com/en/1.6/ref/settings/#std:setting-APPEND_SLASH ),默认值为True 。

将APPEND_SLASH = False添加到我的settings.py文件后,对https://example.org/test的请求导致404 NOT FOUND响应,而没有重定向到http。 所以似乎APPEND_SLASH不尊重HTTP环境变量设置 - 我想配置SECURE_PROXY_SSL_HEADER ( https://docs.djangoproject.com/en/1.6/ref/settings/#std:setting-SECURE_PROXY_SSL_HEADER )会解决这个问题,我有尚未测试过。

顺便说一句,在我的案例中,“错误”链接的原因是模板中的硬编码链接。 避免这样的链接的简单方法是使用内置的{% url ... %}模板标记( https://docs.djangoproject.com/en/1.6/ref/templates/builtins/#url [对不起,我无法点击此链接,因为我没有“至少10个声誉”...])。

也许这有助于你或其他任何想知道为什么Django有时会从https重定向到http的人。

You probably already fixed it and it could be an entirely different problem, but I just came across something that sounds somewhat similar and as I did not find an answer that addressed your issue, I thought it might be worth to post a reply (despite I was having a 301 and you a 302).

I am running a Django site (Django 1.6.1) with gunicorn behind nginx. So nginx does the SSL. The environment variable HTTPS is set to on.

When I set up a test server without an http-to-https redirect, I noticed that some requests end up being redirected to an http address - similar to what you describe, but in my case it was just for one particular link. After looking into the request and response headers, I found out: The initial request https://example.org/test got redirected by Django/gunicorn with 301 MOVED PERMANENTLY to http://exmaple.org/test/. nginx then responded with 400 Bad Request - The plain HTTP request was sent to HTTPS port.

Quickly I came across a setting I had not paid much attention to before: APPEND_SLASH (https://docs.djangoproject.com/en/1.6/ref/settings/#std:setting-APPEND_SLASH) with the default value True.

After adding APPEND_SLASH = False to my settings.py file, a request to https://example.org/test resulted in a 404 NOT FOUND response, without a redirect to http. So it seems that APPEND_SLASH does not respect the HTTP environment variable setting - I guess configuring SECURE_PROXY_SSL_HEADER (https://docs.djangoproject.com/en/1.6/ref/settings/#std:setting-SECURE_PROXY_SSL_HEADER) would solve this, I have not tested it yet.

By the way, the reason for that "faulty" link in my case was a hard-coded link in a template. The easy way to avoid links like that is using the built-in {% url ... %} template tag (https://docs.djangoproject.com/en/1.6/ref/templates/builtins/#url [sorry, I could not make this link clickable because I don't have "at least 10 reputation"...]).

Perhaps this helps you or anyone else who wonders why Django sometimes redirects from https to http.

更多推荐

本文发布于:2023-07-28 04:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1300374.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向   HTTPS   Django   HTTP   redirecting

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!