Django,Nginx,HTTPs和HttpResponseRedirect

时间:2015-04-24 16:33:35

标签: django nginx https

我在尝试从Django返回HttpResponseRedirect时从Nginx收到404错误。这一切都发生在HTTPs下。流程如下:

  • 用户转到页面
  • 以表格
  • 输入一些信息
  • 视图在POST后处理表单,然后尝试将用户重定向到其他页面。

除了重定向到页面之外,Nginx最终只服务于其404页面。

可以让它在开发中工作,而不是在Nginx和HTTPs下,所以我怀疑这与我的Nginx设置有关。我在其他服务器上成功运行,所以我不确定为什么我不能在这里工作

示例Django视图:

@login_required()
def index(request):
    if request.method == 'POST':
        form = ShortenerForm(request.POST)

        if form.is_valid():
            # Do stuff    

            return HttpResponseRedirect(reverse('shortener_thankyou'))
    else:
        form = ShortenerForm()
    return render(request, 'shortener/index.html', {'form': form})

Nginx的

upstream apollo2_app_server {
  # fail_timeout=0 means we always retry an upstream even if it failed
  # to return a good HTTP response (in case the Unicorn master nukes a
  # single worker for timing out).

  server unix:/webapps/apollo2/run/gunicorn.sock fail_timeout=0;
}

server {
   listen 80;
   server_name apollo.mydomain.com;
   rewrite ^ https://$server_name$request_uri? permanent;
}

server {
     listen 443;
     ssl on;
     ssl_certificate /etc/nginx/ssl/bundle.crt;
     ssl_certificate_key /etc/nginx/ssl/mydomain.com.key;
     server_name apollo.mydomain.com;

    client_max_body_size 4G;

    keepalive_timeout    70;

    access_log /webapps/apollo2/logs/nginx-access.log;
    error_log /webapps/apollo2/logs/nginx-error.log;

    location /static/ {
        alias   /webapps/apollo2/static/;
    }

    location /media/ {
        alias   /webapps/apollo2/media/;
    }

    location / {
        # an HTTP header important enough to have its own Wikipedia entry:
        #   http://en.wikipedia.org/wiki/X-Forwarded-For
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        # enable this if and only if you use HTTPS, this helps Rack
        # set the proper protocol for doing redirects:
        # proxy_set_header X-Forwarded-Proto https;

        # pass the Host: header from the client right along so redirects
        # can be set properly within the Rack application
        proxy_set_header Host $http_host;

        # we don't want nginx trying to do something clever with
        # redirects, we set the Host: header above already.
        proxy_redirect off;

        # set "proxy_buffering off" *only* for Rainbows! when doing
        # Comet/long-poll stuff.  It's also safe to set if you're
        # using only serving fast clients with Unicorn + nginx.
        # Otherwise you _want_ nginx to buffer responses to slow
        # clients, really.
        # proxy_buffering off;

        # Try to serve static files from nginx, no point in making an
        # *application* server like Unicorn/Rainbows! serve static files.
        if (!-f $request_filename) {
            proxy_pass http://apollo2_app_server;
            break;
        }
    }

    # Error pages
    error_page 500 502 503 504 /500.html;
    location = /500.html {
        root /webapps/apollo2/static/;
    }
}

Nginx错误

2015/04/24 11:04:10 [error] 18139#0: *3395 upstream prematurely closed connection while reading response header from upstream, client: 192.168.0.119, server: apollo.mydomain.com, request: "POST /shortener/ HTTP/1.1", upstream: "http://unix:/webapps/apollo2/run/gunicorn.sock:/shortener/",

我尝试了许多涉及proxy_set_header X-Forwarded-Protocol $方案的不同解决方案;在Nginx和Djangos SECURE_PROXY_SSL_HEADER =(' HTTP_X_FORWARDED_PROTOCOL',' https')但没有运气。

0 个答案:

没有答案