閱讀212 返回首頁    go 阿裏雲 go 技術社區[雲棲]


在 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中國”

最後更新:2017-05-22 14:33:59

  上一篇:go  京東技術架構(一)構建億級前端讀服務
  下一篇:go  如何在 Ubuntu 和 Linux Mint 上啟用桌麵共享