if ($host = 'bbs.gitlib.com') {
rewrite ^/$ http://bbs.bliwan.com permanent;
}
log_format access '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" $http_x_forwarded_for';
输入格式:
14.18.29.118 - - [24/Jun/2017:20:53:09 +0800] "GET /index.html HTTP/1.1" 200 23 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" -
return形式
# 301永久重定向,302临时重定向
return 301 https://example.com$request_uri;
# return 返回形式
return code;
return code URL;
return URL;
rewrite形式
rewrite ^/$ http://bbs.gitlib.com permanent;
rewrite flag说明:
if判断
if ($http_user_agent ~ (125LA|WinHttpRequest|360Spider)) {
return 444;
}
if ($http_referer ~* "filter=author&orderby=dateline") {
return 444;
}
if ($host = 'bbs.gitlib.com') {
rewrite ^/$ http://bbs1.gitlib.com permanent;
}
比较符说明:
set设置变量
if ( $host ~* (.*)\.yzz\.cn) {
set $domain $1;
}
root /www/website/www/gitlib/$domain/;
# set语法
set variable value;
proxy_pass
proxy_pass将请求传递给HTTP服务器
proxy_buffering: on; # 设置是否开启Proxy Buffer,默认为on
proxy_pass http://upload.gitlib.com; # 设置被代理服务器的地址
proxy_connect_timeout 600;# 设置Nginx服务器与后端被代理服务器尝试建立连接的超时时间,默认为60s
proxy_read_timeout 600; # 设置Nginx服务器向后端被代理服务器发出read请求后,等待响应的超时时间,默认为60s
proxy_send_timeout 600; # 设置Nginx服务器向后端被代理服务器发出write请求后,等待响应的超时时间,默认为60s
proxy_buffer_size 8k; # 设置Nginx服务器从被代理服务器获取的第一段数据buffer大小,一般和proxy_buffers设置的buffer大小一致,或者更小, 默认为4k或者8k
proxy_buffers 4 32k; # 设置Proxy Buffer的个数和每个Buffer的大小
proxy_busy_buffers_size 64k; # 设置处在Busy状态的Buffer总大小上限,默认为8K或者16K
proxy_temp_file_write_size 64k; #
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404; # upstream设置被代理服务器集群时,设置组内服务器出现哪些异常时,可以依次轮询到下一个组内服务器处理
proxy_redirect off; # 修改响应头Location值,off表示直接返回proxy_pass后的值,默认为default(客户端请求的URI),
fastcgi_pass
fastcgi_pass将请求传递给FastCGI服务器
fastcgi_connect_timeout 3000;
fastcgi_send_timeout 3000;
fastcgi_read_timeout 3000;
fastcgi_buffer_size 200k;
fastcgi_buffers 8 200k;
fastcgi_busy_buffers_size 200k;
fastcgi_max_temp_file_size:1024M; # 设置临时文件大小,默认为1024M
fastcgi_temp_file_write_size 200k;# 配置同时写入临时文件的数据量的大小,合理的配置可以避免磁盘IO负载过高,导致系统性能下降,默认为8KB或16KB
fastcgi_temp_path /dev/shm; # 配置磁盘上的一个文件路径,用于临时存放代理服务器的大体积响应数据,如果proxy buffer 被装满后,响应数据仍然没有被Nginx服务器完全接收,响应数据就被会临时存放在该文件中
uwsgi_pass
uwsgi_pass将请求传递给uwsgi服务器(如python服务)
scgi_pass
scgi_pass将请求传递给SCGI服务器
memcached_pass
memcached_pass将请求传递给memcached服务器
Nginx通过proxy_pass和upstream指令实现负载均衡,Nginx原生支持的负载均衡算法有如下几种:
轮询
每个请求按时间顺序逐一分配到不同的应用服务器,如果应用服务器down掉,自动剔除,剩下的继续轮询
权重
通过配置权重,指定轮询几率,权重和访问比率成正比,用于应用服务器性能不均的情况
ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个应用服务器,可以解决session共享的问题。
least_conn
最小连接数
示例:
upstream php {
server 127.0.0.1:9000 max_fails=3 fail_timeout=30s;
server 192.168.1.16 backup;
server 192.168.1.17 down;
}
upstream php {
server 192.168.10.2 weight=1;
server 192.168.10.3 weight=2;
}
upstream php {
ip_hash;
server 192.168.10.16;
server 192.168.10.17;
}
Proxy Cache
Nginx通过proxy_cache来实现缓存。Buffer和Cache都是用于提供IO吞吐小路的,但是概念不同。Buffer(缓冲)主要用于传输效率不同步或者优先级不相同的设备之间传输数据,一般通过对一方数据进行临时存放,再统一发送的办法传递给另一方,以降低进程之间的等待时间,保证速度较快的进程不发生间断,临时存放的数据一旦传送给另一方,这些数据本身也就没有用处了;Cache(缓存)主要用于将硬盘上已有的数据在内存中建立缓存数据,提高数据的访问效率,对于过期不用的缓存可以随时销毁。
Proxy Cache机制依赖于Proxy Buffer机制,只有在Proxy Buffer机制开启的情况下Proxy Cache的配置才会发挥作用。
相关配置参数说明:
示例
proxy_cache_path /data/nginx_cache/ levels=1:2 keys_zone=my_zone:10m inactive=300s max_size=5g;
location / {
proxy_cache my_zone;
proxy_pass http://192.168.10.110:8080/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
Expires
location ~ .*\.(jpg|jpeg|gif|png|ico|swf)?$
{
expires 1d; #设置过期时间
}
通过gzip相关指令可以配置Gzip压缩,对响应数据进行在线实时压缩。
相关配置参数说明:
示例:
gzip on;
gzip_static on;
gzip_comp_level 9;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_http_version 1.0;
gzip_vary on;
gzip_proxied any;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript;
events {
use epoll; # 事务模型
worker_connections 20000; # 一个nginx进程的连接数,nginx服务器允许的同事连接的客户端最大数量Client = worker_processes * worker_connections/2;
}
依据UA屏蔽爬虫
if ($http_user_agent ~* "qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Feedfetcher-Google|Yahoo! Slurp|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou
spider|Sogou web spider|MSNBot|ia_archiver|Tomato Bot") {
return 403;
}
屏蔽IP访问
allow 133.27.182.82;
allow 113.106.18.0/24;
allow 121.201.104.0/24;
deny all;
使用Auth权限访问
auth_basic "bbs-auth";
auth_basic_user_file /usr/local/nginx/conf/bbsauthpwd;
限制带宽
# 用户下载达到 500k 后,便控制其速度在 50k 以内
location /download/ {
limit_rate_after 500k;
limit_rate 50k;
}
限制连接
# 定义了一个名为“down”,10M大小,以连接IP为key的连接数据存储空间
limit_conn_zone $binary_remote_addr zone=down:10m;
# 读取名为`down`连接数据存储空间的数据,限制每个key(上面是以ip作为IP) 最大同时连接数为4
location ~ .*\.(rar|zip|apk)?$ {
limit_conn down 4;
limit_rate 150k;
}
limit_conn_log_level notice: 指定当触发limit的时候日志打印级别
限制请求
# 定义一个名为”one”, 10M大小,每秒1个请求的请求数据存储空间
limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;
# 引用名为“one”的存储空间,burst为等待请求数量数,当等待请求数量超过50个时,则抛出503错误,nodelay 针对的是 burst 参数,burst=50 nodelay 表示这50个请求立马处理,不能延迟,相当于特事特办。不过,即使这20个突发请求立马处理结束,后续来了请求也不会立马处理。burst=50 相当于缓存队列中占了50个坑,即使请求被处理了,这20个位置这只能按 100ms一个来释放
limit_req zone=one burst=50 nodelay;
limit_req_log_level notice: 指定当触发limit的时候日志打印级别
实时显示Nginx运行状况
在安装nginx是编译http_stub_status_module即可,使用参数为–with-http_stub_status_module
location /ngx_status {
stub_status on;
access_log on;
}
设置错误页面
error_page 404 /404.html
Nginx 常见错误码