1. 程式人生 > 實用技巧 >nginx的四層轉發功能

nginx的四層轉發功能



架構圖


配置過程

配置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;
        }
}

配置四層負載均衡

四層負載均衡比七層的轉發效率要高,在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;
    }
}



FBI WARNING

QQ:1402122292 認準原創sheldon 別人叫我曉東