Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> 使用nginx作為tcp代理的安裝配置例子

使用nginx作為tcp代理的安裝配置例子

編輯:關於android開發

使用nginx作為tcp代理的安裝配置例子


以前一般代理tcp時候,都選用haproxy。nginx 1.9 之後,就多了一個選擇。可以使用nginx做代理。這樣大家也不用重新去熟悉haproxy。
tcp 代理本質是 端口到端口映射轉發,比較簡單。沒有http層代理那麼復雜。我一般用來代理https,大家都知道,以前使用nginx 代理https要配置證書等各種瑣事。然而對於tcp層來說,沒有證書一說。純粹轉發簡單干淨的配置,非常的爽; 當然有時候也用tcp替換http層代理。原因是tcp代理配置更加簡單,性能也更高。

一、nginx 下載安裝

方式一:下載地址:http://nginx.org/en/download.html方式二:通過rpm包安裝

1、添加安裝源,在/etc/yum.repos.d底下創建文件nginx.repo

  1. [nginx]
  2. name=nginx repo
  3. baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
  4. gpgcheck=0
  5. enabled=1
2、安裝nginx

  1. #yum clean all
  2. #yum install nginx
3、查看安裝路徑和版本

  1. #whereis nginx
  2. #/usr/sbin/nginx -v
  3. nginx version: nginx/1.10.1
4、查看缺省配置文件路徑

  1. #/usr/sbin/nginx -h
  2. -c filename : set configuration file (default: /etc/nginx/nginx.conf)

二、nginx配置

1、目錄規劃

  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、配置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/nginx.pid;

  6. events {
  7. use epoll;
  8. worker_connections 10240;
  9. }
  10. include /opt/service/nginx/conf/*.conf;
3、配置tcp 代理
/opt/service/nginx/conf/nginx_tcp_proxy.conf

  1. stream {
  2. #---------------------------------------------------------------------
  3. # tcp 代理
  4. #---------------------------------------------------------------------
  5. upstream weixin_proxy {
  6. hash $remote_addr consistent;
  7. server wx.qq.com:443 weight=1 max_fails=3 fail_timeout=60s;
  8. }
  9. server {
  10. listen 443;
  11. proxy_connect_timeout 10s;
  12. proxy_pass weixin_proxy;
  13. proxy_buffer_size 64k;
  14. }
  15. }

三、啟動維護nginx

1、編輯 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/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、啟動nginx.sh

  1. chmod a+rwx nginx.sh
  2. ./nginx.sh restart

整個目錄結構如下

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



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved