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
提高响应速度,节省带宽
```