1. 程式人生 > 其它 >Nginx配置TCP請求轉發

Nginx配置TCP請求轉發

1.TCP請求轉發基於stream在1.9版本前,需要單獨編譯安裝該組建:

# 依賴服務
[root@baolin conf]#yum -y install pcre-devel openssl openssl-devel library

# 使用者
[root@baolin conf]#useradd nginx -u 1000

# 編譯安裝 stream 組建
./configure --user=nginx --group=nginx  --prefix=/usr/local/nginx/ --with-http_stub_status_module --with-http_ssl_module --with-stream  --with-stream_ssl_module

2.配置nginx.conf 實現TCP得請求轉發

[root@baolin conf]# cat /usr/local/nginx/nginx.conf
worker_processes  1;

events {
    worker_connections  1024;
}

# 此為TCP轉發請求 stream 
stream {
    # 後端指向 server 的 8085 埠 stream_backend 組
    upstream stream_backend {
         server 10.50.2.11:8085;
         server 10.50.2.19:8085
; } # 後端指向 server 的 8090 埠 cns組 upstream cns { server 10.50.2.11:8090; server 10.50.2.19:8090; } server { listen 443 ssl; proxy_pass stream_backend; # 指定key 和 crt 地址 ssl_certificate /etc/ssl/certs/my.crt; ssl_certificate_key
/etc/ssl/certs/my.key; ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers HIGH:!aNULL:!MD5; ssl_session_cache shared:SSL:20m; ssl_session_timeout 4h; ssl_handshake_timeout 30s; } server { # 本機監聽埠 8080 listen 8080; # 請求拋給 stream_backend 組 proxy_pass stream_backend; } server { # 本機監聽埠 8090 listen 8090; # 請求拋給 cns 組 proxy_pass cns; } } # 此為HTTP 轉發請求 http http { 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 /var/log/nginx/access.log main; sendfile on; tcp_nopush on; gzip_comp_level 9; gzip_types text/css text/xml application/javascript; gzip_vary on; include /etc/nginx/mime.types; default_type application/octet-stream; # 後端指向 server 的 8585 埠 cns_node 組 upstream cns_node { server 10.50.2.51:8585 weight=3; server 10.50.2.59:8585 weight=3; } server { listen 8585; server_name umout.com; access_log /etc/nginx/logs/server02_access.log main; location /{ index index.html index.htm index.jsp; proxy_pass http://cns_node1; include /etc/nginx/proxy.conf; } } }