ARM Nginx TCP代理

服务器

浏览数:137

2019-10-13

AD:资源代下载服务

下载nginx源码

wget http://nginx.org/download/nginx-1.9.9.tar.gz

编译

su
unar nginx-1.9.9.tar.gz
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install zlib1g-dev
./configure --with-stream
make
make install

运行

vim /usr/local/nginx/conf/nginx.conf

修改之后如下

#user  nobody;
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 test {
        hash $remote_addr consistent;
        server 127.0.0.1:8000;
    }
    server {
        listen 7000;
         proxy_connect_timeout 1s;
        proxy_timeout 10s;
        proxy_pass test;
    }
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    #access_log  logs/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;
    server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            root   html;
            index  index.html index.htm;
        }
        #error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;
    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}

run

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

stop

/usr/local/nginx/sbin/nginx -s stop

编写测试程序

from socket import socket, AF_INET, SOCK_STREAM, SOL_SOCKET, SO_REUSEADDR
tcp_server_socket = socket(AF_INET, SOCK_STREAM)
address = ('', 8000)
tcp_server_socket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
tcp_server_socket.bind(address)
tcp_server_socket.listen(128)
while True:
    print("等待连接")
    tcp_client_socket,addr = tcp_server_socket.accept()
    print('...connected from', addr)
    while True:
        data = tcp_client_socket.recv(1024)
        if not data:
            break
        try:
            if data.decode().startswith('exit'):
                break
        except Exception as e:
            print(e)
            break
        print(data)
        tcp_client_socket.send('server recved.'.encode())
    tcp_client_socket.close()
tcp_server_socket.close()

监听8000端口。python3 test.py
在其它电脑上测试nginx代理TCP。

telnet 192.168.0.100 7000

作者:Baloneo