日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

nginx负载均衡简单配置

發(fā)布時間:2024/1/17 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 nginx负载均衡简单配置 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

nginx負載均衡簡單配置

準備三臺虛擬機來做這個實驗:

172.16.160.99? ? ? ? web服務器
172.16.160.103? ? ? web服務器
172.16.160.98? ? ? ? 負載均衡服務器

首先三臺電腦預裝nginx軟件:?

1、yum安裝nginx

yum install nginx ?

3、啟動nginx

chkconfig nginx on ?
service nginx start

向web服務器中放入測試文件:

<!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style>body {width: 35em;margin: 0 auto;font-family: Tahoma, Verdana, Arial, sans-serif;} </style> </head> <body> <h1>99 !</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p><p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p> </body> </html>

?

配置負載均衡服務器:

vi /etc/nginx/nginx.conf

內容如下:

?

user nginx; worker_processes 1;error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid;events {worker_connections 1024; }http {include /etc/nginx/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 /var/log/nginx/access.log main;sendfile on;#tcp_nopush on; keepalive_timeout 65;#gzip on; upstream store {server 172.16.160.103:80;server 172.16.160.99:80;}server {listen 80;server_name store;charset utf-8;location / {root html;index index.html index.htm;proxy_pass http://store; [root@localhost conf.d]# cat /etc/nginx/nginx.conf user nginx; worker_processes 1;error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid;events {worker_connections 1024; }http {include /etc/nginx/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 /var/log/nginx/access.log main;sendfile on;#tcp_nopush on; keepalive_timeout 65;#gzip on; upstream store { server 172.16.160.103:80;server 172.16.160.99:80; }server { listen 80; server_name store; charset utf-8; location / { root html; index index.html index.htm; proxy_pass http://store; proxy_set_header X-Real-IP $remote_addr; client_max_body_size 100m; } }include /etc/nginx/conf.d/*.conf; }

?

下面瀏覽器打開:172.16.160.98,如果99、103交替顯示則表明試驗成功。

拓展:


1、輪詢(默認)
每個請求按時間順序逐一分配到不同的后端服務器,如果后端服務器down掉,能自動剔除。

2、weight
指定輪詢幾率,weight和訪問比率成正比,用于后端服務器性能不均的情況。
例如:
upstream bakend {
server 192.168.159.10 weight=10;
server 192.168.159.11 weight=10;
}

3、ip_hash
每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個后端服務器,可以解決session的問題。
例如:
upstream resinserver{
ip_hash;
server 192.168.159.10:8080;
server 192.168.159.11:8080;
}


4、fair(第三方)
按后端服務器的響應時間來分配請求,響應時間短的優(yōu)先分配。
upstream resinserver{
server server1;
server server2;
fair;
}


5、url_hash(第三方)

按訪問url的hash結果來分配請求,使每個url定向到同一個后端服務器,后端服務器為緩存時比較有效。
例:在upstream中加入hash語句,server語句中不能寫入weight等其他的參數(shù),hash_method是使用的hash算法

upstream resinserver{
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}

tips:

upstream resinserver{#定義負載均衡設備的Ip及設備狀態(tài)
ip_hash;
server 127.0.0.1:8000 down;
server 127.0.0.1:8080 weight=2;
server 127.0.0.1:6801;
server 127.0.0.1:6802 backup;
}

在需要使用負載均衡的server中增加
proxy_pass http://resinserver/;


每個設備的狀態(tài)設置為:
1.down 表示單前的server暫時不參與負載
2.weight 默認為1.weight越大,負載的權重就越大。
3.max_fails :允許請求失敗的次數(shù)默認為1.當超過最大次數(shù)時,返回proxy_next_upstream 模塊定義的錯誤
4.fail_timeout:max_fails次失敗后,暫停的時間。
5.backup: 其它所有的非backup機器down或者忙的時候,請求backup機器。所以這臺機器壓力會最輕。

nginx支持同時設置多組的負載均衡,用來給不用的server來使用。

client_body_in_file_only 設置為On 可以講client post過來的數(shù)據(jù)記錄到文件中用來做debug
client_body_temp_path 設置記錄文件的目錄 可以設置最多3層目錄
location 對URL進行匹配.可以進行重定向或者進行新的代理 負載均衡

轉載于:https://www.cnblogs.com/wangmo/p/7161437.html

總結

以上是生活随笔為你收集整理的nginx负载均衡简单配置的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。