Django + uWSGI + Nginx + SSL - 工作配置请求(强调 SSL)
Django + uWSGI + Nginx + SSL - request for working configuration (emphasis on SSL)
有人有这四个的工作配置吗?
- Django
- uWSGI
- Nginx
- SSL
主要问题是如何为此正确设置 SSL
?我在谷歌上搜索了很多,但仍然无法正常工作。我有一个 http
和 unix sockets
的工作设置,但这是我所能得到的。
发布了一些其他答案,但它们大多是代码片段,而不是整个配置。
server {
listen 80;
server_name example.com;
rewrite ^/(.*) https://example.com/ permanent;
}
server {
listen 443 ssl;
server_name example.com;
access_log /var/log/nginx/example.com_access.log combined;
error_log /var/log/nginx/example.com_error.log error;
ssl_certificate /etc/nginx/ssl/example-unified.crt;
ssl_certificate_key /etc/nginx/ssl/example.key;
location /static/ {
alias /webapps/example/static/;
}
location /media/ {
alias /webapps/example/media/;
}
location / {
proxy_pass http://localhost:8000/;
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
这是基本的 nginx 配置,它将与 SSL 一起工作,并将请求转发到端口 8000 上的 uwsgi 运行(如果需要,您可以将其更改为套接字)。
对于高级 SSL 设置,请检查 THIS。
我是 nginx、uwsgi 和 ssl 的新手。这里分享我测试的nginx和uwsgi配置。
基本上部署Django有四个步骤,只支持SSL/HTTPS。
- 设置 SSL 证书
- 使用openssl生成server.crt和server.key
openssl req -new -x509 -nodes -out server.crt -keyout server.key
- 在Django项目下配置nginx.conf和uwsgi.ini
- 设置nginx.conf(抱歉,文本块中的布局很奇怪,所以我在这里插入图片。)
- 从 /etc/nginx/sites-enabled 到此文件的符号链接,以便 nginx 可以看到它
sudo ln -s /path/to/django/example_nginx.conf /etc/nginx/sites-enabled/
- 在django项目下配置uwsgi.ini
配置settings.py
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
SECURE_SSL_REDIRECT = True
重启nginx和uwsgi
- 重启nginx
sudo /etc/init.d/nginx restart
- 运行 uwsgi
uwsgi --ini /path/to/django/example_uwsgi.ini
有人有这四个的工作配置吗?
- Django
- uWSGI
- Nginx
- SSL
主要问题是如何为此正确设置 SSL
?我在谷歌上搜索了很多,但仍然无法正常工作。我有一个 http
和 unix sockets
的工作设置,但这是我所能得到的。
发布了一些其他答案,但它们大多是代码片段,而不是整个配置。
server {
listen 80;
server_name example.com;
rewrite ^/(.*) https://example.com/ permanent;
}
server {
listen 443 ssl;
server_name example.com;
access_log /var/log/nginx/example.com_access.log combined;
error_log /var/log/nginx/example.com_error.log error;
ssl_certificate /etc/nginx/ssl/example-unified.crt;
ssl_certificate_key /etc/nginx/ssl/example.key;
location /static/ {
alias /webapps/example/static/;
}
location /media/ {
alias /webapps/example/media/;
}
location / {
proxy_pass http://localhost:8000/;
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
这是基本的 nginx 配置,它将与 SSL 一起工作,并将请求转发到端口 8000 上的 uwsgi 运行(如果需要,您可以将其更改为套接字)。
对于高级 SSL 设置,请检查 THIS。
我是 nginx、uwsgi 和 ssl 的新手。这里分享我测试的nginx和uwsgi配置。
基本上部署Django有四个步骤,只支持SSL/HTTPS。
- 设置 SSL 证书
- 使用openssl生成server.crt和server.key
openssl req -new -x509 -nodes -out server.crt -keyout server.key
- 使用openssl生成server.crt和server.key
- 在Django项目下配置nginx.conf和uwsgi.ini
- 设置nginx.conf(抱歉,文本块中的布局很奇怪,所以我在这里插入图片。)
- 从 /etc/nginx/sites-enabled 到此文件的符号链接,以便 nginx 可以看到它
sudo ln -s /path/to/django/example_nginx.conf /etc/nginx/sites-enabled/
- 在django项目下配置uwsgi.ini
- 设置nginx.conf(抱歉,文本块中的布局很奇怪,所以我在这里插入图片。)
配置settings.py
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') SESSION_COOKIE_SECURE = True CSRF_COOKIE_SECURE = True SECURE_SSL_REDIRECT = True
重启nginx和uwsgi
- 重启nginx
sudo /etc/init.d/nginx restart
- 运行 uwsgi
uwsgi --ini /path/to/django/example_uwsgi.ini
- 重启nginx