天天看點

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

托管 django web 應用程式相當簡單,雖然它比标準的 php 應用程式更複雜一些。 讓 web 伺服器對接 django 的方法有很多。 gunicorn 就是其中最簡單的一個。

gunicorn(green unicorn 的縮寫)在你的 web 伺服器 django 之間作為中間伺服器使用,在這裡,web 伺服器就是 nginx。 gunicorn 服務于應用程式,而 nginx 處理靜态内容。

<a target="_blank"></a>

使用 pip 安裝 gunicorn 是超級簡單的。 如果你已經使用 virtualenv 搭建好了你的 django 項目,那麼你就有了 pip,并且應該熟悉 pip 的工作方式。 是以,在你的 virtualenv 中安裝 gunicorn。

<code>$ pip install gunicorn</code>

gunicorn 最有吸引力的一個地方就是它的配置非常簡單。處理配置最好的方法就是在 django 項目的根目錄下建立一個名叫 <code>gunicorn</code> 的檔案夾。然後在該檔案夾内,建立一個配置檔案。

在本篇教程中,配置檔案名稱是 <code>gunicorn-conf.py</code>。在該檔案中,建立類似于下面的配置:

<code>import multiprocessing</code>

<code></code>

<code>bind = 'unix:///tmp/gunicorn1.sock'</code>

<code>workers = multiprocessing.cpu_count() * 2 + 1</code>

<code>reload = true</code>

<code>daemon = true</code>

在上述配置的情況下,gunicorn 會在 <code>/tmp/</code> 目錄下建立一個名為 <code>gunicorn1.sock</code> 的 unix 套接字。 還會啟動一些工作程序,程序數量相當于 cpu 核心數量的 2 倍。 它還會自動重新加載并作為守護程序運作。

gunicorn 的運作指令有點長,指定了一些附加的配置項。 最重要的部分是将 gunicorn 指向你項目的 <code>.wsgi</code>檔案。

<code>gunicorn -c gunicorn/gunicorn-conf.py -d --error-logfile gunicorn/error.log yourproject.wsgi</code>

上面的指令應該從項目的根目錄運作。 <code>-c</code> 選項告訴 gunicorn 使用你建立的配置檔案。 <code>-d</code> 再次指定 gunicorn 為守護程序。 最後一部分指定 gunicorn 的錯誤日志檔案在你建立 <code>gunicorn</code> 檔案夾中的位置。 指令結束部分就是為 gunicorn 指定 <code>.wsgi</code> 檔案的位置。

現在 gunicorn 配置好了并且已經開始運作了,你可以設定 nginx 連接配接它,為你的靜态檔案提供服務。 本指南假定你已經配置好了 nginx,而且你通過它托管的站點使用了單獨的 server 塊。 它還将包括一些 ssl 資訊。

<code># 連接配接到 gunicorn</code>

<code>upstream yourproject-gunicorn {</code>

<code>server unix:/tmp/gunicorn1.sock fail_timeout=0;</code>

<code>}</code>

<code># 将未加密的流量重定向到加密的網站</code>

<code>server {</code>

<code>listen 80;</code>

<code>server_name yourwebsite.com;</code>

<code>return 301 https://yourwebsite.com$request_uri;</code>

<code># 主服務塊</code>

<code># 設定監聽的端口,指定監聽的域名</code>

<code>listen 443 default ssl;</code>

<code>client_max_body_size 4g;</code>

<code>server_name yourwebsite.com;</code>

<code># 指定日志位置</code>

<code>access_log /var/log/nginx/yourwebsite.access_log main;</code>

<code>error_log /var/log/nginx/yourwebsite.error_log info;</code>

<code># 告訴 nginx 你的 ssl 證書</code>

<code>ssl on;</code>

<code>ssl_certificate /etc/letsencrypt/live/yourwebsite.com/fullchain.pem;</code>

<code>ssl_certificate_key /etc/letsencrypt/live/yourwebsite.com/privkey.pem;</code>

<code># 設定根目錄</code>

<code>root /var/www/yourvirtualenv/yourproject;</code>

<code># 為 nginx 指定靜态檔案路徑</code>

<code>location /static/ {</code>

<code># autoindex the files to make them browsable if you want</code>

<code>autoindex on;</code>

<code># the location of your files</code>

<code>alias /var/www/yourvirtualenv/yourproject/static/;</code>

<code># set up caching for your static files</code>

<code>expires 1m;</code>

<code>access_log off;</code>

<code>add_header cache-control "public";</code>

<code>proxy_ignore_headers "set-cookie";</code>

<code># 為 nginx 指定你上傳檔案的路徑</code>

<code>location /media/ {</code>

<code>autoindex if you want</code>

<code># the location of your uploaded files</code>

<code>alias /var/www/yourvirtualenv/yourproject/media/;</code>

<code># set up aching for your uploaded files</code>

<code>location / {</code>

<code># try your static files first, then redirect to gunicorn</code>

<code>try_files $uri @proxy_to_app;</code>

<code># 将請求傳遞給 gunicorn</code>

<code>location @proxy_to_app {</code>

<code>proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;</code>

<code>proxy_set_header host $http_host;</code>

<code>proxy_redirect off;</code>

<code>proxy_pass http://njc-gunicorn;</code>

<code># 緩存 html、xml 和 json</code>

<code>location ~* \.(html?|xml|json)$ {</code>

<code>expires 1h;</code>

<code># 緩存所有其他的靜态資源</code>

<code>location ~* \.(jpg|jpeg|png|gif|ico|css|js|ttf|woff2)$ {</code>

配置檔案有點長,但是還可以更長一些。其中重點是指向 gunicorn 的 <code>upstream</code> 塊以及将流量傳遞給 gunicorn 的 <code>location</code> 塊。大多數其他的配置項都是可選,但是你應該按照一定的形式來配置。配置中的注釋應該可以幫助你了解具體細節。

儲存檔案之後,你可以重新開機 nginx,讓修改的配置生效。

<code># systemctl restart nginx</code>

一旦 nginx 線上生效,你的站點就可以通過域名通路了。

原文釋出時間為:2017-04-15

本文來自雲栖社群合作夥伴“linux中國”