介绍

托管 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. # 缓存 HTML、XML 和 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 在线生效,你的站点就可以通过域名访问了。

结语

如果你想深入研究,Nginx 可以做很多事情。但是,上面提供的配置是一个很好的开始,并且你可以用于实践中。 如果你见惯了 Apache 和臃肿的 PHP 应用程序,像这样的服务器配置的速度应该是一个惊喜。

阅读原文

在 Linux 上使用 Nginx 和 Gunicorn 托管 Django 应用的更多相关文章

  1. linux上使用nginx、uwsgi部署django项目

    参考:CentOS7下部署Django项目详细操作步骤 注意事项: 在虚拟环境中操作,虚拟环境中安装nginx.uwsgi,虚拟环境外需安装uwsgi -- 临时关闭防火墙:systemctl sto ...

  2. 在Linux上使用Nginx为Solr集群做负载均衡

    在Linux上使用Nginx为Solr集群做负载均衡 在Linux上搭建solr集群时需要用到负载均衡,但测试环境下没有F5 Big-IP负载均衡交换机可以用,于是先后试了weblogic的proxy ...

  3. Linux+.Net Core+Nginx(在Linux上使用Nginx反向代理.Net Core 项目)

    Linux+.Net Core+Nginx 之前的文章中有提到关于使用Nginx在linux来实现反向代理,今天我们继续加点料.在Centos7中部署.NetCore,然后使用Nginx进行反向代理! ...

  4. 01 linux上安装 nginx

    一:linux上安装 nginx 下载nginx:wget http://nginx.org/download/nginx-1.6.2.tar.gz 解压:tar zxvf nginx-1.6.2.t ...

  5. [运维] 如何在 Linux 上安装 Nginx 服务器(一)

    原因 因为小程序对素材的大小是由要求的, 所以为了简化小程序上的内存要求, 在Linux上安装nginx来作为静态资源服务器, 这篇为第一篇, 主要介绍怎么在Linux上安装nginx, 下一篇将会介 ...

  6. Nginx 和 Gunicorn 部署 Django项目

    目录 Nginx 和 Gunicorn 部署 Django项目 配置Nginx 安装配置Gunicorn 通过命令行直接启动 Gunicorn 与 uwsgi 的区别,用哪个好呢 Gunicorn u ...

  7. Linux上配置Nginx+PHP5(FastCGI)

    原为地址:http://www.laruence.com/2009/07/28/1030.html Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,以事件驱动的方式编写,所以有非常好的性能,同时 ...

  8. 【Linux】Linux上安装Nginx

    本文介绍Linux环境安装Nginx,这里用的Linux系统是CentOS 7.2. 1. 从Nginx官网下载Nginx.这里用的版本为:1.13.6. 2. 将下载下来的Nginx上传到Linux ...

  9. linux上搭建nginx+php+mysql环境详细讲解

    1.mysql安装 #安装编译环境 yum install -y gcc gcc-c++ gcc-devel g++ g++-devel; yum install -y wget yum instal ...

随机推荐

  1. 触发器二(DML触发器)(学习笔记)

    DML触发器(语句触发器) 由DML语句进行触发,当用户执行了INSERT,UPDATE,DELETE操作时就会触发操作 示例一.只有在每个月的10日才允许办理,新员工入职与离职,其他时间不允许增加和 ...

  2. 【二】Drupal 入门之新建主题

    Drupal 的模板是以  *.tpl.php  命名的 php 文件 1.在Drupal中,默认模板路径为 moudles/system 这就是我们为什么还没有制作模板 Drupal 就能够正常显示 ...

  3. KVC简介 -字典转模型,模型转字典

    // 下面两个方法.都属于 KVC 的方法 // KVC 是 cocoa 的大招.间接给对象属性设置数值 // 程序运行过程中,动态给对象属性设置数值.不关心 .h 中是怎样定义的 //      仅 ...

  4. MySQL_Oracle_事物的隔离级别

    数据库会话的设置: 1:脏读 情景:A事物读取B事物修改了但是未提交的数据 问题:若B回滚了事物,A就读到了错误数据. 2:不可重复读 情景:A事物查询数据,B修改了数据,A又查询数据 问题:A事物前 ...

  5. rarcrack

    apt-get install rarcrack使用方法:rarcrack --threads xx --type rar xx.rar

  6. 在Ubuntu Server是配置iptables防火墙

    iptables 是一个安装在Ubuntu Server上的默认防火墙.在正常的ubuntu安装过程中,iptables是被安装上了的,但是它默认允许所有的流量(不管防火墙是否是无效的) 关于ipta ...

  7. 【Oracle】ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务

    早上使用PL/SQL连接Oracle的时候,报错如下 解决办法: 找到文件listener.ora,新增以下红色区域,注意:路径需要根据自己的Oracle安装路径自行设置 # listener.ora ...

  8. python之函数用法divmod

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法divmod #http://python.jobbole.com/81480/ #d ...

  9. raise语句

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #Python学习手册 868 #raise语句 res=[IndexError,TypeError] #ra ...

  10. 安装mysql 和 apache

    一. 安装apache服务器 1. 检查apache服务器是否安装 #service httpd status 2. 如提示未被识别的服务,则表明组件未安装,需手动安装 #yum install ht ...