ARM Nginx TCP代理
下載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
修改之後如下
#usernobody; worker_processes1; #error_loglogs/error.log; #error_loglogs/error.lognotice; #error_loglogs/error.loginfo; #pidlogs/nginx.pid; events { worker_connections1024; } 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 { includemime.types; default_typeapplication/octet-stream; #log_formatmain'$remote_addr - $remote_user [$time_local] "$request" ' #'$status $body_bytes_sent "$http_referer" ' #'"$http_user_agent" "$http_x_forwarded_for"'; #access_loglogs/access.logmain; sendfileon; #tcp_nopushon; #keepalive_timeout0; keepalive_timeout65; #gzipon; server { listen80; server_namelocalhost; #charset koi8-r; #access_loglogs/host.access.logmain; location / { roothtml; indexindex.html index.htm; } #error_page404/404.html; # redirect server error pages to the static page /50x.html # error_page500 502 503 504/50x.html; location = /50x.html { roothtml; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { #proxy_passhttp://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { #roothtml; #fastcgi_pass127.0.0.1:9000; #fastcgi_indexindex.php; #fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name; #includefastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { #denyall; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { #listen8000; #listensomename:8080; #server_namesomenamealiasanother.alias; #location / { #roothtml; #indexindex.html index.htm; #} #} # HTTPS server # #server { #listen443 ssl; #server_namelocalhost; #ssl_certificatecert.pem; #ssl_certificate_keycert.key; #ssl_session_cacheshared:SSL:1m; #ssl_session_timeout5m; #ssl_ciphersHIGH:!aNULL:!MD5; #ssl_prefer_server_cipherson; #location / { #roothtml; #indexindex.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