nginx 通常用做请求转发,用做服务器集群的负载均衡php
典型的高并发集群是 nginx+tomcat(多个)html
nginx能够高效处理对静态文件的请求,tomcat 负责动态请求node
配置范例:mysql
#user nobody; worker_processes 1; error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; 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; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; gzip on; proxy_connect_timeout 600; #nginx跟后端服务器链接时超时时间 proxy_read_timeout 600; ##链接成功后,后端服务器响应时间(代理接收超时) proxy_send_timeout 600; #后端服务器数据回传时间(代理发送超时) proxy_buffer_size 64k; ##设置代理服务器(nginx)保存用户头信息的缓冲区大小 proxy_buffers 4 64k; #proxy_buffers缓冲区,网页平均在32k如下的话,这样设置 proxy_busy_buffers_size 128k; # proxy_temp_file_write_size 128k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传 upstream youyiskyweb { sticky; #粘性session #server 172.16.129.121:1337; #server 172.16.129.155:1337; server 171.121.14.198:1337; server 171.121.14.215:1337; } upstream youyiskyapi { #server 171.121.14.213:8080; server 171.121.14.214:8080; #server 171.121.14.215:8080; } limit_req_status 599; #返回状态吗 limit_req_zone $binary_remote_addr zone=allips:10m rate=60r/s; #限制ip请求频率 server { listen 80; server_name youyisky; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://youyiskyweb; limit_req zone=allips burst=5 nodelay; #root html; #index index.html index.htm; } location /nginx_status { stub_status on; access_log off; #allow SOME.IP.ADD.RESS; #deny all; } location /htnewsroom { proxy_pass http://youyiskyapi; #limit_req zone=allips burst=5 nodelay; } location /user_portrait { proxy_pass http://171.121.14.213:8080/zk_image/user_portrait; } # Directives # check #syntax: *check interval=milliseconds [fall=count] [rise=count] #[timeout=milliseconds] [default_down=true|false] #[type=tcp|http|ssl_hello|mysql|ajp|fastcgi]* #default: *none, if parameters omitted, default parameters are #interval=30000 fall=5 rise=2 timeout=1000 default_down=true type=tcp* # context: *upstream* #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; # server_name localhost; # ssl on; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_timeout 5m; # ssl_protocols SSLv2 SSLv3 TLSv1; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
负载均衡和热备份nginx
粘性session的负载均衡,须要安装对应的模块,即从新编译安装,ip_hash 也能够作到粘性session 效果但二者本质不一样web
nginx 从新编译安装须要把须要的模块一次性配置好,能够指定安装目录sql
nginx 负载均衡和热备份是两种配置,负载均衡也能够实现热备份的效果,对于宕机,后台集群的某台机器的tomcat挂掉,nginx不会继续向其转发,若是是tomcat正常,api 报错,则nginx 识别正常
shell
nginx监控后端
nginx 自己自带监控,简单的页面,研究过taobao 开发的负载监控,发现编译安装不成功api
nginx 转发状况能够经过开启日志监控查看
ip限制请求次数
此配置能够达到限制ip访问频率的效果,但没有返回自定义状态码
总结:nginx经常使用与负载均衡,能够根据业务须要制定多种负载均衡策略,能够识别网络爬虫,制定防采集策略
对于热备份,经常使用其余方式实现。