天天看點

搭建Nginx四層反向代理

需求背景:

前段時間公司因為業務需求需要部署一個正向代理,我已經分享出來了https://www.cnblogs.com/Dfengshuo/p/11911406.html,現有因架構個更改,需要再加個在原先的反向代理下再加一層,ok,其實還是挺雞肋的,但是沒辦法,上司安排就要根據安排需求做。其實nginx反向代理分兩種,四層網絡層代理和七層應用層代理,想要實作這兩種模式很簡單,隻是配置檔案略微不同而已。今天分享一下四層協定的代理吧!

首先安裝nginx環境:

搭建Nginx四層反向代理
搭建Nginx四層反向代理

1 yum -y install pcre-devel zlib-devel gcc gcc+c++ make openssl openssl-devel
2 tar xf nginx-1.11.4.tar.gz
3 cd nginx-1.11.4/
4  ./configure --prefix=/usr/local/nginx  --with-http_stub_status_module --with-http_gzip_static_module --with-http_ssl_module --with-http_stub_status_module --with-pcre --with-stream && make && make install      

View Code

編譯安裝完畢後進行更改nginx配置檔案,配置四層協定反向代理

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


--------------------------反向配置區------------------------------------------
stream {

server {

listen 30010;                #監聽端口

proxy_connect_timeout 1s;

proxy_timeout 3s;

proxy_pass 10.157.8.18:30010;    #跳轉位址

}

server {

listen 30013;

proxy_connect_timeout 1s;

proxy_timeout 3s;

proxy_pass 10.157.8.18:30013;

}




server {

listen 30014;

proxy_connect_timeout 1s;

proxy_timeout 3s;

proxy_pass 10.157.8.18:30014;

}


}

--------------------------反向配置區------------------------------------------

http {
    include       mime.types;
    default_type  application/octet-stream;

    #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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
    
    }   

}
      

 配置完成後儲存重新開機nginx即可,進行測試,結果表明成功!

搭建Nginx四層反向代理

- 但行好事,

莫問前程 -