关于nginx做负载均衡的配置以及各个配置的含义(简)

最近在公司做了一个项目,整体是分为service层和web层两个项目,中间通过springmvc的http接口调用.主要的业务逻辑都是在service层中去处理.而web层负责后台数据的管理,主要是提供给后台管理员使用.

项目的架构是两台阿里云的linux服务器,硬件配置为250G硬盘,8G内存,4核cpu;每台机器上部署一个service服务和一个web服务,一个nginx负载均衡服务.

web服务来说,对外暴露的是LBS的外网地址,web调用service是调用nginx地址,nginx为service做的负载均衡.会将请求负载到两台linux服务上去处理,web请求的是本台服务器上的nginx地址.

service服务,对外提供的接口服务都是http接口.用LBS对两台服务器上的nginx服务都做了重新负载分配.这样做的原因是阿里云做的LBS不能在内网之间调用的限制.service对外暴露的是LBS的内网地址,这样是考虑到service服务的安全性,主要是提供给公司内部的业务系统使用的.

在web调用service的nginx负载服务时,就要对nginx进行配置.下面附上配置图解:

 

[java] view plain copy
 
  1. #user  nobody;  
  2. worker_processes  1;  
  3.   
  4. #error_log  logs/error.log;  
  5. #error_log  logs/error.log  notice;  
  6. #error_log  logs/error.log  info;  
  7.   
  8. #pid        logs/nginx.pid;  
  9.   
  10.   
  11. events {  
  12.     worker_connections  1024;  
  13. }  
  14.   
  15. #标识http协议的一些日志格式和数据类型  
  16. http {  
  17.     include       mime.types;  
  18.     default_type  application/octet-stream;  
  19.   
  20.     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  21.                       '$status $body_bytes_sent "$http_referer" '  
  22.                       '"$http_user_agent" "$http_x_forwarded_for"';  
  23.   
  24.     #access_log  logs/access.log  main;  
  25.   
  26.     sendfile        on;  
  27.     #tcp_nopush     on;  
  28.   
  29.     #keepalive_timeout  0;  
  30.     keepalive_timeout  65;  
  31.   
  32.     #gzip  on;  
  33.   
  34.       
  35.       
  36.     #服务器的集群 (就是负载的配置信息,请求来了之后需要nginx分发请求到哪些机器上)   
  37.     upstream  test-service {  #服务器集群名字,此处叫做 test-service   
  38.         server    192.168.13.227:9080;#服务器配置   weight是权重的意思,权重越大,分配的概率越大。  
  39.           
  40.         server    192.168.13.80:8080;   
  41.           
  42.     }   
  43.     #nginx服务器的监听ip和端口,使用的编码格式  
  44.     server {  
  45.         listen       80;  
  46.         server_name  192.168.13.80;  
  47.         charset utf-8;  
  48.         #charset koi8-r;  
  49.   
  50.         #access_log  logs/host.access.log  main;  
  51.   
  52.         #nginx的常用配置  
  53.         location / { #location代表拦截的请求路径/标识全部拦截  
  54.             proxy_pass http://test-service;  #固定格式标识负载到哪个upstream上,这里选择之前配置的test-service上.即http://test-service=http://+test-service (基本配置这一项目就可以实现负载均衡,以下配置按需配置) 
  55.             proxy_redirect off;   
  56.               
  57.               
  58.             proxy_set_header        Host            $host;  
  59.             proxy_set_header        X-Real-IP       $remote_addr;  
  60.             proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;  
  61.             client_max_body_size    10m;  
  62.             client_body_buffer_size 128k;  
  63.             proxy_connect_timeout   3;  
  64.             proxy_send_timeout      30;  
  65.             proxy_read_timeout      30;  
  66.             proxy_buffer_size       128k;  
  67.             proxy_buffers           4 256k;  
  68.             proxy_busy_buffers_size 256k;  
  69.             proxy_temp_file_write_size  256k;  
  70.               
  71.               
  72.             #root   html;  
  73.             #index  index.html index.htm;  
  74.         }  
  75.   
  76.         #error_page  404              /404.html;  
  77.   
  78.         # redirect server error pages to the static page /50x.html  
  79.         #错误页面的跳转到哪些html上在nginx目录当中都是有的  
  80.         error_page   500 502 503 504  /50x.html;  
  81.         location = /50x.html {  
  82.             root   html;  
  83.         }  
  84.   
  85.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
  86.         #  
  87.         #location ~ \.php$ {  
  88.         #    proxy_pass   http://127.0.0.1;  
  89.         #}  
  90.   
  91.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
  92.         #  
  93.         #location ~ \.php$ {  
  94.         #    root           html;  
  95.         #    fastcgi_pass   127.0.0.1:9000;  
  96.         #    fastcgi_index  index.php;  
  97.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
  98.         #    include        fastcgi_params;  
  99.         #}  
  100.   
  101.         # deny access to .htaccess files, if Apache's document root  
  102.         # concurs with nginx's one  
  103.         #  
  104.         #location ~ /\.ht {  
  105.         #    deny  all;  
  106.         #}  
  107.     }  
  108.   
  109.   
  110.     # another virtual host using mix of IP-, name-, and port-based configuration  
  111.     #  
  112.     #server {  
  113.     #    listen       8000;  
  114.     #    listen       somename:8080;  
  115.     #    server_name  somename  alias  another.alias;  
  116.   
  117.     #    location / {  
  118.     #        root   html;  
  119.     #        index  index.html index.htm;  
  120.     #    }  
  121.     #}  
  122.   
  123.   
  124.     # HTTPS server  
  125.     #  
  126.     #server {  
  127.     #    listen       443 ssl;  
  128.     #    server_name  localhost;  
  129.   
  130.     #    ssl_certificate      cert.pem;  
  131.     #    ssl_certificate_key  cert.key;  
  132.   
  133.     #    ssl_session_cache    shared:SSL:1m;  
  134.     #    ssl_session_timeout  5m;  
  135.   
  136.     #    ssl_ciphers  HIGH:!aNULL:!MD5;  
  137.     #    ssl_prefer_server_ciphers  on;  
  138.   
  139.     #    location / {  
  140.     #        root   html;  
  141.     #        index  index.html index.htm;  
  142.     #    }  
  143.     #}  
  144.   
  145. }  
posted @ 2018-05-19 23:25  西伯利亚狼520  阅读(131)  评论(0)    收藏  举报