架構圖
配置過程
配置web服務器
# 1、配置web01,更改配置文件
[root@web01 /etc/nginx/conf.d]# vi test1.conf
server {
listen 8007;
server_name test.gong.com;
root /website/test;
index index.html;
}
# 2、創建站點目錄。
[root@web01 /etc/nginx/conf.d]# mkdir -p /website/test
# 2、添加主頁
[root@web01 /etc/nginx/conf.d]# echo 'This is <h1 style="color:red;">web01</h1> page!!' >/website/test/index.html
# 4、重啟nginx
[root@web01 /etc/nginx/conf.d]# nginx -s reload
# 用同樣的方法配置web02
## web02監聽的8008端口
[root@web01 /etc/nginx/conf.d]# vi test1.conf
server {
listen 8008;
server_name test.gong.com;
root /website/test;
index index.html;
}
配置七層負載均衡
# 1、配置負載均衡
[root@lb01 /etc/nginx/conf.d]# vi upstream.conf
upstream test_gong {
172.16.1.7:8007;
172.16.1.8:8008;
}
server {
listen 8005;
server_name test.gong.com;
location / {
proxy_pass http://test_gong;
include proxy_params;
}
}
[root@lb01 /etc/nginx/conf.d]# nginx -s reload
# 第二台也使用相同的配置
##
[root@db01 /etc/nginx/conf.d]# vi upstream.conf
upstream test_gong {
server 172.16.1.7:8007;
server 172.16.1.8:8008;
}
server {
listen 8051;
server_name test.gong.com;
location / {
proxy_pass http://test_gong;
include proxy_params;
}
}
四層負載均衡概念
四層負載均衡
七層負載均衡:只識別域名,是http層。
四層負載均衡:不識別域名,是tcp層,類似於端口轉發。
在nginx-1.9.0之前的版本沒有四層負載均衡。
ngx_stream_core_module
用於四層負載均衡
The ngx_stream_core_module
module is available since version 1.9.0. This module is not built by default, it should be enabled with the --with-stream
configuration parameter.
“ngx_stream_core_”模塊自1.9.0版起提供。默認情況下,此模塊不是生成的,應使用“--with-stream”配置參數啟用它。
官方實例
因為是四層的協議,所以不能寫在http模塊當中。
stream {
upstream backend {
hash $remote_addr consistent;
server backend1.example.com:12345 weight=5;
server 127.0.0.1:12345 max_fails=3 fail_timeout=30s;
server unix:/tmp/backend3;
}
upstream dns {
server 192.168.0.1:53535;
server dns.example.com:53;
}
server {
listen 12345;
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass backend;
}
server {
listen 127.0.0.1:53 udp reuseport;
proxy_timeout 20s;
proxy_pass dns;
}
server {
listen [::1]:12345;
proxy_pass unix:/tmp/stream.socket;
}
}
四層負載作用
端口轉發
做7層負載的高可用
7層負載的端口限制
四層轉發的效率比七層的高,因為在tcp的第四層。
大並發的場景會在,七層負載前面添加四層負載。
動靜分離
不需要運維來做,開發做的。
-
動態請求:該請求會調用數據庫中的數據。
-
靜態請求:用戶請求不會調用數據庫。
-
動態頁面:后端開發寫的需要調用數據庫的頁面(python、java、C、php)
-
靜態頁面:前端開發寫的不需要調用數據庫。
配置四層負載均衡
四層負載均衡比七層的轉發效率要高,在yum安裝nginx的時候不帶支持四層負載均衡的模塊所以要使用源碼編譯安裝。
[root@nfs01 ~]# tar -xf nginx-1.16.1.tar.gz
[root@nfs01 /application]# yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
[root@nfs01 ~/nginx-1.16.1]# ./configure --prefix=/application/nginx-1.16.1 --user=www --group=www --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --with-stream
[root@nfs01 ~/nginx-1.16.1]# make && make install
[root@nfs01 ~]# vi /etc/profile.d/nginx.sh
export PATH="/application/nginx/sbin:$PATH"
[root@nfs01 ~]# ln -s /application/nginx-1.16.1/ /application/nginx
[root@nfs01 ~]# source /etc/profile
[root@nfs01 ~]# vi /application/nginx/conf/nginx.conf
...
events {
worker_connections 1024;
}
# 加入它在指定的目錄下配置單獨的配配置文件
include conf.c/*.conf;
http {
...
[root@nfs01 /application/nginx/conf/conf.c]# vi four_upstream.conf
stream {
upstream lb {
server 172.16.1.5:8005;
server 172.16.1.51:8051;
}
log_format main '$remote_addr $remote_port - [$time_local] $status $protocol '
'"$upstream_addr" "$upstream_bytes_sent" "$upstream_connect_time"';
server {
listen 80;
proxy_connect_timeout 3s;
proxy_timeout 3s;
proxy_pass lb;
access_log logs/access.log main;
}
}