Install and configure nginx as a tcp proxy

Source: Internet
Author: User
Tags haproxy

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.

  1. [Nginx]
  2. Name = nginx repo
  3. Baseurl = http://nginx.org/packages/centos/?releasever/?basearch/
  4. Gpgcheck = 0
  5. Enabled = 1
2. Install nginx

  1. # Yum clean all
  2. # Yum install nginx
3. view the installation path and version

  1. # Whereis nginx
  2. #/Usr/sbin/nginx-v
  3. Nginx version: nginx/1.10.1
4. view the default configuration file path

  1. #/Usr/sbin/nginx-h
  2. -C filename: set configuration file (default:/etc/nginx. conf)
Ii. nginx configuration 1. Directory Planning

  1. Mkdir-p/opt/service/nginx/conf
  2. Mkdir-p/opt/logs/nginx
  3. Cd/opt/service/nginx
  4. Ln-s/usr/sbin/nginx
  5. Ln-s/opt/logs/nginx log
/Opt/service/nginx/
── Conf
── Log->/opt/logs/nginx
── Nginx->/usr/sbin/nginx

2. Configure nginx. conf

  1. User nginx;
  2. Worker_processes 16;
  3. Worker_rlimit_nofile 100000;
  4. Error_log/opt/service/nginx/log/error. log error;
  5. Pid/opt/service/nginx. pid;

  6. Events {
  7. Use epoll;
  8. Worker_connections 10240;
  9. }
  10. Include/opt/service/nginx/conf/*. conf;
3. Configure the tcp proxy
/Opt/service/nginx/conf/nginx_tcp_proxy.conf

  1. Stream {
  2. #---------------------------------------------------------------------
  3. # Tcp proxy
  4. #---------------------------------------------------------------------
  5. Upstream weixin_proxy {
  6. Hash $ remote_addr consistent;
  7. Server wx.qq.com: 443 weight = 1 max_fails = 3 fail_timeout = 60 s;
  8. }
  9. Server {
  10. Listen 443;
  11. Proxy_connect_timeout 10 s;
  12. Proxy_pass weixin_proxy;
  13. Proxy_buffer_size 64 k;
  14. }
  15. }
3. Start and maintain nginx1 and edit nginx. sh

  1. #! /Bin/sh
  2. # Description: nginx server
  3. # Nginx-this script is used to control nginx service
  4. # Processname nginx
  5. # Nginx version: nginx/1.10.1

  6. Nginx = "/usr/sbin/nginx"
  7. Prog = "nginx"
  8. Conf_file = "/etc/nginx. conf"

  9. Start (){
  10. If ['pgrep $ prog | wc-l'-eq 2]; then
  11. If [-x $ nginx] & [-f $ conf_file]; then
  12. $ Nginx-c $ conf_file
  13. Ret = $?
  14. If [$ ret-eq 0]; then
  15. Echo "$ prog start successed"
  16. Else
  17. Echo "$ prog start failed"
  18. Fi
  19. Else
  20. Echo "$ prog config file not exist"
  21. Fi
  22. Else
  23. Num = 'pgrep $ prog'
  24. Echo "$ prog is already started... $ num"
  25. Fi
  26. }


  27. Stop (){
  28. If ['pgrep $ prog | wc-l'-ne 2]; then
  29. Killall-9 $ prog
  30. Ret = $?
  31. If [$ ret-eq 0]; then
  32. Echo "$ prog stop successed"
  33. Else
  34. Echo "$ prog stop failed"
  35. Fi
  36. Else
  37. Echo "$ prog is already stopped ..."
  38. Fi
  39. }

  40. Restart (){
  41. Stop
  42. Sleep 2
  43. Start
  44. }

  45. Reload (){
  46. If ['pgrep $ prog | wc-l'-ne 0]; then
  47. Pid = 'ps-ef | grep $ prog | grep master | awk '{print $2 }''
  48. If [-x $ nginx] & [-f $ conf_file]; then
  49. Kill-HUP $ pid
  50. Ret = $?
  51. If [$ ret-eq 0]; then
  52. Echo "$ prog reload successed"
  53. Else
  54. Echo "$ prog reload failed"
  55. Fi
  56. Else
  57. Echo "$ prog config file is not exist"
  58. Fi
  59. Else
  60. Echo "$ prog is stopped, please start $ prog first ..."
  61. Fi
  62. }

  63. Check (){
  64. If [-x $ nginx] & [-f $ conf_file]; then
  65. $ Nginx-t-c $ conf_file
  66. Ret = $?
  67. If [$ ret-eq 0]; then
  68. Echo "$ prog check successed"
  69. Else
  70. Echo "$ prog check failed"
  71. Fi
  72. Else
  73. Echo "$ prog program or config file not exit! "
  74. Fi
  75. }

  76. Case $1 in
  77. Start)
  78. Start
  79. ;;
  80. Stop)
  81. Stop
  82. ;;
  83. Restart)
  84. Restart
  85. ;;
  86. Reload)
  87. Reload
  88. ;;
  89. Check)
  90. Check
  91. ;;
  92. *)
  93. Echo "Usage: $0 {start | stop | restart | reload | check }"
  94. Esac


2. Start nginx. sh

  1. Chmod a + rwx nginx. sh
  2. ./Nginx. sh restart

The entire directory structure is as follows:

  1. # Ll
  2. Total 8
  3. Drwxr-xr-x 2 root 33 Dec 21 17: 16 conf
  4. Lrwxrwxrwx 1 root 15 Dec 21 17: 00 log->/opt/logs/nginx
  5. Lrwxrwxrwx 1 root 15 Dec 21 16:59 nginx->/usr/sbin/nginx
  6. -Rw-r -- 1 root 6 Dec 21 17:21 nginx. pid
  7. -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;



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.