nginx負載均衡二:配置
阿新 • • 發佈:2018-08-24
isp p s tac spl and add none ssl hide
配置方法一(可用):
upstream tomcatserver1 { server 192.168.70.170 weight=3; server 192.168.70.172;
server 192.168.70.173 down;
server 192.168.70.174;
server 192.168.70.175 backup; } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://tomcatserver1
; index index.html index.htm; } }
通過以上配置,便可以實現,在訪問192.168.70.169這個網站時,由於配置了proxy_pass地址,所有請求都會先通過nginx反向代理服務器,在服務器將請求轉發給目的主機時,讀取upstream為 tomcatsever1的地址,讀取分發策略,配置tomcat1權重為3,所以nginx會將大部分請求發送給49服務器上的tomcat1,也就是192.168.70.170;較少部分給tomcat2來實現有條件的負載均衡,當然這個條件就是服務器1、2的硬件指數處理請求能力。
1)down:表示單前的server暫時不參與負載
2)Weight:默認為1.weight越大,負載的權重就越大。
3)max_fails:允許請求失敗的次數默認為1.當超過最大次數時,返回proxy_next_upstream 模塊定義的錯誤
4)fail_timeout:max_fails 次失敗後,暫停的時間。
5)Backup:其它所有的非backup機器down或者忙的時候,請求backup機器。所以這臺機器壓力會最輕。
配置方法二(可用):
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logsView Code/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; // 位置1:upstream upstream 192.168.70.169 { server 192.168.70.170; server 192.168.70.172; #ip_hash; } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm index.php; } #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; fastcgi_param SCRIPT_FILENAME /$document_root$fastcgi_script_name; //位置2:proxy_pass proxy_pass http://192.168.70.169; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 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; # } #} }
nginx負載均衡二:配置