在 Linux 上使用 Nginx 和 Gunicorn 托管 Django 应用

简介:

介绍

托管 Django Web 应用程序相当简单,虽然它比标准的 PHP 应用程序更复杂一些。 让 Web 服务器对接 Django 的方法有很多。 Gunicorn 就是其中最简单的一个。

Gunicorn(Green Unicorn 的缩写)在你的 Web 服务器 Django 之间作为中间服务器使用,在这里,Web 服务器就是 Nginx。 Gunicorn 服务于应用程序,而 Nginx 处理静态内容。

Gunicorn

安装

使用 Pip 安装 Gunicorn 是超级简单的。 如果你已经使用 virtualenv 搭建好了你的 Django 项目,那么你就有了 Pip,并且应该熟悉 Pip 的工作方式。 所以,在你的 virtualenv 中安装 Gunicorn。

 
  1. $ pip install gunicorn

配置

Gunicorn 最有吸引力的一个地方就是它的配置非常简单。处理配置最好的方法就是在 Django 项目的根目录下创建一个名叫 Gunicorn 的文件夹。然后在该文件夹内,创建一个配置文件。

在本篇教程中,配置文件名称是 gunicorn-conf.py。在该文件中,创建类似于下面的配置:

 
  1. import multiprocessing
  2. bind = 'unix:///tmp/gunicorn1.sock'
  3. workers = multiprocessing.cpu_count() * 2 + 1
  4. reload = True
  5. daemon = True

在上述配置的情况下,Gunicorn 会在 /tmp/ 目录下创建一个名为 gunicorn1.sock 的 Unix 套接字。 还会启动一些工作进程,进程数量相当于 CPU 内核数量的 2 倍。 它还会自动重新加载并作为守护进程运行。

运行

Gunicorn 的运行命令有点长,指定了一些附加的配置项。 最重要的部分是将 Gunicorn 指向你项目的 .wsgi文件。

 
  1. gunicorn -c gunicorn/gunicorn-conf.py -D --error-logfile gunicorn/error.log yourproject.wsgi

上面的命令应该从项目的根目录运行。 -c 选项告诉 Gunicorn 使用你创建的配置文件。 -D 再次指定 gunicorn 为守护进程。 最后一部分指定 Gunicorn 的错误日志文件在你创建 Gunicorn 文件夹中的位置。 命令结束部分就是为 Gunicorn 指定 .wsgi 文件的位置。

Nginx

现在 Gunicorn 配置好了并且已经开始运行了,你可以设置 Nginx 连接它,为你的静态文件提供服务。 本指南假定你已经配置好了 Nginx,而且你通过它托管的站点使用了单独的 server 块。 它还将包括一些 SSL 信息。

如果你想知道如何让你的网站获得免费的 SSL 证书,请查看我们的 Let'sEncrypt 指南

 
  1. # 连接到 Gunicorn
  2. upstream yourproject-gunicorn {
  3. server unix:/tmp/gunicorn1.sock fail_timeout=0;
  4. }
  5. # 将未加密的流量重定向到加密的网站
  6. server {
  7. listen 80;
  8. server_name yourwebsite.com;
  9. return 301 https://yourwebsite.com$request_uri;
  10. }
  11. # 主服务块
  12. server {
  13. # 设置监听的端口,指定监听的域名
  14. listen 443 default ssl;
  15. client_max_body_size 4G;
  16. server_name yourwebsite.com;
  17. # 指定日志位置
  18. access_log /var/log/nginx/yourwebsite.access_log main;
  19. error_log /var/log/nginx/yourwebsite.error_log info;
  20. # 告诉 nginx 你的 ssl 证书
  21. ssl on;
  22. ssl_certificate /etc/letsencrypt/live/yourwebsite.com/fullchain.pem;
  23. ssl_certificate_key /etc/letsencrypt/live/yourwebsite.com/privkey.pem;
  24. # 设置根目录
  25. root /var/www/yourvirtualenv/yourproject;
  26. # Nginx 指定静态文件路径
  27. location /static/ {
  28. # Autoindex the files to make them browsable if you want
  29. autoindex on;
  30. # The location of your files
  31. alias /var/www/yourvirtualenv/yourproject/static/;
  32. # Set up caching for your static files
  33. expires 1M;
  34. access_log off;
  35. add_header Cache-Control "public";
  36. proxy_ignore_headers "Set-Cookie";
  37. }
  38. # Nginx 指定你上传文件的路径
  39. location /media/ {
  40. Autoindex if you want
  41. autoindex on;
  42. # The location of your uploaded files
  43. alias /var/www/yourvirtualenv/yourproject/media/;
  44. # Set up aching for your uploaded files
  45. expires 1M;
  46. access_log off;
  47. add_header Cache-Control "public";
  48. proxy_ignore_headers "Set-Cookie";
  49. }
  50. location / {
  51. # Try your static files first, then redirect to Gunicorn
  52. try_files $uri @proxy_to_app;
  53. }
  54. # 将请求传递给 Gunicorn
  55. location @proxy_to_app {
  56. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  57. proxy_set_header Host $http_host;
  58. proxy_redirect off;
  59. proxy_pass http://njc-gunicorn;
  60. }
  61. # 缓存 HTMLXML JSON
  62. location ~* \.(html?|xml|json)$ {
  63. expires 1h;
  64. }
  65. # 缓存所有其他的静态资源
  66. location ~* \.(jpg|jpeg|png|gif|ico|css|js|ttf|woff2)$ {
  67. expires 1M;
  68. access_log off;
  69. add_header Cache-Control "public";
  70. proxy_ignore_headers "Set-Cookie";
  71. }
  72. }

配置文件有点长,但是还可以更长一些。其中重点是指向 Gunicorn 的 upstream 块以及将流量传递给 Gunicorn 的 location 块。大多数其他的配置项都是可选,但是你应该按照一定的形式来配置。配置中的注释应该可以帮助你了解具体细节。

保存文件之后,你可以重启 Nginx,让修改的配置生效。

 
  1. # systemctl restart nginx

一旦 Nginx 在线生效,你的站点就可以通过域名访问了。

原文发布时间为:2017-04-15

本文来自云栖社区合作伙伴“Linux中国”

相关文章
|
1天前
|
消息中间件 存储 NoSQL
django的gunicorn的异步任务执行
django的gunicorn的异步任务执行
9 4
|
6天前
|
中间件 应用服务中间件 nginx
Nginx+uWSGI+Django原理
Nginx+uWSGI+Django原理
|
5天前
|
应用服务中间件 Linux Shell
Linux 配置 Nginx 服务的详细步骤,绝对干货
Linux 配置 Nginx 服务的详细步骤,绝对干货
21 0
|
2月前
|
Unix Linux Ruby
在windows和linux上高效快捷地发布Dash应用
在windows和linux上高效快捷地发布Dash应用
|
2月前
|
Linux iOS开发 开发者
跨平台开发不再难:.NET Core如何让你的应用在Windows、Linux、macOS上自如游走?
【8月更文挑战第28天】本文提供了一份详尽的.NET跨平台开发指南,涵盖.NET Core简介、环境配置、项目结构、代码编写、依赖管理、构建与测试、部署及容器化等多个方面,帮助开发者掌握关键技术与最佳实践,充分利用.NET Core实现高效、便捷的跨平台应用开发与部署。
82 3
|
2月前
|
负载均衡 应用服务中间件 网络安全
Django后端架构开发:Nginx服务优化实践
Django后端架构开发:Nginx服务优化实践
46 2
|
2月前
|
存储 Linux 网络安全
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Linux/Linux Container)
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Linux/Linux Container)
|
2月前
|
JavaScript Linux
【Azure App Service for Linux】NodeJS镜像应用启动失败,遇见 RangeError: Incorrect locale information provided
【Azure App Service for Linux】NodeJS镜像应用启动失败,遇见 RangeError: Incorrect locale information provided
|
2月前
|
JavaScript Linux API
【Azure 应用服务】NodeJS Express + MSAL 应用实现AAD集成登录并部署在App Service Linux环境中的实现步骤
【Azure 应用服务】NodeJS Express + MSAL 应用实现AAD集成登录并部署在App Service Linux环境中的实现步骤
|
2月前
|
前端开发 JavaScript Linux
【Azure 应用服务】在Azure App Service for Linux环境中,部署的Django应用,出现加载css、js等静态资源文件失败
【Azure 应用服务】在Azure App Service for Linux环境中,部署的Django应用,出现加载css、js等静态资源文件失败