tcp 代理本质是 端口到端口映射转发,比较简单。没有http层代理那么复杂。我一般用来代理https,大家都知道,以前使用nginx 代理https要配置证书等各种琐事。然而对于tcp层来说,没有证书一说。纯粹转发简单干净的配置,非常的爽; 当然有时候也用tcp替换http层代理。原因是tcp代理配置更加简单,性能也更高。
一、nginx 下载安装
方式一:下载地址:http://nginx.org/en/download.html方式二:通过rpm包安装1、添加安装源,在/etc/yum.repos.d底下创建文件nginx.repo
- [nginx]
- name=nginx repo
- baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
- gpgcheck=0
- enabled=1
添加安装源 /etc/apt/sources.list.d/nginx.list, 文件内容为
- deb http://nginx.org/packages/ubuntu/ xenial nginx
deb-src http://nginx.org/packages/ubuntu/ xenial nginx
2、安装nginx
- #yum clean all
- #yum install nginx
如果是ubuntu
- #sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys $key
#sudo apt-get update
#sudo apt-get install nginx
3、查看安装路径和版本
- #whereis nginx
- #/usr/sbin/nginx -v
- nginx version: nginx/1.10.1
- #/usr/sbin/nginx -h
- -c filename : set configuration file (default: /etc/nginx/nginx.conf)
二、nginx配置
1、目录规划
/opt/service/nginx/
├── conf
├── log -> /opt/logs/nginx
└── nginx -> /usr/sbin/nginx
2、配置nginx.conf
3、配置tcp 代理
/opt/service/nginx/conf/nginx_tcp_proxy.conf
2、启动 nginx.sh
整个目录结构如下
问题:
当代理的后端是域名的时候,域名的dns发生变化。nginx并不知道。看文档nginx提供了定时刷新dns的功能,但是我配置好像没有起到作用。
在 nginx.conf 尾部加上这个配置
resolver 100.100.2.136 valid=1s;
include /etc/nginx/nginx_vhost/*.conf;
- mkdir -p /opt/service/nginx/conf
- mkdir -p /opt/logs/nginx
- cd /opt/service/nginx
- ln -s /usr/sbin/nginx
- ln -s /opt/logs/nginx log
├── conf
├── log -> /opt/logs/nginx
└── nginx -> /usr/sbin/nginx
2、配置nginx.conf
- user nginx;
- worker_processes 16;
- worker_rlimit_nofile 100000;
- error_log /opt/service/nginx/log/error.log error;
- pid /opt/service/nginx/nginx.pid;
events {- use epoll;
- worker_connections 10240;
- }
- include /opt/service/nginx/conf/*.conf;
/opt/service/nginx/conf/nginx_tcp_proxy.conf
- stream {
- #---------------------------------------------------------------------
- # tcp 代理
- #---------------------------------------------------------------------
- upstream weixin_proxy {
- hash $remote_addr consistent;
- server wx.qq.com:443 weight=1 max_fails=3 fail_timeout=60s;
- }
- server {
- listen 443;
- proxy_connect_timeout 10s;
- proxy_pass weixin_proxy;
- proxy_buffer_size 64k;
- }
- }
三、启动维护nginx
1、编辑 nginx.sh- #!/bin/sh
- # description:nginx server
- # nginx - this script is used to control nginx service
- # processname nginx
- # nginx version: nginx/1.10.1
-
- nginx="/usr/sbin/nginx"
- prog="nginx"
- conf_file="/etc/nginx/nginx.conf"
-
- start() {
- if [ `pgrep $prog | wc -l` -eq 2 ];then
- if [ -x $nginx ] && [ -f $conf_file ];then
- $nginx -c $conf_file
- ret=$?
- if [ $ret -eq 0 ];then
- echo "$prog start successed"
- else
- echo "$prog start failed"
- fi
- else
- echo "$prog config file not exist"
- fi
- else
- num=`pgrep $prog `
- echo "$prog is already started ... $num "
- fi
- }
-
-
- stop() {
- if [ `pgrep $prog | wc -l` -ne 2 ];then
- killall -9 $prog
- ret=$?
- if [ $ret -eq 0 ];then
- echo "$prog stop successed"
- else
- echo "$prog stop failed"
- fi
- else
- echo "$prog is already stopped ..."
- fi
- }
-
- restart() {
- stop
- sleep 2
- start
- }
-
- reload() {
- if [ `pgrep $prog | wc -l` -ne 0 ];then
- pid=`ps -ef | grep $prog | grep master | awk '{print $2}'`
- if [ -x $nginx ] && [ -f $conf_file ];then
- kill -HUP $pid
- ret=$?
- if [ $ret -eq 0 ];then
- echo "$prog reload successed"
- else
- echo "$prog reload failed"
- fi
- else
- echo "$prog config file is not exist"
- fi
- else
- echo "$prog is stopped, please start $prog first ..."
- fi
- }
-
- check() {
- if [ -x $nginx ] && [ -f $conf_file ];then
- $nginx -t -c $conf_file
- ret=$?
- if [ $ret -eq 0 ];then
- echo "$prog check successed"
- else
- echo "$prog check failed"
- fi
- else
- echo "$prog program or config file not exit!"
- fi
- }
-
- case $1 in
- start)
- start
- ;;
- stop)
- stop
- ;;
- restart)
- restart
- ;;
- reload)
- reload
- ;;
- check)
- check
- ;;
- *)
- echo "Usage: $0 {start|stop|restart|reload|check}"
- esac
2、启动 nginx.sh
- chmod a+rwx nginx.sh
- ./nginx.sh restart
整个目录结构如下
- # ll
- total 8
- drwxr-xr-x 2 root root 33 Dec 21 17:16 conf
- lrwxrwxrwx 1 root root 15 Dec 21 17:00 log -> /opt/logs/nginx
- lrwxrwxrwx 1 root root 15 Dec 21 16:59 nginx -> /usr/sbin/nginx
- -rw-r--r-- 1 root root 6 Dec 21 17:21 nginx.pid
- -rwxrwxrwx 1 root root 2172 Dec 21 17:20 nginx.sh
问题:
当代理的后端是域名的时候,域名的dns发生变化。nginx并不知道。看文档nginx提供了定时刷新dns的功能,但是我配置好像没有起到作用。
在 nginx.conf 尾部加上这个配置
resolver 100.100.2.136 valid=1s;
include /etc/nginx/nginx_vhost/*.conf;