nginx+uwsgi部署django項目

nginx的日誌

#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;
remote_addr 訪問ip地址
remote_user 訪問的用戶
time_local 本地時間
request 包括請求方式  請求地址  請求協議版本
status 狀態碼
body_bytes_sent 發送的大小
http_user_agent 用戶的請求頭
http_x_forwarded_for

禁止訪問

能夠寫在server或者location裏面
deny 192.168.21.1;
allow 192.168.21.131;
deny 192.168.21.0/24;

反向代理

  • 起到保護網站安全的做用
  • 能夠緩存靜態文件
  • 實現負載均衡 F5 A10 lvs haproxy nginx
upstream django {
        server 192.168.21.128:81;
}
 server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        proxy_pass http://django;
        }

權重

weightpython

upstream django {
	server 192.168.21.128:81 weight=3;
	server 192.168.21.131:81
}
 server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        proxy_pass http://django;
        }
        }
獲得的結果是:
訪問128的3次,才訪問131的一次

ip_hash

每一個請求的ip作hash運算,這樣每一個固定的訪客都會被負載到後端固定的機器nginx

upstream django {
	ip_hash;
	server 192.168.21.128:81
	server 192.168.21.131:81
}

backup

當前面的都訪問不到,則請求backup的備份,只要有一個通,則不會走backupshell

upstream django {
	server 192.168.21.128:81;
	server 192.168.21.131:81;
	server 192.168.21.131:82 backup;
}

nginx location匹配規則

location = / {
	精確匹配/ ,後面不能帶其餘的東西
    [ configuration A ]
}

location / {
	全部的以/開頭的地址
    [ configuration B ]
}

location /documents/ {
	只匹配/documents/
    [ configuration C ]
}

location ^~ /images/ {
	# 匹配以/images/開頭。
	~嚴格大小寫
    [ configuration D ]
}

location ~* \.(gif|jpg|jpeg)$ {
	以(gif|jpg|jpeg)結尾的文件
	~* 不區分大小寫
    [ configuration E ]
}
優先級
= > 完整路徑 > ^~ > /

location分離

server  {

        listen 80 ;
        server_name www.taobao.com taobao.com;
        location / {
        proxy_pass http://192.168.21.131:82;
        }
        location ~*\.(jpg|gif|png)$ {
        root /data/img;
        }

status

location /status {
	stub_status on;
}

壓縮

gzip on
提升響應速度,節省帶寬

WSGI

django自帶的wsgiref 在調試模式下使用的wsgi的文件,網關接口,協議django

uwsgi:協議json

uWSGI:具體實現方式後端

安裝

pip3 install uwsgi -i https://pypi.douban.com/simple

準備django程序

啓動

cd django目錄
uwsgi --http :8080 --module mysite.wsgi

配置文件格式緩存

conf
py
cnf
xml
json
ini
yaml

配置文件啓動安全

[uwsgi]
http = :8080
#項目路徑
chdir= /data/mysite
# uwsgi的文件
wsgi-file= mysite/wsgi.py
# 虛擬環境
# virtualenv = /root/env
# 進程個數
processes = 2
# 線程個數
threads=2
# 後臺啓動,指定日誌的輸出
daemonize=/data/mysite/django.log
# 清除臨時文件
vacuum = true
# python文件發生改變自動重啓
py-autoreload=1

uwsgi --ini file

nginx的配置文件負載均衡

server {
	listen 80;
	server_name crm.oldboy.com;
	location / {
		proxy_pass http://127.0.0.1:8080;
	}
	location /static {
		root /data/supercrm;
	}
}

在django的配置中要寫入socket

SATAIC_ROOT=os.path.join(BASE_DIR,'static/')

執行命令

python3 manager.py collectstatic #用來收集靜態文件

第二種配置方式

uwsgi
socket= :9090
nginx的配置文件
location / {
		include uwsgi_params;
		uwsgi_pass 127.0.0.1:8080;
}

第三種配置方式

uwsgi
socket = file.sock
nginx的配置文件
location /{
	include uwsgi_params;
	uwsgi_pass unix://file.sock
}

1567486021036

相關文章
相關標籤/搜索