• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            concentrate on c/c++ related technology

            plan,refactor,daily-build, self-discipline,

              C++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              37 Posts :: 1 Stories :: 12 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(9)

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            現在nginx的版本已經到了1.9.12,不過在1.9.0的時候,就開始了對TCP代理服務器的支持。
            之前也有一些早期的做法,比如就有寫了nginx_tcp_module模塊的https://github.com/yaoweibin/nginx_tcp_proxy_module,
            以及nginx_stream_lua模塊的
            https://github.com/openresty/stream-lua-nginx-module
            本來就想著用這兩個的,后來發現已經有支持了,就先來試試看看這個了。
            默認的話,nginx是沒有開啟對tcp的支持的.如果要支持的話,需要用--with-stream去指定。
            我這邊只要需要三個IP,一個是nginx進程所在的機器,我這邊用192.168.1.4表示,
            另外兩臺就是TCP上游業務服務器,我們分別用192.168.1.5和192.168.1.21來表示,
            基于這樣的考慮是因為nginx充當tcp代理服務器后,自身也會占用一個端口。
            并且從目前來說,貌似一個nginx進程也就只能listen一個端口。
            我用的版本是nginx 1.9.9.
            我在/usr/local/nginx/conf下面新建了nginx_stream_tcp.conf作為本次的測試。
            worker_processes auto;
              2 error_log /usr/local/nginx/logs/error.log info;
              3 events {
              4     worker_connections  1024;
              5 }
              6
              7 stream {
              8     upstream loadbalance {
              9 # Specifies a load balancing method for a server group where client-server mapping is based on the hashed key value
             10         hash $remote_addr consistent;
             11 # sets the weight of the server, by default, 1.
             12         server 192.168.1.21:8001 weight=5;
             13 # by default, the parameter is set to 10 seconds.
             14         server 192.168.1.5:9001  max_fails=3 fail_timeout=30s;
             15
             16 
             17     }
             18
             19 server {
             20 # listen 8001.
             21         listen 8001;
             22 # Defines a timeout for establishing a connection with a proxied server.
             23         proxy_connect_timeout 1s;
             24 # Sets the timeout between two successive read or write operations on client or proxied server connections
             25         proxy_timeout 3s;
             26 # Sets the address of a proxied server
             27         proxy_pass loadbalance;
             28     }
             29
             30 }
            這個大部分是官網支持的,所以其實我也沒有修改太多,就設置了幾個IP和端口。
            在編譯好nginx后,可以用/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx_stream_tcp.conf
            就可以起來了,可以通過netstat -tlnp查看是否起來,大致顯示這樣的:
            tcp        0      0 0.0.0.0:8001            0.0.0.0:*               LISTEN      7744/nginx_stream_t
            當然也可以通過telnet來查看。
            接下來,寫個簡單的cs程序來驗證下吧.

            server端:
            if __name__ == '__main__':
             import socket
             import commands
             sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
             sock.bind(('192.168.1.21', 8001))
             sock.listen(5)
             while True:
              connection,address = sock.accept()
              print "connected by",address
              while 1:

                buf = connection.recv(1024)
                if buf != "" :
                 print "connection buffer", buf
                 connection.sendall(buf)
             connection.close()

            client端:
            if __name__ == '__main__': 
                import socket
                HOST='192.168.1.4'
                PORT=8001
                sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
                sock.connect((HOST, PORT))
                while 1:
                    cmd = raw_input("please input cmd:")
                    sock.sendall(cmd)
                    data = sock.recv(1024)
                    print data
                sock.close()

            在client向192.168.1.4發起一個TCP連接時,接下來192.168.1.4會連接到192.168.1.21,
            192.168.1.21的server端會顯示, connected by ('192.168.1.4',58559)
            后面的端口應該是隨機的臨時端口。
            其實它的確起到了轉發作用,這個nginx進程相當于一個通用網關,如果有兩個client向nginx進程發起連接,一旦nginx那邊調度到同一個上游TCP服務器,那么只需要從NGINX進程建立一個TCP連接到上游TCP服務器即可。
            之前我自己設計網關的時候,不是依賴于用戶的,而是通過靜態配置來建立連接的,比如上游邏輯服務器跟網關有連接,那都是在啟動服務器的時候,就建立起來了。
            這樣有個好處就是可以省事,不過也可以采用這種方式,有玩家連接進某個上游TCP服務器才讓網關跟這個TCP服務器建立連接,否則無需處理。因為不少時候還是會有不少服務器處于空閑的.

            posted on 2016-03-11 08:44 jolley 閱讀(844) 評論(0)  編輯 收藏 引用 所屬分類: nginx
            国产aⅴ激情无码久久| 久久婷婷五月综合色奶水99啪| 久久精品国产亚洲av影院| 久久精品国产亚洲av水果派| 国产精品免费福利久久| 色综合久久天天综合| 国产成人99久久亚洲综合精品| 久久久久亚洲AV综合波多野结衣| 亚洲人成无码www久久久 | 久久WWW免费人成—看片| 国产精品青草久久久久福利99 | 少妇久久久久久久久久| 亚洲嫩草影院久久精品| 色偷偷久久一区二区三区| 久久久久国产精品麻豆AR影院| 久久亚洲国产成人精品性色| 国产精品无码久久四虎| 亚洲午夜无码久久久久| 久久久久国产一级毛片高清板| 久久久久久久久久久久中文字幕| 久久久WWW成人免费精品| 久久国产免费观看精品3| 人妻无码精品久久亚瑟影视 | 99久久精品无码一区二区毛片| 亚洲精品美女久久777777| 国内精品久久久久久久久| 99久久中文字幕| 精品无码久久久久久尤物| 欧美伊人久久大香线蕉综合| 无码任你躁久久久久久久| 久久免费大片| 四虎亚洲国产成人久久精品| 久久精品国产色蜜蜜麻豆| 国产高清国内精品福利99久久 | 性做久久久久久久久老女人| 国产精品久久久天天影视香蕉| 99国产精品久久| 国产亚洲婷婷香蕉久久精品| 国产精品一区二区久久国产| 国产精品久久久久久久久鸭| 99久久99久久|