个人随笔
Nginx负载均衡搭建
2019-07-08 23:59:23

Nginx的简介就不多说了,直接说使用即可,我们用nginx来都是当做服务器来接受客户端的请求,内部转发到应用。这里介绍一下怎么用nginx来搭建负载均衡。

nginx搭建

http://nginx.org/en/download.html 选择第一个window即可,下载完后双击nginx.exe启动,页面输入localhost会返回如下页面就表示搭建成功啦。

搭建负载均衡

有时候我们有几台服务器,用来缓解服务端的压力,那么此时可以用nginx搭建负载均衡,我这里就直接用端口区分即可,如下,在nginx.conf配置文件的http中加入内容:

  1. server {
  2. listen 8080;
  3. server_name localhost;
  4. location / {
  5. root html;
  6. index index8080.html index.htm;
  7. }
  8. error_page 500 502 503 504 /50x.html;
  9. location = /50x.html {
  10. root html;
  11. }
  12. }
  13. server {
  14. listen 8081;
  15. server_name localhost;
  16. location / {
  17. root html;
  18. index index8081.html index.htm;
  19. }
  20. error_page 500 502 503 504 /50x.html;
  21. location = /50x.html {
  22. root html;
  23. }
  24. }

当然这里要在html的目录下新建两个html: index8080.html、index8081.html.

然后,在http中再加入如下内容,设置负载均衡,负载均衡策略默认为轮询:

  1. upstream first {
  2. server 127.0.0.1:8080;
  3. server 127.0.0.1:8081;
  4. }

最后,修改80请求的策略到负载均衡first:

  1. server {
  2. listen 80;
  3. server_name localhost;
  4. location / {
  5. proxy_pass http://first;
  6. proxy_set_header Host $host;
  7. proxy_set_header X-Real-IP $remote_addr;
  8. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  9. }
  10. }

重新启动nginx,访问localhost,会发现轮询访问了8080和80801端口,到此负载均衡搭建完毕。


nginx的默认配置文件

这个跟本篇博文无关

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