概要
- 防盗链
- 根据文件类型设置过期时间
- 静态资源访问
- 日志配置
- 日志字段说明
- access_log 访问日志
- error_log 日志
- 日志切割
- 反向代理
- 禁止指定user_agent
- nginx访问控制
- 负载均衡
防盗链
location ~* \.(gif|jpg|png)$ {
根据文件类型设置过期时间
location ~.*\.css$ {
expires 1d;
break;
}
location ~.*\.js$ {
expires 1d;
break;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
access_log off;
expires 15d;
静态资源访问
http {
日志配置
日志字段说明
字段 | 说明 | |
remote_addr 和 http_x_forwarded_for |
客户端 IP 地址 |
remote_user |
客户端用户名称 |
request |
请求的 URI 和 HTTP 协议 |
status |
请求状态 |
body_bytes_sent |
返回给客户端的字节数,不包括响应头的大小 |
bytes_sent |
返回给客户端总字节数 |
connection |
连接的序列号 |
connection_requests |
当前同一个 TCP 连接的的请求数量 |
msec |
日志写入时间。单位为秒,精度是毫秒 |
pipe |
如果请求是通过HTTP流水线(pipelined)发送,pipe值为“p”,否则为“.” |
http_referer |
记录从哪个页面链接访问过来的 |
http_user_agent |
记录客户端浏览器相关信息 |
request_length |
请求的长度(包括请求行,请求头和请求正文) |
time_iso8601 |
ISO8601标准格式下的本地时间 |
time_local |
记录访问时间与时区 |
access_log 访问日志
http {
log_format access '$remote_addr - $remote_user [$time_local] $host "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$clientip"';
access_log /srv/log/nginx/talk-fun.access.log access;
}复制代码
error_log 日志
error_log /srv/log/nginx/nginx_error.log error;
日志切割
# 和apache不同的是,nginx没有apache一样的工具做切割,需要编写脚本实现。
反向代理
http {
include mime.types;
server_tokens off;
禁止指定user_agent
nginx访问控制
负载均衡
http {
upstream test.net {
ip_hash;
server 192.168.10.13:80;
server 192.168.10.14:80 down;
server 192.168.10.15:8009 max_fails=3 fail_timeout=20s;
server 192.168.10.16:8080;
}
server {
location / {
proxy_pass http://test.net;
}
}
}