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經常使用與負載均衡,能夠根據業務須要制定多種負載均衡策略,能夠識別網絡爬蟲,制定防採集策略
對於熱備份,經常使用其餘方式實現。