Nginx七層負載均衡的實現示例
七層負載均衡介紹
Nginx七層負載均衡是在應用層(HTTP/HTTPS)上進行的,可以根據HTTP請求的具體內容,如URL、Cookie、Header等,來決定將請求轉發(fā)到哪個后端服務器。這種方式不僅能夠均衡服務器的計算負載,還能實現更復雜的路由策略,例如:
會話粘性(Sticky Sessions):確保用戶的會話請求始終被定向到同一個后端服務器。
基于內容的路由:根據請求的內容(如URL、頭部信息)將請求分發(fā)到不同的服務器。
四層與七層負載均衡的區(qū)別
1.1 四層負載均衡(Layer 4 Load Balancing)
在傳輸層(Transport Layer)上進行。
關注網絡層面的信息,如源和目標IP地址、端口號等。
根據網絡信息決定將數據包轉發(fā)到哪個服務器。
不深入檢查數據包的內容。
主要適用于基于TCP/UDP的流量,如HTTP和HTTPS。
1.2 七層負載均衡(Layer 7 Load Balancing)
在應用層(Application Layer)上進行。
深入檢查網絡流量的內容,如HTTP請求頭、URL、Cookie等。
根據流量內容做復雜的路由決策。
可以處理多種應用層協議,不僅限于HTTP。
2 七層負載均衡配置
| 服務器類型 | IP地址 | 發(fā)行版 |
|---|---|---|
| 代理服務器(proxy) | 192.168.110.31/24 | Rocky Linux 8 |
| 靜態(tài)地址服務器(static) | 192.168.110.32/24 | Rocky Linux 8 |
| 默認地址服務器(default) | 192.168.110.33/24 | Rocky Linux 8 |
| 動態(tài)地址服務器(upload) | 192.168.110.34/24 | Rocky Linux 8 |
2.1 后端節(jié)點配置
2.1.1 靜態(tài)地址服務器(static)
[root@static ~]# mkdir /nginx/static
[root@static ~]# echo "This is static page IP=`hostname -I`" >> /nginx/static/index.html
[root@static ~]# vim /etc/nginx/conf.d/VirtualHost.conf
server {
listen 192.168.110.32:80;
server_name www.nginx,com;
root /nginx;
?
location / {
index index.html;
}
}
?
[root@static ~]# nginx -s reload
[root@static ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@static ~]# curl 192.168.110.32/static/
This is static page IP=192.168.110.322.1.2 默認地址服務器(default)
[root@default ~]# mkdir /nginx/default
[root@default ~]# echo "This is default page IP=`hostname -I`" >> /nginx/default/index.html
[root@default ~]# vim /etc/nginx/conf.d/VirtualHost.conf
server {
listen 192.168.110.33:80;
server_name www.nginx,com;
root /nginx/default;
?
location / {
index index.html;
}
}
?
[root@default ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@default ~]# nginx -s reload
[root@default ~]# curl 192.168.110.33/default/
This is default page IP=192.168.110.332.1.3 動態(tài)地址服務器(upload)
[root@upload ~]# mkdir /nginx/upload
[root@upload ~]# echo "This is upload page IP=`hostname -I`" >> /nginx/upload/index.html
[root@upload ~]# vim /etc/nginx/conf.d/VirtualHost.conf
server {
listen 192.168.110.34:80;
server_name www.nginx,com;
root /nginx;
?
location / {
index index.html;
}
}
?
[root@upload ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@upload ~]# nginx -s reload
[root@upload ~]# curl 192.168.110.34/upload/
This is upload page IP=192.168.110.342.2 代理服務器配置
[root@proxy ~]# vim /etc/nginx/conf.d/proxy.conf
upstream static_pools {
server 192.168.110.32;
}
?
upstream default_pools {
server 192.168.110.33;
}
?
upstream upload_pools {
server 192.168.110.34;
}
?
server {
listen 80;
server_name www.nginx.com;
?
location /static {
proxy_pass http://static_pools;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
?
location /default {
proxy_pass http://default_pools;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
?
location /upload {
proxy_pass http://upload_pools;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
?
[root@proxy ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@proxy ~]# nginx -s reload注意:這里location的寫法,這里的主要難點就在這。例如:如果站點目錄寫 root /nginx/ststic; 那最后訪問 http://www.nginx.com/static/ 時轉發(fā)路徑就為http://www.nginx.com/static/static。所以寫/nginx就行了
2.3 客戶端測試訪問
[root@client ~]# echo '192.168.110.31 www.nginx.com' >> /etc/hosts [root@client ~]# curl http://www.nginx.com This is default page IP=192.168.110.33 [root@client ~]# curl http://www.nginx.com/static/ This is static page IP=192.168.110.32 [root@client ~]# curl http://www.nginx.com/upload/ This is upload page IP=192.168.110.34
2.4 查看各節(jié)點訪問日志
[root@static ~]# tail -1 /var/log/nginx/access.log 192.168.110.31 - - [21/Apr/2024:17:07:34 +0800] "GET /static/ HTTP/1.0" 200 39 "-" "curl/7.61.1" "192.168.110.35" ? [root@default ~]# tail -1 /var/log/nginx/access.log 192.168.110.31 - - [21/Apr/2024:17:07:32 +0800] "GET / HTTP/1.0" 200 40 "-" "curl/7.61.1" "192.168.110.35" ? [root@upload ~]# tail -1 /var/log/nginx/access.log 192.168.110.31 - - [21/Apr/2024:17:07:36 +0800] "GET /upload/ HTTP/1.0" 200 39 "-" "curl/7.61.1" "192.168.110.35"
到此這篇關于Nginx七層負載均衡的實現示例的文章就介紹到這了,更多相關Nginx七層負載均衡內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
nginx出現500 Internal Server Error錯誤的解決方法
這篇文章主要介紹了nginx出現500 Internal Server Error錯誤的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或工作有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-09-09
在Debian11上安裝Openresty服務(Nginx+Lua)的詳細教程
OpenResty 是一個基于 Nginx 與 Lua 的高性能 Web 平臺,其內部集成了大量精良的 Lua 庫、第三方模塊以及大多數的依賴項,這篇文章主要介紹了在Debian11上安裝Openresty服務(Nginx+Lua)?,需要的朋友可以參考下2022-10-10

