简体   繁体   English

Django redirect()使用HTTPS在生产服务器上将URL截断为域名

[英]Django redirect() truncating URLs to domain name on production server using HTTPS

I'm running Django 1.8, Python 2.7. 我正在运行Django 1.8,Python 2.7。 I have noticed a problem on my site with redirects which seems only to affect my production server using HTTPS protocol, but not staging or dev servers running plain HTTP. 我注意到站点上的重定向问题似乎只影响使用HTTPS协议的生产服务器,而不会影响运行纯HTTP的登台服务器或开发服务器。

As examples, I've identified two simple things which do not currently work for me over HTTPS: 作为示例,我确定了两个简单的问题,这些问题目前在HTTPS上对我不起作用:

  1. Visiting https://www.eduduck.com/support/thanks with no trailing slash redirects to https://www.eduduck.com/ instead of appending a slash and redirecting to https://www.eduduck.com/support/thanks/ as I would have expected with (default settings) APPEND_SLASH = True. 参观https://www.eduduck.com/support/thanks没有尾随斜线重定向到https://www.eduduck.com/而不是附加一个斜线和重定向到的https://www.eduduck.com/support/谢谢/(与我期望的一样)(默认设置)APPEND_SLASH = True。

  2. Submitting a valid support form 'wrongly' redirects to https://www.eduduck.com/ site base url when using HTTPS but works correctly with HTTP, redirecting to /support/thanks/ 使用HTTPS时,提交有效的支持表格“错误地”重定向到https://www.eduduck.com/网站的基本URL,但在HTTP上可以正常使用,重定向到/ support / thanks /

It's all routine stuff, or should be. 都是常规的东西,或者应该是。 Very perplexed by this; 对此很困惑; any pointers most gratefully received. 非常感谢收到的所有指针。 NB: Problem only appears under HTTPS 注意:问题仅出现在HTTPS下

support/urls.py support / urls.py

from django.conf.urls import url
from django.views.generic import TemplateView

from . import views

urlpatterns = [
    url(r'^$', views.support, name='support'),
    url(
        r'^thanks/$',
        TemplateView.as_view(template_name ='support/thanks.html')),
]

support/forms.py support / forms.py

from django import forms

class SupportForm(forms.Form):
    """General request for support or contact"""
    subject = forms.CharField(max_length = 100)
    email = forms.EmailField(label='Your email')
    message = forms.CharField(widget = forms.Textarea)

    def clean_message(self):
        """Sensible messages cannot be too short"""

        message = self.cleaned_data['message']
        wc = len(message.split())
        if wc < 4:
            raise forms.ValidationError(
                "Please muster four or more words of wisdom or woe!"
            )
        return message

support/views.py support / views.py

from django.core.mail import send_mail

from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.template import RequestContext

from .forms import SupportForm

import logging
logger = logging.getLogger(__name__)

def support(request):
    """Provide a support/contact email form"""

    logger.info('Support view')
    if request.method=="POST":
        form = SupportForm(request.POST)
        if form.is_valid():
            cdata = form.cleaned_data
            send_mail(cdata['subject'],
                      cdata['message'],
                      cdata.get('email', 'example@example.com'),
                        ['example@example.com'],
            )
            return HttpResponseRedirect('/support/thanks/')
    else:
        form = SupportForm()

    return render(
        request, 
        'support/support.html',
        {'support_form': form}, 
    )

So in the end, yes it was my nginx config. 所以最后,是的,这是我的nginx配置。 I had an old sites-enabled/default config file, which I deleted. 我有一个旧的启用了站点的默认配置文件,该文件已删除。 This made some progress, but I also had to edit the config file for my production site. 这取得了一些进展,但是我还必须编辑生产站点的配置文件。 The diff for the final (working) server block looks like this: 最终(工作)服务器块的差异如下所示:

server {
    - listen [::]:80 default_server; 
    + listen 80 default_server;
    server_name www.example.com;
    return 301 https://$host$request_uri
}

As I mentioned in my earlier comment, despite reading the docs, I still don't exactly know what is wrong with [::]:80 which I thought was for IPv6 compatibility. 正如我在较早的评论中提到的那样,尽管阅读了文档,但我仍然不完全知道[::]:80出了什么问题,我认为这是为了实现IPv6兼容性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM