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;
        }

权重

weight

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运算,这样每个固定的访客都会被负载到后端固定的机器

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

backup

当前面的都访问不到,则请求backup的备份,只要有一个通,则不会走backup

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的文件,网关接口,协议

uwsgi:协议

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的配置中要写入

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

原文地址:https://www.cnblogs.com/python001-vip/p/12742186.html