Install and configure nginx as a tcp proxy
Previously, haproxy was used for Proxy tcp. After nginx 1.9, one more choice is available. You can use nginx as a proxy. In this way, you do not need to get familiar with haproxy again.
Tcp proxy is essentially port-to-port ing and forwarding, which is relatively simple. Not as complex as http layer proxy. I usually use https proxy. As we all know, I used to use nginx proxy https to configure certificates and other things. However, for the tcp layer, there is no certificate. Purely simple and clean forwarding configuration, which is very refreshing; of course, sometimes the http layer proxy is replaced with tcp. The reason is that the tcp proxy configuration is simpler and the performance is higher.
I. nginx download and Installation Method 1: http://nginx.org/en/download.html ::::through rpmpackage Installation
1. Add the installation source and create the nginx. repo file under/etc/yum. repos. d.
- [Nginx]
- Name = nginx repo
- Baseurl = http://nginx.org/packages/centos/?releasever/?basearch/
- Gpgcheck = 0
- Enabled = 1
2. Install nginx
- # Yum clean all
- # Yum install nginx
3. view the installation path and version
- # Whereis nginx
- #/Usr/sbin/nginx-v
- Nginx version: nginx/1.10.1
4. view the default configuration file path
- #/Usr/sbin/nginx-h
- -C filename: set configuration file (default:/etc/nginx. conf)
Ii. nginx configuration 1. Directory Planning
- 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
/Opt/service/nginx/
── Conf
── Log->/opt/logs/nginx
── Nginx->/usr/sbin/nginx
2. Configure nginx. conf
- User nginx;
- Worker_processes 16;
- Worker_rlimit_nofile 100000;
- Error_log/opt/service/nginx/log/error. log error;
- Pid/opt/service/nginx. pid;
Events {
- Use epoll;
- Worker_connections 10240;
- }
- Include/opt/service/nginx/conf/*. conf;
3. Configure the tcp proxy
/Opt/service/nginx/conf/nginx_tcp_proxy.conf
- Stream {
- #---------------------------------------------------------------------
- # Tcp proxy
- #---------------------------------------------------------------------
- Upstream weixin_proxy {
- Hash $ remote_addr consistent;
- Server wx.qq.com: 443 weight = 1 max_fails = 3 fail_timeout = 60 s;
- }
- Server {
- Listen 443;
- Proxy_connect_timeout 10 s;
- Proxy_pass weixin_proxy;
- Proxy_buffer_size 64 k;
- }
- }
3. Start and maintain nginx1 and edit 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. 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. Start nginx. sh
- Chmod a + rwx nginx. sh
- ./Nginx. sh restart
The entire directory structure is as follows:
- # Ll
- Total 8
- Drwxr-xr-x 2 root 33 Dec 21 17: 16 conf
- Lrwxrwxrwx 1 root 15 Dec 21 17: 00 log->/opt/logs/nginx
- Lrwxrwxrwx 1 root 15 Dec 21 16:59 nginx->/usr/sbin/nginx
- -Rw-r -- 1 root 6 Dec 21 17:21 nginx. pid
- -Rwxrwxrwx 1 root 2172 Dec 21 17:20 nginx. sh
Problem:
When the back-end of Contemporary management is a domain name, the dns of the domain name changes. Nginx does not know. See the document. nginx provides the dns refresh function on a regular basis, but my configuration does not seem to play a role.
Add this configuration at the end of nginx. conf.
Resolver 100.100.2.136 valid = 1 s;
Include/etc/nginx/nginx_vhost/*. conf;