nginx:socker forwarding

1. Install nginx, the stream module is not installed by default, you need to manually add parameters: –with-stream, official download address: download, select nginx1.9 or above according to your system version.
2. nginx.conf configuration, reference description: ngx_stream_core_module (http://nginx.org/en/docs/stream/ngx_stream_core_module.html)
===================== =================
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
.................
}

# Configuration folder for tcp layer forwarding

include /etc/nginx/tcp.d/*.conf;
===== ================================
Please note that the stream configuration cannot be placed in http, that is, it cannot be placed in /etc /nginx/conf.d/, because the stream is forwarded through the tcp layer, not http forwarding.
If the configuration is in http, starting nginx will report the following error:
nginx: [emerg] "server" directive is not allowed here

3. Create a new bss_num_30001.conf file under tcp.d
========================== =============
stream {
    # Add proxy upstream for socket forwarding
    bss_num_socket {
        hash $remote_addr consistent;
        # Forwarding destination address and port
        server 130.51.11.33:19001 weight=5 max_fails=3 fail_timeout =30s;
    }

    # Provide forwarding service, that is, access localhost:30001, it will jump to the forwarding address specified by proxy bss_num_socket
    server {
       listen 30001;
       proxy_connect_timeout 1s;
       proxy_timeout 3s;
       proxy_pass bss_num_socket;
    }
}
======== ===============================

4. Restart nginx, access localhost:30001, it will jump to the forwarding address 130.51.11.33:19001 specified by bss_num_socket

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326179677&siteId=291194637