【问题标题】:Django Admin page redirecting to the Home page with Gunicorn + Nginx when using SSL使用 SSL 时使用 Gunicorn + Nginx 重定向到主页的 Django 管理页面
【发布时间】:2014-11-18 04:25:01
【问题描述】:

我的网站已启动并正常运行,但我无法访问 Django 管理页面。每次我尝试访问 /admin/ 时,它都会将我重定向到 https 主页。

我是第一次使用 Gunicorn + Nginx,所以我很确定它与 SSL 设置有关,但不知道如何配置它。

这是我的 Nginx 配置文件:

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;
    }
}

这是我的管理员 urls.py。相当标准:

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
    )

我可以使用我的站点 url 9000 端口访问 Django Admin,这是正常的http。也许这是一个安全问题。但是,当我以这种方式访问​​管理员时,没有 CSS 样式,因此很难导航。

我在跑步:

Ubuntu 14.04
Django 1.6.5
Python 3.4.0
Nginx 1.6.2
Gunicorn 19.1.1

提前致谢。

【问题讨论】:

  • @AaronYsidoro 你为 Django 设置了SECURE_PROXY_SSL_HEADER 吗?
  • 当您获得重定向时,您是使用 http:// 还是 https:// 访问您的网站? (它应该只适用于上述配置中的 https://)
  • @chuk2bp 我将该设置添加到我的settings.py,但它仍然将我重定向到主页,并且不允许我访问 Django Admin。还有其他想法吗?
  • @talyyweb 我正在通过https 访问该网站。但我仍然无法通过 https 访问 Django Admin .....
  • 如果你从你的 nginx 设置的服务器 curl http://104.131.57.229:9000/admin 可以访问管理员吗?

标签: python django ssl nginx gunicorn


【解决方案1】:

我的问题的解决方案是在nginx config file 中,我需要更改http 重定向配置以重定向到https 中的请求URL,而不是https home page

我首先将它添加到我的 Django settings.py,因此 Django 信任来自 Nginx 代理的 https:

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

接下来,我更改了每次从 http 重定向到 https home page 的原始代码:

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

到这个重定向到请求的 URL 的新代码,但是该 URL 的 https

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

现在 Django 将正确地(或者更确切地说是 Nginx),将正确地重定向到请求的 URL。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-23
    • 1970-01-01
    • 2021-03-01
    • 2012-08-02
    • 1970-01-01
    • 2016-04-05
    • 2011-12-25
    • 1970-01-01
    相关资源
    最近更新 更多