duffn
duffn

Reputation: 3760

Django, Nginx, HTTPs and HttpResponseRedirect

I'm receiving a 404 error from Nginx when attempting to return an HttpResponseRedirect from Django. This is all happening under HTTPs The flow goes something like this:

Except, instead of redirecting to the page, Nginx just eventually servers its 404 page.

I can get this to work in development while not under Nginx and HTTPs, so I suspect this has something to do with my Nginx setup. I have this working successfully on other servers so I'm unsure why I cannot get it working here

Sample Django view:

@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 error

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/",

I've tried a number of different solutions involving proxy_set_header X-Forwarded-Protocol $scheme; in Nginx and Djangos SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTOCOL', 'https') but without luck.

Upvotes: 0

Views: 815

Answers (1)

duffn
duffn

Reputation: 3760

It turned out that this had nothing to do with Nginx, SSL or Django. IT changed one of our DNS servers without notifying me. The response was timing out because it could not resolve DNS. Updating to the new DNS server in /etc/resolv.conf solved the issue.

Upvotes: 0

Related Questions