Nginx web負載均衡配置
下載nginx
- http://nginx.org/en/download.html
wget http://nginx.org/download/nginx-1.20.2.tar.gz
安裝nginx
- 參考: https://www.cnblogs.com/-wei/p/15219624.html
./configure --prefix=/opt/nginx
或
./configure --prefix=/opt/nginx --with-http_ssl_module
make
make install
報錯
- ./configure: error: the HTTP rewrite module requires the PCRE library
yum -y install pcre-devel
- ./configure: error: the HTTP gzip module requires the zlib library.
yum -y install zlib-devel
- ./configure: error: SSL modules require the OpenSSL library.
yum -y install openssl openssl-devel
nginx限制請求數據包大小
client_max_body_size 1000m;
負載均衡配置
- 參考: https://www.cnblogs.com/telwanggs/p/14977290.html
upstream test_group {
# If there is no specific strategy, round-robin
# would be the default strategy.
# least_conn;
# ip_hash;
server 172.16.217.109:8501 weight=1 max_fails=2 fail_timeout=30s;
server 172.16.217.109:8502 weight=1 max_fails=2 fail_timeout=30s;
}
location / {
#root html;
#index index.html index.htm;
client_max_body_size 3m;
proxy_pass http://test_group;
}
nginx負載均衡策略
- 輪詢:將客戶端發起的請求,平均分配給每一臺服務器
- 權重:將客戶的的請求,根據服務器的權重值不同,分配不同的數量
- ip_hash:基于發起請求的客戶端的ip地址不同,他始終會將請求發送到指定的服務器上,客戶端ip地址不變,就會一直發送到一個服務器上。
輪詢
upstream my_server{
server IP:8080;
server IP:8081;
}
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
proxy_pass http://my_server/;
}
}
權重
upstream my_server{
server IP:8080 weight=10;
server IP:8081 weight=2;
}
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
proxy_pass http://my_server/;
}
}
ip_hash
upstream my_server{
ip_hash;
server IP:8080 weight=10;
server IP:8081 weight=2;
}
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
proxy_pass http://my_server/;
}
}