配置
代理
配置示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
| # kencery 注释说明Nginx文件 # 时间:2016-1-19 # 学习内容,只是来自互联网,有版权问题请联系我删除。
######## Nginx的main(全局配置)文件 #指定nginx运行的用户及用户组,默认为nobody #user nobody; #开启的线程数,一般跟逻辑CPU核数一致 worker_processes 1;
#定位全局错误日志文件,级别以notice显示,还有debug,info,warn,error,crit模式 ##debug输出最多,crir输出最少,根据实际环境而定 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info;`
#指定进程id的存储文件位置 #pid logs/nginx.pid;
#指定一个nginx进程打开的最多文件描述符数目,受系统进程的最大打开文件数量限制 #worker_rlimit_nofile 65535
events { #设置工作模式为epoll,除此之外还有select,poll,kqueue,rtsig和/dev/poll模式 #use epoll;
#定义每个进程的最大连接数,受系统进程的最大打开文件数量限制。 worker_connections 1024; }
#######Nginx的Http服务器配置,Gzip配置 http { #主模块指令,实现对配置文件所包含的文件的设定,可以减少主配置文件的复杂度,DNS主配置文件中的zonerfc1912,acl基本上都是用include语句。 include mime.types;
#核心模块指令,智力默认设置为二进制流,也就是当文件类型未定义时使用这种方式 default_type application/octet-stream;
#下面代码为日志格式的设定,main为日志格式的名称,可自行设置,后面引用 #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"';
#引用日志main #access_log logs/access.log main;
#设置允许客户端请求的最大的单个文件字节数 #client_max_body_size 20M; #指定来自客户端请求头的headebuffer大小 #client_header_buffer_size 32k; #指定连接请求试图写入缓存文件的目录路径 #client_body_temp_path /dev/shm/client_body_temp; #指定客户端请求中较大的消息头的缓存最大数量和大小,目前设置为4个32KB #large client_header_buffers 4 32k;
#开启高效文件传输模式 sendfile on; #开启防止网络阻塞 #tcp_nopush on; #开启防止网络阻塞 #tcp_nodelay on;
#设置客户端连接保存活动的超时时间 #keepalive_timeout 0; keepalive_timeout 65;
#设置客户端请求读取超时时间 #client_header_timeout 10; #设置客户端请求主体读取超时时间 #client_body_timeout 10; #用于设置相应客户端的超时时间 #send_timeout
####HttpGZip模块配置 #httpGzip modules #开启gzip压缩 #gzip on; #设置允许压缩的页面最小字节数 #gzip_min_length 1k; #申请4个单位为16K的内存作为压缩结果流缓存 #gzip_buffers 4 16k; #设置识别http协议的版本,默认为1.1 #gzip_http_version 1.1; #指定gzip压缩比,1-9数字越小,压缩比越小,速度越快 #gzip_comp_level 2; #指定压缩的类型 #gzip_types text/plain application/x-javascript text/css application/xml; #让前端的缓存服务器进过gzip压缩的页面 #gzip_vary on; upstream aProxy{ server 127.0.0.1:8181; server 127.0.0.1:8282; } #########Nginx的server虚拟主机配置 server { listen 80; server_name uucs.wenxiaomiao.com; #charset koi8-r; #access_log logs/host.access.log main; location ~ /* { proxy_pass http://localhost:6001; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { #监听端口为 80 listen 80;
#设置主机域名 server_name localhost;
#设置访问的语言编码 #charset koi8-r;
#设置虚拟主机访问日志的存放路径及日志的格式为main #access_log logs/host.access.log main;
#设置虚拟主机的基本信息 location / { #设置虚拟主机的网站根目录 root /home/uucs/applications/nginx/web;
#设置虚拟主机默认访问的网页 index index.html index.htm; } location ^~ /api/ { rewrite "^/api/(.*)$" /admin/$1 break; proxy_pass http://localhost:80; proxy_set_header Host $host; } location ^~ /juhe/ { #echo 'juhe pipei chenggong'; proxy_pass http://apis.juhe.cn; ## 重写地址 /juhe ----> http://apis.juhe.cn rewrite ^.+juhe/?(.*)$ /$1 break; }
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }
# proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #}
# deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
# another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias;
# location / { # root html; # index index.html index.htm; # } #}
# HTTPS server # #server { # listen 443 ssl; # server_name localhost;
# ssl_certificate cert.pem; # ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on;
# location / { # root html; # index index.html index.htm; # } #}
}
|
配置说明
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
location [=|~|~*|^~] uri { … }
location ~ .*\.(php|php5)?$ { root /var/www/html; …… }
location = /news/ { } location ~ \.(html) { } location ~* \.(html){ } location ^~ /index/ { } location / { }
|
路径重写
限流
限流配置
1、正常限制访问频率(正常流量)
限制一个用户发送的请求,我Nginx多久接收一个请求。
Nginx中使用ngx_http_limit_req_module模块来限制的访问频率,限制的原理实质是基于漏桶算法原理来实现的。在nginx.conf配置文件中可以使用limit_req_zone命令及limit_req命令限制单个IP的请求处理频率。
定义限流维度,一个用户一分钟一个请求进来,多余的全部漏掉
1 2 3 4 5 6 7 8
| limit_req_zone $binary_remote_addr zone=one:10m rate=1r/m;
server{ location/seckill.html{ limit_req zone=zone; proxy_pass http://lj_seckill; } }
|
1r/s代表1秒一个请求,1r/m一分钟接收一个请求, 如果Nginx这时还有别人的请求没有处理完,Nginx就会拒绝处理该用户请求。
2、突发限制访问频率(突发流量)
限制一个用户发送的请求,我Nginx多久接收一个。
上面的配置一定程度可以限制访问频率,但是也存在着一个问题:如果突发流量超出请求被拒绝处理,无法处理活动时候的突发流量,这时候应该如何进一步处理呢?Nginx提供burst参数结合nodelay参数可以解决流量突发的问题,可以设置能处理的超过设置的请求数外能额外处理的请求数。我们可以将之前的例子添加burst参数以及nodelay参数:
定义限流维度,一个用户一分钟一个请求进来,多余的全部漏掉
1 2 3 4 5 6 7 8
| limit_req_zone $binary_remote_addr zone=one:10m rate=1r/m;
server{ location/seckill.html{ limit_req zone=zone burst=5 nodelay; proxy_pass http://lj_seckill; } }
|
为什么就多了一个 burst=5 nodelay; 呢,多了这个可以代表Nginx对于一个用户的请求会立即处理前五个,多余的就慢慢来落,没有其他用户的请求我就处理你的,有其他的请求的话我Nginx就漏掉不接受你的请求
3、 限制并发连接数
Nginx中的ngx_http_limit_conn_module模块提供了限制并发连接数的功能,可以使用limit_conn_zone指令以及limit_conn执行进行配置。接下来我们可以通过一个简单的例子来看下:
1 2 3 4 5 6 7 8 9 10 11 12
| http { limit_conn_zone $binary_remote_addr zone=myip:10m; limit_conn_zone $server_name zone=myServerName:10m; server { location / { limit_conn myip 10; limit_conn myServerName 100; rewrite / http://www.lijie.net permanent; } } }
|
上面配置了单个IP同时并发连接数最多只能10个连接,并且设置了整个虚拟服务器同时最大并发数最多只能100个链接。当然,只有当请求的header被服务器处理后,虚拟服务器的连接数才会计数。刚才有提到过Nginx是基于漏桶算法原理实现的,实际上限流一般都是基于漏桶算法和令牌桶算法实现的。
引用
引用
日志
Nginx日志配置位于conf 的http 块下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| http { log_format main '----------------- \n' '[$remote_addr] [$request][$status] referer:[$http_referer] \n ' ' TIME:[$time_local] bytes:[$body_bytes_sent] USER:[$remote_user] ' 'forwarded:[$http_x_forwarded_for] \n' ' [$http_user_agent] ';
log_format json '{ "@timestamp": "$time_iso8601", ' '"remote_addr": "$remote_addr",' '"request": "$request",' '"upstr_addr": "$upstream_addr",' '"costime": "$request_time",' '"realtime": "$upstream_response_time",' '"status": $status,' '"x_forwarded": "$http_x_forwarded_for",' '"referer": "$http_referer",' '"bytes":$body_bytes_sent,' '"dm":$request_body,' '"agent": "$http_user_agent" }'; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| $http_x_forwarded_for $remote_user $time_local $request_method $scheme $host $request_uri $uri $query_string $server_protocol $status $body_bytes_sent $http_referer $http_user_agent $request_time $upstream_addr $request_id $upstream_response_time $arg_PARAMETER 客户端GET请求中PARAMETER 字段的值 $args 客户端请求中的参数 $binary_remote_addr 远程地址的二进制表示 $body_bytes_sent 已发送的消息体字节数 $content_length HTTP请求信息中content-length的字段 $content_type 请求信息中content-type字段 $cookie_COOKIE 客户端请求中COOKIE头域的值 $document_root 针对当前请求的根路径设置值 $document_uri 与$uri相同 $host 请求信息中的host头域,如果请求中没有Host行,则等于设置的服务器名 $http_HEADER HTTP请求信息里的HEADER地段 $http_host 与$host相同,但是如果请求信息中没有host行,则可能不同客户端cookie信息 $http_cookie 客户端cookie信息 $http_referer 客户端是从哪一个地址跳转过来的 $http_user_agent 客户端代理信息,也就是你客户端浏览器 $http_via 最后一个访问服务器的IP $http_x_forwarded_for 相当于访问网路访问的路径 $is_args 如果有args的值,则等于”?”,否则为空 $limit_rate 对连接速率的限制 $nginx_version 当前Nginx的版本 $pid 当前Nginx服务器的进程的进程ID $query_string 与$args相同 $remote_addr 客户端IP地址 $remote_port 客户端的端口 $remote_user 客户端的用户名,用于 auth basic module验证 $request 客户端请求 $request_body 客户端发送的报文体 $request_body_file 发送后端服务器的本地临时缓存文件的名称 $request_filename 当前请求的文件路径名,由root或alias指令与URI请求生成 $request_method 请求后端数据的方法,例如”GET”,”POST” $request_uri 请求的URI,带参数,不包含主机名 $scheme 所用的协议,如http或者HTTPS,比如rewrite^(.+)$$scheme://mysite.name$redirect $sent_http_cache_control 对应http请求头中的Cache-Control,需要打开chrome浏览器,右键检查,选中network,点中其中一个请求的资源 $sent_http_connection 对应http请求中的Connection $sent_http_content_type 对应http请求中的Content-Type $sent_last_modified 对应请求中的Last-Modified $server_addr 服务端的地址 $server_name 请求到达的服务器名 $server_port 请求到达服务器端口号 $server_protocol 请求协议的版本号,HTTP1.0/HTTP1.1 $uri 请求的不带请求参数的URI,可能和最初的值有不同,比如经过重定向之类的
|