#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;
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运算,这样每一个固定的访客都会被负载到后端固定的机器nginx
upstream django { ip_hash; server 192.168.21.128:81 server 192.168.21.131:81 }
当前面的都访问不到,则请求backup的备份,只要有一个通,则不会走backupshell
upstream django { server 192.168.21.128:81; server 192.168.21.131:81; server 192.168.21.131:82 backup; }
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 ] } 优先级 = > 完整路径 > ^~ > /
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; }
location /status { stub_status on; }
gzip on 提升响应速度,节省带宽
django自带的wsgiref 在调试模式下使用的wsgi的文件,网关接口,协议django
uwsgi:协议json
uWSGI:具体实现方式后端
pip3 install uwsgi -i https://pypi.douban.com/simple
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 }