使用uwsgi和Nginx在Django 1.7+中设置Https

时间:2015-07-30 22:01:54

标签: python django nginx https

我在使用https设置网站时遇到问题。目前,我将我的nginx服务器设置为侦听http和https响应。

但是,现在我只想允许https并将任何http请求重定向到htpps。

我没试过这篇文章:How to deploy an HTTPS-only site, with Django/nginx?

在Django 1.7 +中这样做的推荐方法是什么?

下面是我的ngninx.conf文件:

# mysite_nginx.conf

# the upstream component nginx needs to connect to
upstream django {
    server unix:///uwsgi-tutorial/mysite/mysite.sock; # for a file socket
#   server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}

# configuration of the server
server {
    # the port your site will be served on
    listen 80;
    listen 443 default_server ssl;
    #ssl on;
    ssl_certificate     /uwsgi-tutorial/conf/www.example.com.chained.crt;
    ssl_certificate_key /uwsgi-tutorial/conf/www.example.com.key;

    # the domain name it will serve for
#   server_name localhost; # substitute your machine's IP address or FQDN
    server_name example.com; # substitute your machine's IP address or FQDN
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias /uwsgi-tutorial/mysite/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /uwsgi-tutorial/mysite/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /uwsgi-tutorial/mysite/uwsgi_params; # the uwsgi_params file you installed
    }
}

1 个答案:

答案 0 :(得分:0)

已经有一个图书馆可以很好地完成这项工作 - sslify:

https://github.com/rdegges/django-sslify

继续阅读github页面上的说明。