当前位置:   article > 正文

nginx作为tcp的负载均衡

nginx作为tcp的负载均衡

背景: 

之前总以为nginx只能当静态资源web容器和web的负载均衡,现在才知道原来也可以当tcp的负载均衡。现在我们项目测试服就是一个nginx对应了2个测试服的负载均衡。配置如下:

  1. [root@localhost conf]# cat nginx.conf
  2. #user nobody;
  3. worker_processes 1;
  4. #error_log logs/error.log;
  5. #error_log logs/error.log notice;
  6. #error_log logs/error.log info;
  7. #pid logs/nginx.pid;
  8. events {
  9. worker_connections 1024;
  10. }
  11. http {
  12. include mime.types;
  13. default_type application/octet-stream;
  14. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  15. # '$status $body_bytes_sent "$http_referer" '
  16. # '"$http_user_agent" "$http_x_forwarded_for"';
  17. #access_log logs/access.log main;
  18. sendfile on;
  19. #tcp_nopush on;
  20. #keepalive_timeout 0;
  21. keepalive_timeout 65;
  22. #gzip on;
  23. server {
  24. listen 80;
  25. server_name localhost;
  26. #charset koi8-r;
  27. #access_log logs/host.access.log main;
  28. location / {
  29. root html;
  30. index index.html index.htm;
  31. }
  32. #error_page 404 /404.html;
  33. # redirect server error pages to the static page /50x.html
  34. #
  35. error_page 500 502 503 504 /50x.html;
  36. location = /50x.html {
  37. root html;
  38. }
  39. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  40. #
  41. #location ~ \.php$ {
  42. # proxy_pass http://127.0.0.1;
  43. #}
  44. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  45. #
  46. #location ~ \.php$ {
  47. # root html;
  48. # fastcgi_pass 127.0.0.1:9000;
  49. # fastcgi_index index.php;
  50. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  51. # include fastcgi_params;
  52. #}
  53. # deny access to .htaccess files, if Apache's document root
  54. # concurs with nginx's one
  55. #
  56. #location ~ /\.ht {
  57. # deny all;
  58. #}
  59. }
  60. # another virtual host using mix of IP-, name-, and port-based configuration
  61. #
  62. #server {
  63. # listen 8000;
  64. # listen somename:8080;
  65. # server_name somename alias another.alias;
  66. # location / {
  67. # root html;
  68. # index index.html index.htm;
  69. # }
  70. #}
  71. # HTTPS server
  72. #
  73. #server {
  74. # listen 443 ssl;
  75. # server_name localhost;
  76. # ssl_certificate cert.pem;
  77. # ssl_certificate_key cert.key;
  78. # ssl_session_cache shared:SSL:1m;
  79. # ssl_session_timeout 5m;
  80. # ssl_ciphers HIGH:!aNULL:!MD5;
  81. # ssl_prefer_server_ciphers on;
  82. # location / {
  83. # root html;
  84. # index index.html index.htm;
  85. # }
  86. #}
  87. }
  88. stream {
  89. // 2个tcp负载均衡
  90. upstream gateway {
  91. server 10.x.x.206:10001 ;
  92. server 10.x.x.207:10001 ;
  93. }
  94. server {
  95. # 监听到1W端口
  96. listen 10000;
  97. # proxy_connect_timeout 2s;
  98. # proxy_timeout 30s;
  99. proxy_pass gateway;
  100. }
  101. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/216805
推荐阅读
相关标签
  

闽ICP备14008679号