Home  >  Article  >  Operation and Maintenance  >  Does nginx support sockets?

Does nginx support sockets?

(*-*)浩
(*-*)浩Original
2019-06-10 16:44:235249browse

There is an interface that communicates through socket. There are IP restrictions on access to the peer server, so we have to use the springboard machine because it has the permission to access the peer server. nginx1.9 begins to support TCP layer forwarding, which is implemented through stream, and socket is also based on TCP communication.

Does nginx support sockets?

Implementation process:

1. Install nginx. The stream module is not installed by default and needs to be manually installed. Add parameters: –with-stream, select nginx1.9 or above according to your system version.

2.nginx.conf configuration, refer to the description: ngx_stream_core_module

nginx.conf

user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
.................
}

# tcp层转发的配置文件夹

include /etc/nginx/tcp.d/*.conf;

Please note that the stream configuration cannot be placed in Within http, that is, it cannot be placed in /etc/nginx/conf.d/, because stream is forwarded through the tcp layer, not http.

If configured in http, the following error will be reported when starting nginx:

nginx: [emerg] "server" directive is not allowed here

3. Create a new bss_num_30001.conf file under tcp.d with the following content:

stream {
    # 添加socket转发的代理
    upstream bss_num_socket {
        hash $remote_addr consistent;
        # 转发的目的地址和端口
        server 130.51.11.33:19001 weight=5 max_fails=3 fail_timeout=30s;
    }

    # 提供转发的服务,即访问localhost:30001,会跳转至代理bss_num_socket指定的转发地址
    server {
       listen 30001;
       proxy_connect_timeout 1s;
       proxy_timeout 3s;
       proxy_pass bss_num_socket;
    }
}

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

For more Nginx related technical articles, please visit the Nginx Usage Tutorial column to learn!

The above is the detailed content of Does nginx support sockets?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn