Nginx_stream

in Inbox with 0 comment

nginx 配置tcp转发

  1. yum install nginx
  2. vim /etc/nginx/nginx.conf

      stream {
          # 添加socket转发的代理
          upstream backend {
              hash $remote_addr consistent;
              # 转发的目的地址和端口
              server 127.0.0.1:19699 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 backend;
          }
      }
  3. service start nginx
  4. cat index.html

    HTTP/1.1 200 OK
    Accept-Ranges: bytes
    Cache-Control: max-age=604800
    Content-Type: text/html
    Date: Thu, 07 Dec 2017 03:28:04 GMT
    Etag: "359670651"
    Expires: Thu, 14 Dec 2017 03:28:04 GMT
    Last-Modified: Fri, 09 Aug 2013 23:54:35 GMT
    Server: ECS (oxr/83C9)
    Vary: Accept-Encoding
    X-Cache: HIT
    Content-Length: 1270
    
    <!doctype html>
    <html>
    <head>
        <title>Example Domain</title>
    
        <meta charset="utf-8" />
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <style type="text/css">
        body {
            background-color: #f0f0f2;
            margin: 0;
            padding: 0;
            font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
            
        }
        div {
            width: 600px;
            margin: 5em auto;
            padding: 50px;
            background-color: #fff;
            border-radius: 1em;
        }
        a:link, a:visited {
            color: #38488f;
            text-decoration: none;
        }
        @media (max-width: 700px) {
            body {
                background-color: #fff;
            }
            div {
                width: auto;
                margin: 0 auto;
                border-radius: 0;
                padding: 1em;
            }
        }
        </style>    
    </head>
    
    <body>
    <div>
        <h1>Example Domain</h1>
        <p>This domain is established to be used for illustrative examples in documents. You may use this
        domain in examples without prior coordination or asking for permission.</p>
        <p><a href="http://www.iana.org/domains/example">More information...</a></p>
    </div>
    </body>
    </html>
  5. ncat -l 19699 --keep-open --exec "/bin/cat /root/index.html"
  6. Ref: