user nobody;javascript
worker_processes 4;css
error_log logs/error.log;html
events { # use epoll; # use:指定nginx的工做模式,能够省略。 worker_connections 4096; # worker_connections:定义nginx每一个进程的最大链接数,默认1024。 }java
http { include mime.types; default_type application/octet-stream;node
# 设置日志的格式 # $remote_addr 与 $http_x_forwarded_for 记录客户端的ip地址 # $remote_user:客户端用户名称 # $request:请求的url log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; # 启动高效传输文件的模式 # 开启sendfile后,nginx在传输文件时直接在磁盘和tcp socket之间传输数据,整个过程都是在内核中完成。 # nginx中的sendfile相似于javaNIO中的直接缓冲区。 sendfile on; # 设置http链接的超时时间,默认为75s # 超过该值后,nginx会断掉当前的http链接 # 若该值设置的太小,则在上传大文件时可能会由于链接超时而致使上传失败 # 若该值设置的过大,则在接口访问完成后http链接没法被及时释放掉,从而致使链接数愈来愈大,最终致使nginx奔溃。 keepalive_timeout 120; limit_rate_after 3m; # 链接下载了3m后,再进行限速。 limit_rate 512k; # 对每一个链接限速512k # 开启gzip压缩,以减小带宽,提升页面加载速度。 gzip on; gzip_min_length 1k; # 大于1k的数据才会去压缩 gzip_buffers 4 16k; # 设置用于处理请求压缩的缓冲区数量和大小 gzip_http_version 1.0; # 指定gzip支持的http协议的版本。1.0表示支持1.0以上(包括1.0)的版本。注:经过proxy_pass进行反向代理时,nginx和后端的upstream server之间默认是用HTTP/1.0协议进行通讯的 gzip_comp_level 2; # 压缩率,取值为1-9,压缩率越大则压缩的越完全,可是须要消耗更多的cpu gzip_types text/plain application/x-javascript application/javascript text/javascript text/css application/xml; # 须要进行压缩的数据类型 gzip_vary on; # 告诉客户端nginx在传送数据时使用了gzip压缩,即response中的Vary: Accept-Encoding。 ####### 分布式限流 ####### limit_req_zone $binary_remote_addr zone=per_ip_req:10m rate=10r/s; # 概念:limit_req_zone是采用漏桶算法来限制单位时间内的请求数,即速率限制。 # 第一个参数:$binary_remote_addr 表示经过remote_addr这个标识来作限制,即限制同一客户端ip的请求("binary_"的目的是为了缩写内存占用量)。 # 第二个参数:zone=per_ip_req:10m 表示生成一个大小为10M,名字为per_ip_req的内存区域,用来存储访问的频次信息。 # 第三个参数:rate=10r/s 表示同一客户端ip的最大访问频次。(r/s 即 request/second) limit_conn_zone $binary_remote_addr zone=perip_conn:10m; # 概念:limit_req_conn是用来限制同一时间内的链接数,即并发限制。 # 用来限制同一ip在同一时间内的链接数。 limit_conn_zone $server_name zone=perserver_conn:10m; # 用来限制同一server在同一时间内的链接数。 ########################### # 定义web服务器 server { listen 80; server_name localhost; location / { return 403; } location /status { stub_status on; access_log off; } }
include /usr/local/nginx/conf/vhost/*.conf; }nginx
########web
######## upstream advertise.conf { ip_hash; # 对请求的ip作hash运算,根据ip的hash值将请求分配到指定的服务器上。 server 192.168.0.1:8080 max_fails=3 fail_timeout=3s; # ip_hash下weight会失效。 server 192.168.0.2:8080 max_fails=3 fail_timeout=3s; }算法
########后端
######## upstream landpage.conf { server 192.168.1.1:8080 max_fails=3 fail_timeout=3s; server 192.168.1.2:8080 max_fails=3 fail_timeout=3s; }缓存
########
######## upstream seckill.conf { fair; server 192.168.2.1:8080 weight=1 max_fails=2 fail_timeout=30s; server 192.168.2.2:8080 weight=1 max_fails=2 fail_timeout=30s; server 192.168.2.3:8080 weight=1 max_fails=2 fail_timeout=30s backup; # 备用服务,当其它服务都down或者忙时,请求会打到备用服务。 server 192.168.2.4:8080 weight=1 max_fails=2 fail_timeout=30s down; # 表示该服务暂时不接受请求。 }
########
######## upstream cache.conf { server 192.168.3.1:8080 weight=1 max_fails=2 fail_timeout=30s; server 192.168.4.2:8080 weight=1 max_fails=2 fail_timeout=30s; hash $request_uri; hash_method crc32; }
server { listen 80; server_name seckill.jxn.com; index index.html index.htm index.jsp ;
# 对URL进行匹配,能够进行重定向或者进行新的代理 location / { proxy_pass http://seckill; ######## 对该server进行限流 ######## limit_req zone=per_ip_req burst=5 nodelay; # 该server上对请求进行限流,burst表示漏桶(缓存区)的大小,默认是0; # 当请求的速率超过限制的速率(rate)时,nginx会将超过速率限制的请求放到漏桶中(超时)等待,若是漏桶满了,则nginx直接返回503,即客户端获得一个服务器忙的响应。 # nodelay表示漏桶已满时直接返回503。注意:若是不设置nodelay,则咱们必须设置rate,若两者都没设置,那么全部的请求都会排队等待。 limit_conn perip_conn 2; # 该server容许同一个ip同时发起的最大链接数 limit_conn perserver_conn 1000; # 该server在同一时间容许的最大链接数 limit_rate 100k; # 每一个链接(下载)限速100k }
}
server { listen 80; server_name speed.jxn.com; index index.html index.htm index.jsp ;
# root /data/tomcat/tomcat-speed/webapps/; location ~ ^/WEB-INF/* { deny all; } error_page 404 /404.html; # redirect server error pages to the static page /50x.html error_page 500 502 503 504 /50x.html; location / { index index.jsp; proxy_pass http://advertise.conf; proxy_redirect off; # 将代理服务器收到的用户的信息传到真实服务器上 proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $http_x_forwarded_for; proxy_set_header Host $host; 0 client_max_body_size 10m; client_body_buffer_size 128k; proxy_buffers 32 4k; proxy_connect_timeout 600; proxy_send_timeout 600; proxy_read_timeout 600; } location /landpage/ { index index.jsp; proxy_pass http://landpage.conf; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $http_x_forwarded_for; proxy_set_header Host $host; # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; client_body_buffer_size 128k; proxy_buffers 32 4k; proxy_connect_timeout 600; proxy_send_timeout 600; proxy_read_timeout 600; } # expires 1h; access_log /usr/local/nginx/logs/advertise.access.log; error_log /usr/local/nginx/logs/advertise-error.log;
}