自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何在Linux上使用Nginx和Gunicorn托管Django應(yīng)用

系統(tǒng) Linux 系統(tǒng)運(yùn)維
托管 Django Web 應(yīng)用程序相當(dāng)簡單,雖然它比標(biāo)準(zhǔn)的 PHP 應(yīng)用程序更復(fù)雜一些。 讓 Web 服務(wù)器對接 Django 的方法有很多。 Gunicorn 就是其中最簡單的一個。Gunicorn(Green Unicorn 的縮寫)在你的 Web 服務(wù)器 Django 之間作為中間服務(wù)器使用,在這里,Web 服務(wù)器就是 Nginx。 Gunicorn 服務(wù)于應(yīng)用程序,而 Nginx 處理靜態(tài)內(nèi)容。

[[189021]]

介紹

托管 Django Web 應(yīng)用程序相當(dāng)簡單,雖然它比標(biāo)準(zhǔn)的 PHP 應(yīng)用程序更復(fù)雜一些。 讓 Web 服務(wù)器對接 Django 的方法有很多。 Gunicorn 就是其中最簡單的一個。

Gunicorn(Green Unicorn 的縮寫)在你的 Web 服務(wù)器 Django 之間作為中間服務(wù)器使用,在這里,Web 服務(wù)器就是 Nginx。 Gunicorn 服務(wù)于應(yīng)用程序,而 Nginx 處理靜態(tài)內(nèi)容。

Gunicorn

安裝

使用 Pip 安裝 Gunicorn 是超級簡單的。 如果你已經(jīng)使用 virtualenv 搭建好了你的 Django 項目,那么你就有了 Pip,并且應(yīng)該熟悉 Pip 的工作方式。 所以,在你的 virtualenv 中安裝 Gunicorn。

  1. $ pip install gunicorn 

配置

Gunicorn 最有吸引力的一個地方就是它的配置非常簡單。處理配置***的方法就是在 Django 項目的根目錄下創(chuàng)建一個名叫 Gunicorn 的文件夾。然后在該文件夾內(nèi),創(chuàng)建一個配置文件。

在本篇教程中,配置文件名稱是 gunicorn-conf.py。在該文件中,創(chuàng)建類似于下面的配置:

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

在上述配置的情況下,Gunicorn 會在 /tmp/ 目錄下創(chuàng)建一個名為 gunicorn1.sock 的 Unix 套接字。 還會啟動一些工作進(jìn)程,進(jìn)程數(shù)量相當(dāng)于 CPU 內(nèi)核數(shù)量的 2 倍。 它還會自動重新加載并作為守護(hù)進(jìn)程運(yùn)行。

運(yùn)行

Gunicorn 的運(yùn)行命令有點長,指定了一些附加的配置項。 最重要的部分是將 Gunicorn 指向你項目的 .wsgi 文件。

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

上面的命令應(yīng)該從項目的根目錄運(yùn)行。 -c 選項告訴 Gunicorn 使用你創(chuàng)建的配置文件。 -D 再次指定 gunicorn 為守護(hù)進(jìn)程。 ***一部分指定 Gunicorn 的錯誤日志文件在你創(chuàng)建 Gunicorn 文件夾中的位置。 命令結(jié)束部分就是為 Gunicorn 指定 .wsgi 文件的位置。

Nginx

現(xiàn)在 Gunicorn 配置好了并且已經(jīng)開始運(yùn)行了,你可以設(shè)置 Nginx 連接它,為你的靜態(tài)文件提供服務(wù)。 本指南假定你已經(jīng)配置好了 Nginx,而且你通過它托管的站點使用了單獨的 server 塊。 它還將包括一些 SSL 信息。

如果你想知道如何讓你的網(wǎng)站獲得免費的 SSL 證書,請查看我們的 Let'sEncrypt 指南。

  1. # 連接到 Gunicorn 
  2. upstream yourproject-gunicorn { 
  3.     server unix:/tmp/gunicorn1.sock fail_timeout=0; 
  4. # 將未加密的流量重定向到加密的網(wǎng)站 
  5. server { 
  6.     listen       80; 
  7.     server_name  yourwebsite.com; 
  8.     return       301 https://yourwebsite.com$request_uri; 
  9. # 主服務(wù)塊 
  10. server { 
  11.     # 設(shè)置監(jiān)聽的端口,指定監(jiān)聽的域名 
  12.     listen 443 default ssl; 
  13.     client_max_body_size 4G; 
  14.     server_name yourwebsite.com; 
  15.     # 指定日志位置 
  16.     access_log /var/log/nginx/yourwebsite.access_log main; 
  17.     error_log /var/log/nginx/yourwebsite.error_log info; 
  18.     # 告訴 nginx 你的 ssl 證書 
  19.     ssl on
  20.     ssl_certificate /etc/letsencrypt/live/yourwebsite.com/fullchain.pem; 
  21.     ssl_certificate_key /etc/letsencrypt/live/yourwebsite.com/privkey.pem; 
  22.     # 設(shè)置根目錄 
  23.     root /var/www/yourvirtualenv/yourproject; 
  24.     # 為 Nginx 指定靜態(tài)文件路徑 
  25.     location /static/ { 
  26.         # Autoindex the files to make them browsable if you want 
  27.         autoindex on
  28.         # The location of your files 
  29.         alias /var/www/yourvirtualenv/yourproject/static/; 
  30.         # Set up caching for your static files 
  31.         expires 1M; 
  32.         access_log off
  33.         add_header Cache-Control "public"
  34.         proxy_ignore_headers "Set-Cookie"
  35.     } 
  36.     # 為 Nginx 指定你上傳文件的路徑 
  37.     location /media/ { 
  38.         Autoindex if you want 
  39.         autoindex on
  40.         # The location of your uploaded files 
  41.         alias /var/www/yourvirtualenv/yourproject/media/; 
  42.         # Set up aching for your uploaded files 
  43.         expires 1M; 
  44.         access_log off
  45.         add_header Cache-Control "public"
  46.         proxy_ignore_headers "Set-Cookie"
  47.     } 
  48.     location / { 
  49.         # Try your static files firstthen redirect to Gunicorn 
  50.         try_files $uri @proxy_to_app; 
  51.     } 
  52.     # 將請求傳遞給 Gunicorn 
  53.     location @proxy_to_app { 
  54.         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
  55.         proxy_set_header Host $http_host; 
  56.         proxy_redirect off
  57.         proxy_pass   http://njc-gunicorn; 
  58.     } 
  59.     # 緩存 HTML、XML 和 JSON 
  60.     location ~* \.(html?|xml|json)$ { 
  61.         expires 1h; 
  62.     } 
  63.     # 緩存所有其他的靜態(tài)資源 
  64.     location ~* \.(jpg|jpeg|png|gif|ico|css|js|ttf|woff2)$ { 
  65.         expires 1M; 
  66.         access_log off
  67.         add_header Cache-Control "public"
  68.         proxy_ignore_headers "Set-Cookie"
  69.     } 

配置文件有點長,但是還可以更長一些。其中重點是指向 Gunicorn 的 upstream 塊以及將流量傳遞給 Gunicorn 的 location 塊。大多數(shù)其他的配置項都是可選,但是你應(yīng)該按照一定的形式來配置。配置中的注釋應(yīng)該可以幫助你了解具體細(xì)節(jié)。

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

  1. # systemctl restart nginx 

一旦 Nginx 在線生效,你的站點就可以通過域名訪問了。

結(jié)語

如果你想深入研究,Nginx 可以做很多事情。但是,上面提供的配置是一個很好的開始,并且你可以用于實踐中。 如果你見慣了 Apache 和臃腫的 PHP 應(yīng)用程序,像這樣的服務(wù)器配置的速度應(yīng)該是一個驚喜。 

責(zé)任編輯:龐桂玉 來源: Linux中國
相關(guān)推薦

2019-01-07 09:50:06

Linuxtarball命令

2019-11-26 16:58:51

Linuxpkgsrc

2023-01-17 07:40:59

LinuxAppImage應(yīng)用程序

2017-05-10 09:40:57

Ubuntupm2Nginx

2022-09-19 08:45:52

Telnet系統(tǒng)Linux

2018-08-06 09:30:00

LinuxPbcopyPbpaste

2021-10-02 10:10:47

LinuxBusyBox命令

2020-08-24 12:37:54

Linuxxargs命令

2019-08-13 15:39:27

Linux應(yīng)用程序

2021-06-09 09:36:18

DjangoElasticSearLinux

2019-08-14 09:42:06

LinuxElasticsearKibana

2018-03-28 08:30:01

Linux倉庫應(yīng)用程序

2022-12-27 08:51:31

WebDebian 11

2009-04-16 10:15:34

Windows AzuSilverLight托管

2018-06-27 10:50:16

UbuntuexFAT驅(qū)動器

2022-08-12 08:38:52

FFmpegLinux命令

2020-11-22 07:20:15

LinuxEtcherUSB

2022-08-10 13:12:04

Linuxcat命令

2021-08-03 14:33:53

cron定時器Linux命令

2015-08-21 09:07:52

LinuxNMAP安全
點贊
收藏

51CTO技術(shù)棧公眾號