NGINX 做TCP轉發(端口轉發)並記錄日志


nginx安裝 略 (注意:必須加上--with-stream這個模塊)

修改nginx.conf:

user  www www;
worker_processes  4; 
pid        logs/nginx.pid;
 
events {
    #use epoll;                            #Linux最常用支持大並發的事件觸發機制
    worker_connections  65535;
}
 
stream {
    log_format proxy '$remote_addr [$time_local] '
                 '$protocol $status $bytes_sent $bytes_received '
                 '$session_time "$upstream_addr" '
                 '"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';
 
    access_log logs/access_8000.log proxy ;
    open_log_file_cache off;
    
    upstream zifangsky {
        #hash $remote_addr consistent;
        server 10.10.100.31:8000 weight=5 max_fails=1 fail_timeout=20s;
    }
 
    server {
        listen 8000;
        proxy_connect_timeout 10s;
        proxy_timeout 10s;
        proxy_pass zifangsky;
    tcp_nodelay on;
    }
}

在上面的配置文件中配置了在訪問此服務器的8080端口時,會將流量相應轉發到10.10.100.31這個服務器的8000端口上。另外,測試發現只有當一個會話結束之后nginx才會將相關日志記錄到指定的日志文件中


免責聲明!

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



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