Nginx supports Socket Socket forwarding process detailed Nginx support forwarding process detailed

Recently the boss let me find a way to take a number when switching platform server to the standby server is down, naturally thought of using nginx, of course, primarily to support nginx http request, our platform is to use the socket, but also nginx support socket, reprint the following article.

Transfer: https://www.cnblogs.com/knowledgesea/p/6497783.html

Nginx supports Socket forwarding process detailed

 

Preface

A netizen asked in the group, nginx support socket forwarding it?

Honestly, I did not make socket forward, but I knew socket Like http tcp or udp communication is through, I guess it look nginx should support it, and then search the Internet about the matter, said online support, but also support relevant official documents, and reasonable, so I do not uphold the gab, know when you know the scientific attitude, my only friends back to it two words: "may," not too much pull, this time the group also has to answer for his friends wrong with me, I will continue to do my friends live, work, work, work, work, not too much concerned about this issue, not too much concerned about this issue.

An hour later, after a half-day, after two weeks, after one year, ten years later ... 

After this went on, I went to the group: Holy crap, it went so far as goods in clinging to the same problem, kept asking: "how to get, how to get?"! ! Yang Guo feel deeply grieved palm have mastered it, why this stock has not the slightest progress. Is he in bible study and understanding of Volume 5, ready Cultivation Buddha!

So I question generated great interest in this matter, I began to engage. From the end got to get only 30 minutes, but 30 minutes, get, get. The following pages I will explain the whole process.

This piece says we do not spray me, but I just want to say such a group of friends:

1, you are asking how to do before, official documents have seen it, too Baidu related article?

2, Baidu document, there are hand surgeon before? I want to swim without water okay?

3, surgeon own process, once the error, you have not been seriously wrong message?

4, moves in an unlocked when there is no thought about similar moves is kind of how? Or you to design the moves would look like? Thoughts? Thoughts? Association? Association?

If a bit too much, I would like to draw your attention, and if you're not symptomatic, can bypass, do not spray.

The first 10 minutes of the first to write a socket server can communicate with the client

Implement server can send information to the client, the client receives information displayed. Achieved with c # console application.

Server monitor local ip and port, the server code is as follows:

Copy the code
class Program
    {
        static Socket sck = null;
        static void Main(string[] args)
        {             
            sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);    
            //监听本机ip        
            IPAddress ip = IPAddress.Parse("172.18.8.196");           
            IPEndPoint endPoint = new IPEndPoint(ip, int.Parse("11911"));            
            sck.Bind(endPoint);
            sck.Listen(10);
            Console.WriteLine("开启监听!");
            Thread thread = new Thread(JtSocket);
            thread.IsBackground = true;
            thread.Start();
            while (true)
            {               
                Var = Console.ReadLine MSG () Trim ();. 
                IF (! MSG = "") 
                { 
                    data // to be transmitted, generating a word; byte [] buffer = System.Text.Encoding.ASCII.GetBytes ( msg) section array. 
                    accSck.Send (Buffer); 
                    Console.WriteLine ( "the client sends:" + MSG); 
                } 
            } 
        } 
        static accSck the Socket = null;       
        static void JtSocket () 
        { 
            the while (to true) 
            {                
                accSck = sck.Accept () ;                
                Console.WriteLine ( "link success!"); 
            } 
        } 
    }
Copy the code

Nginx client connection to the proxy server ip ip, the client follows:

Copy the code
 class Program
    {
        static Socket clientSocket = null;
        static Thread thread = null;
        static void Main(string[] args)
        {
            clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //这里联通nginx代理服务器地址ip
            IPAddress ip = IPAddress.Parse("172.18.8.252");
            IPEndPoint endpoint = new IPEndPoint(ip, Convert.ToInt32("11911"));
            clientSocket.Connect(endpoint);
            thread = new Thread(ReceMsg);
            thread.IsBackground = true;
            thread.Start();
            Console.WriteLine("123");
            Console.ReadKey();
        }
        static void ReceMsg()
        {
            while (true)
            {
                byte[] buffer = new byte[1024 * 1024 * 2];
                clientSocket.Receive(buffer);
                string ReceiveMsg = System.Text.Encoding.UTF8.GetString(buffer).Substring(0,30);
                Console.WriteLine("接收到数据:" + ReceiveMsg);
            }
        }
    }
Copy the code

Intermediate 10 min, the server set up nginx

Prepare a linux server. nginx official website: http://nginx.org/  . Search the web using a roughly  ngx_stream_core_module  this module, where you can also look at the official documentation of what other modules are doing, it would have enabled the associated configuration instructions, and sample configuration.

The first sentence is: This ngx_stream_core_modulemodule is a self-1.9.0 version. This module is not built by default, use the configuration parameter is enabled  --with-stream .

Well, we install nginx, do this, and should be able to install it.

[root@localhost /]# cd /usr/local/src
[root@localhost src]# wget http://nginx.org/download/nginx-1.11.10.tar.gz

Then decompression, decompression finished, to enable the feature documentation suggesting the need to use this parameter --with-stream based on.

[root@localhost src]# ./configure  --prefix=/usr/local/nginx --with-stream 

然后,make,make install。

It is nginx configuration profiles, there are examples in this document after completion, we can see the same level events module, in accordance with this do Jiuhaola.

Copy the code
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{
    upstream abc{
        server 172.18.8.196:11911;
    }
    server{
        listen 11911;
        proxy_pass abc;
    }
}
Copy the code

ok, save and exit and restart nginx configuration to take effect can be. Here all operations are done on the matter, let us wait and see, look at the results of it.

The last 10 minutes, the test results nginx forwards socket

Start the server ---> Display: Turn listening ---> to start the client ----> Client display: a successful connection ----> server input: abc -----> server: Click back car ----> client display: abc. as follows

Server: Client:

So far all have been the end of the whole process, function realization.

I just want to start a discussion here, do not spray! !

to sum up

Next is everyone's favorite summarize the contents of the matter, the contents of two, as follows:

1, hoping to focus on my other articles.

2, blog, there are no very clear to say it, or you have a better way, then welcome to join the two exchange group at the top left, we learn to explore together.

Preface

A netizen asked in the group, nginx support socket forwarding it?

Honestly, I did not make socket forward, but I knew socket Like http tcp or udp communication is through, I guess it look nginx should support it, and then search the Internet about the matter, said online support, but also support relevant official documents, and reasonable, so I do not uphold the gab, know when you know the scientific attitude, my only friends back to it two words: "may," not too much pull, this time the group also has to answer for his friends wrong with me, I will continue to do my friends live, work, work, work, work, not too much concerned about this issue, not too much concerned about this issue.

An hour later, after a half-day, after two weeks, after one year, ten years later ... 

After this went on, I went to the group: Holy crap, it went so far as goods in clinging to the same problem, kept asking: "how to get, how to get?"! ! Yang Guo feel deeply grieved palm have mastered it, why this stock has not the slightest progress. Is he in bible study and understanding of Volume 5, ready Cultivation Buddha!

So I question generated great interest in this matter, I began to engage. From the end got to get only 30 minutes, but 30 minutes, get, get. The following pages I will explain the whole process.

This piece says we do not spray me, but I just want to say such a group of friends:

1, you are asking how to do before, official documents have seen it, too Baidu related article?

2, Baidu document, there are hand surgeon before? I want to swim without water okay?

3, surgeon own process, once the error, you have not been seriously wrong message?

4, moves in an unlocked when there is no thought about similar moves is kind of how? Or you to design the moves would look like? Thoughts? Thoughts? Association? Association?

If a bit too much, I would like to draw your attention, and if you're not symptomatic, can bypass, do not spray.

The first 10 minutes of the first to write a socket server can communicate with the client

Implement server can send information to the client, the client receives information displayed. Achieved with c # console application.

Server monitor local ip and port, the server code is as follows:

Copy the code
class Program
    {
        static Socket sck = null;
        static void Main(string[] args)
        {             
            sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);    
            //监听本机ip        
            IPAddress ip = IPAddress.Parse("172.18.8.196");           
            IPEndPoint endPoint = new IPEndPoint(ip, int.Parse("11911"));            
            sck.Bind(endPoint);
            sck.Listen(10);
            Console.WriteLine("开启监听!");
            Thread thread = new Thread(JtSocket);
            thread.IsBackground = true;
            thread.Start();
            while (true)
            {               
                Console.WriteLine ( "link success!"); 
            }
                Console.ReadLine MSG = var () Trim ();. 
                IF (MSG = ""!) 
                { 
                    byte [] = Buffer System.Text.Encoding.ASCII.GetBytes (MSG); // data to be transmitted, generates a byte array. 
                    accSck.Send (Buffer); 
                    Console.WriteLine ( "the client sends:" + MSG); 
                } 
            } 
        } 
        static accSck the Socket = null;       
        static void JtSocket () 
        { 
            the while (to true) 
            {                
                accSck = sck.Accept () ;                
        } 
    }
Copy the code

Nginx client connection to the proxy server ip ip, the client follows:

Copy the code
 class Program
    {
        static Socket clientSocket = null;
        static Thread thread = null;
        static void Main(string[] args)
        {
            clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //这里联通nginx代理服务器地址ip
            IPAddress ip = IPAddress.Parse("172.18.8.252");
            IPEndPoint endpoint = new IPEndPoint(ip, Convert.ToInt32("11911"));
            clientSocket.Connect(endpoint);
            thread = new Thread(ReceMsg);
            thread.IsBackground = true;
            thread.Start();
            Console.WriteLine("123");
            Console.ReadKey();
        }
        static void ReceMsg()
        {
            while (true)
            {
                byte[] buffer = new byte[1024 * 1024 * 2];
                clientSocket.Receive(buffer);
                string ReceiveMsg = System.Text.Encoding.UTF8.GetString(buffer).Substring(0,30);
                Console.WriteLine("接收到数据:" + ReceiveMsg);
            }
        }
    }
Copy the code

Intermediate 10 min, the server set up nginx

Prepare a linux server. nginx official website: http://nginx.org/  . Search the web using a roughly  ngx_stream_core_module  this module, where you can also look at the official documentation of what other modules are doing, it would have enabled the associated configuration instructions, and sample configuration.

The first sentence is: This ngx_stream_core_modulemodule is a self-1.9.0 version. This module is not built by default, use the configuration parameter is enabled  --with-stream .

Well, we install nginx, do this, and should be able to install it.

[root@localhost /]# cd /usr/local/src
[root@localhost src]# wget http://nginx.org/download/nginx-1.11.10.tar.gz

Then decompression, decompression finished, to enable the feature documentation suggesting the need to use this parameter --with-stream based on.

[root@localhost src]# ./configure  --prefix=/usr/local/nginx --with-stream 

然后,make,make install。

It is nginx configuration profiles, there are examples in this document after completion, we can see the same level events module, in accordance with this do Jiuhaola.

Copy the code
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{
    upstream abc{
        server 172.18.8.196:11911;
    }
    server{
        listen 11911;
        proxy_pass abc;
    }
}
Copy the code

ok, save and exit and restart nginx configuration to take effect can be. Here all operations are done on the matter, let us wait and see, look at the results of it.

The last 10 minutes, the test results nginx forwards socket

Start the server ---> Display: Turn listening ---> to start the client ----> Client display: a successful connection ----> server input: abc -----> server: Click back car ----> client display: abc. as follows

Server: Client:

So far all have been the end of the whole process, function realization.

I just want to start a discussion here, do not spray! !

to sum up

Next is everyone's favorite summarize the contents of the matter, the contents of two, as follows:

1, hoping to focus on my other articles.

2, blog, there are no very clear to say it, or you have a better way, then welcome to join the two exchange group at the top left, we learn to explore together.

Guess you like

Origin www.cnblogs.com/linwenbin/p/11589916.html