[Nginx, Django]Securing connections with SSL

Reference: Django by Example Chapter 13

Create directory under /etc/nginx, and cd to that directory

mkdir /etc/nginx/ssl
cd /etc/nginx/ssl

Creating a SSL certificate

openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout your_key_name.key -out your_cert_name.crt

You will be asked to enter the following information:

Country Name (2 letter code) []:
State or Province Name (full name) []:
Locality Name (eg, city) []: 
Organization Name (eg, company) []:
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []: 
Email Address []: 

The most important field is the Common Name. You have to specify the domain name for the certificate, it must match the way you access your website.
If you access your website by IP, eg. 10.0.0.1, fill in 10.0.0.1
If you access your website by Domain Name, eg. example.cm, fill in example.com

Configuring Nginx to use SSL

Edit the nginx.conf file or the file in your sites-enabled directory and modify the server directive to include the following
SSL directives:

server {
    listen 80;
    listen 443 ssl;
    ssl_certificate /etc/nginx/ssl/your_cert_name.crt;
    ssl_certificate_key /etc/nginx/ssl/your_key_name.key;
    server_name ip_or_domain_name;
    # ...
}

Restart Nginx with the following command:

systemctl restart nginx

Configuring your project for SSL

Django includes some settings specific to SSL. Edit the settings.py and add the following code to it:

SECURE_SSL_REDIRECT = True
CSRF_COOKIE_SECURE = True

These settings are as follows:
SECURE_SSL_REDIRECT: Whether HTTP requests have to be redirected to
HTTPS ones
CSRF_COOKIE_SECURE: This has to be set to establish a secure cookie for the
cross-site request forgery protection

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值