问题描述:

My site is up and working, but I can't access the Django Admin page. Every time I try to access /admin/ it redirects me to the https home page.

I am using Gunicorn + Nginx for the first time, so I am pretty sure it has to do with the SSL settings, but am not sure how to configure it.

Here is my Nginx config file:

upstream app_server {

server 104.131.57.229:9000 fail_timeout=0;

}

server {

listen 80;

return 301 https://mysite.com/;

}

server {

listen 443 ssl;

ssl on;

ssl_certificate /etc/nginx/ssl/www_mysite_com.crt;

ssl_certificate_key /etc/nginx/ssl/mysite.com.key;

ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;

ssl_ciphers HIGH:!aNULL:!MD5;

server_name mysite.com;

root /home/django/mysite_project/mysite/mysite;

access_log /var/log/nginx/access.log;

error_log /var/log/nginx/error.log;

client_max_body_size 4G;

keepalive_timeout 5;

location /static/ {

autoindex on;

alias /home/django/mysite_project/mysite/static/;

}

location / {

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_set_header Host $http_host;

proxy_redirect off;

proxy_pass http://app_server;

}

error_page 500 502 503 504 /500.html;

location = /500.html {

root /home/django/mysite_project/mysite/templates;

}

}

Here is my admin urls.py. Pretty standard:

from django.conf.urls import patterns, include, url

from django.contrib import admin

admin.autodiscover()

urlpatterns = patterns('',

url(r'^admin/', include(admin.site.urls)),

# other urls

)

I can access the Django Admin using my site url 9000 port, and this is normal http. Maybe that is a security issue. When I access the Admin this way however, there isn't an CSS styling, so it's hard to navigate.

I am running:

Ubuntu 14.04

Django 1.6.5

Python 3.4.0

Nginx 1.6.2

Gunicorn 19.1.1

Thanks in advance.

网友答案:

The solution to my problem was that in the nginx config file, I needed to change the http redirect config to redirect to the request URL in https, and not the https home page.

I first added this to my Django settings.py so Django trusts the https from the Nginx proxy:

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

Next, I changed this original code which was redirecting from http to the https home page each time:

server {
    listen 80;
    return 301 https://mysite.com/;
}

To this new code which redirects to the requested URL, but the https of that URL:

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

And now Django will correctly (or Nginx rather), will correctly redirect to the requested URL.

相关阅读:
Top