## nginx的日志

```SHELL
 #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
```

## 禁止访问

```SHELL
可以写在server或者location里面
deny 192.168.21.1;
allow 192.168.21.131;
deny 192.168.21.0/24;

```

## 反向代理

- 起到保护网站安全的作用
- 可以缓存静态文件
- 实现负载均衡 F5 A10 lvs haproxy  nginx

```SHELL
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

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

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

### backup

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

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

## nginx location匹配规则

```SHELL
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分离

```SHELL
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

```SHELL
location /status {
   stub_status on;
}
```

vi /etc/hosts
tail -f logs/access.log

## 压缩

```shell
gzip on
提高响应速度,节省带宽
```

## WSGI

django自带的wsgiref 在调试模式下使用的wsgi的文件,网关接口,协议

uwsgi:协议

uWSGI:具体实现方式

### 安装

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

### 准备django程序
先创建项目
python3 manage.py runserver 0.0.0.0:8080
### 启动

```SHELL
启动方式
cd django目录
uwsgi --http :8080 --module mysite.wsgi
```

配置文件格式

```shell
conf
py
cnf
xml
json
ini
yaml
```

配置文件启动

```SHELL
[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
uwsgi --ini /etc/uwsgi.ini

```

nginx的配置文件

sudo /opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf
vim /etc/uwsgi.ini
ll /data/supcrm/super.sock

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

在django的配置中要写入

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

执行命令

```SHELL
python3 manager.py collectstatic #用来收集静态文件
```

### 第二种配置方式

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

### 第三种配置方式 适合本地

```shell
uwsgi
socket = file.sock
nginx的配置文件  vim /etc/uwsgi.ini
location /{
   include uwsgi_params;
   uwsgi_pass unix://file.sock
}
```
killall -9 uwsgi
uwsgi --ini /etc/uwsgi.ini
ll /data/supcrm/super.sock

mysql 远程连接
Host列指定了允许用户登录所使用的IP,比如user=root Host=192.168.1.1。这里的意思就是说root用户只能通过192.168.1.1的客户端去访问。 user=root Host=localhost,表示只能通过本机客户端去访问。而%是个通配符,如果Host=192.168.1.%,那么就表示只要是IP地址前缀为“192.168.1.”的客户端都可以连接。如果Host=%,表示所有IP都有连接权限。 

注意:在生产环境下不能为了省事将host设置为%,这样做会存在安全问题,具体的设置可以根据生产环境的IP进行设置;

 

update user set host = '%' where user ='root';
Host设置了“%”后便可以允许远程访问。



![1567486021036](C:\Users\oldboy\AppData\Roaming\Typora\typora-user-images\1567486021036.png)


vim conf/nginx.conf

yum install -y python36 python36-pip python36-devel
yum install -y gcc
pip3 install uwsgi django==1.11 pymysql -i http://pypi.douban.com/simple
cd /data/
迁移项目
cd 项目
python3 manage.py runserver 0.0.0.0:8001

pip3 install django-multiselectfield -i http://pypi.douban.com/simple
uwsgi --http :8001 --module 项目.wsgi

vim /etc/uwsgi.ini 配置文件