一,實(shí)驗(yàn)介紹
利用三臺CentOS 7虛擬機(jī)搭建簡單的Nginx反向代理負(fù)載集群,三臺虛擬機(jī)地址及功能介紹
192.168.2.76 nginx負(fù)載均衡器
192.168.2.82 web01服務(wù)器
192.168.2.78 web02服務(wù)器
二,安裝nginx軟件(以下操作三臺虛擬機(jī)都要進(jìn)行)
有些Centos 7.6里面沒有安裝wget命令,所以要自己安裝:
yum -y install wget
安裝nginx軟件:(三個服務(wù)器都要安裝)
$ wget http://dl.Fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
$ rpm -ivh epel-release-latest-7.noarch.rpm
$ yum install nginx (直接yum安裝)
安裝就這么簡單方便,安裝完成后,就可以使用systemctl來控制nginx的啟動了
$ systemctl enable nginx (加入開機(jī)啟動)
$ systemctl start nginx (開啟nginx)
$ systemctl status nginx (查看狀態(tài))
三臺服務(wù)器分別安裝好nginx后測試能否正常運(yùn)行,提供web服務(wù)。如果報錯可能是防火墻的原因,請看最后幾步關(guān)于防火墻的。
修改代理服務(wù)器的nginx的配置文件,實(shí)現(xiàn)負(fù)載均衡。顧名思義就是將多個請求分發(fā)到不同的服務(wù)上,實(shí)現(xiàn)均衡的負(fù)載,減小單個服務(wù)的壓力。
$ vi /etc/nginx/nginx.conf (修改配置文件,全局配置文件)
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto; (默認(rèn)為自動,可以自己設(shè)置,一般不大于cpu核數(shù))
error_log /var/log/nginx/error.log; (錯誤日志路徑)
pid /run/nginx.pid; (pid文件路徑)
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
accept_mutex on; (設(shè)置網(wǎng)路連接序列化,防止驚群現(xiàn)象發(fā)生,默認(rèn)為on)
multi_accept on; (設(shè)置一個進(jìn)程是否同時接受多個網(wǎng)絡(luò)連接,默認(rèn)為off)
worker_connections 1024; (一個進(jìn)程的最大連接數(shù))
}
http {
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; (這里注釋掉)
tcp_nodelay on;
keepalive_timeout 65; (連接超時時間)
types_hash_max_size 2048;
gzip on; (開啟壓縮)
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
# 這里設(shè)置負(fù)載均衡,負(fù)載均衡有多中策略,nginx自帶的有輪詢,權(quán)重,ip-hash,響應(yīng)時間等粗略。
# 默認(rèn)為平分http負(fù)載,為輪詢的方式。
# 權(quán)重則是按照權(quán)重來分發(fā)請求,權(quán)重高的負(fù)載大
# ip-hash,根據(jù)ip來分配,保持同一個ip分在同一臺服務(wù)器上。
# 響應(yīng)時間,根據(jù)服務(wù)器都nginx 的響應(yīng)時間,優(yōu)先分發(fā)給響應(yīng)速度快的服務(wù)器。
集中策略可以適當(dāng)組合
upstream tomcat { (tomcat為自定義的負(fù)載均衡規(guī)則名)
ip_hash; (ip_hash則為ip-hash方法)
server 192.168.2.78:80 weight=3 fail_timeout=20s;
server 192.168.2.82:80 weight=4 fail_timeout=20s;
## 可以定義多組規(guī)則
}
server {
listen 80 default_server; (默認(rèn)監(jiān)聽80端口)
listen localhost; (監(jiān)聽的服務(wù)器)
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / { ( / 表示所有請求,可以自定義來針對不同的域名設(shè)定不同負(fù)載規(guī)則 和服務(wù))
proxy_pass http://tomcat; (反向代理,填上你自己的負(fù)載均衡規(guī)則名)
proxy_redirect off; (下面一些設(shè)置可以直接復(fù)制過去,不要的話,有可能導(dǎo)致一些 沒法認(rèn)證等的問題)
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 90; (下面這幾個都只是一些超時設(shè)置,可不要)
proxy_send_timeout 90;
proxy_read_timeout 90;
}
# location ~.(gif|jpg|png)$ { (比如,以正則表達(dá)式寫)
# root /home/root/images;
# }
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate “/etc/pki/nginx/server.crt”;
# ssl_certificate_key “/etc/pki/nginx/private/server.key”;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
更新配置后,可以重載配置生效,不需要重啟服務(wù)
nginx -s reload
如果不能訪問,可能是由于防火墻打開了,端口沒有開啟:
啟動: systemctl start firewalld
關(guān)閉: systemctl stop firewalld
查看狀態(tài): systemctl status firewalld
開機(jī)禁用 : systemctl disable firewalld
開機(jī)啟用 : systemctl enable firewalld
開啟一個端口:
添加
firewall-cmd –zone=public –add-port=80/tcp –permanent (–permanent永久生效,沒有此參數(shù)重啟后失效)
重新載入
firewall-cmd –reload
查看
firewall-cmd –zone= public –query-port=80/tcp
刪除
firewall-cmd –zone= public –remove-port=80/tcp –permanent