搭建Nginx四層反向代理


需求背景:

前段時間公司因為業務需求需要部署一個正向代理,我已經分享出來了https://www.cnblogs.com/Dfengshuo/p/11911406.html,現有因架構個更改,需要再加個在原先的反向代理下再加一層,ok,其實還是挺雞肋的,但是沒辦法,領導安排就要根據安排需求做。其實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即可,進行測試,結果表明成功!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM