centos7 Nginx 多tomcat实现负载均衡

nginx是一个web服务器,类似apache一样,但是比apache性能更好,更快。还可以实现反向代理,和负载均衡,常用于构建web服务集群的负载均衡。今天就来记录一下,nginx的安装与配置,实现负载均衡的作用。


首先,安装
$ wget http://dl.Fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm  
(ps:网上有一些源是不完全的,只是安装了简单的nginx,其他一些依赖并没有一起安装,导致配置文件不 全。请用这里的连接,完整可用。)
$ rpm -ivh epel-release-latest-7.noarch.rpm
$ yum install nginx (直接yum安装)


安装就这么简单方便,安装完成后,就可以使用systemctl来控制nginx的启动了
$ systemctl enable nginx (加入开机启动)
$ systemctl start nginx (开启nginx)
$ systemctl status nginx (查看状态)


然后就是设置nginx的配置文件,实现负载均衡。顾名思义就是将多个请求分发到不同的服务上,实现均衡的负载,减小单个服务的压力。
$ vi /etc/nginx/nginx.conf  (修改配置文件,全局配置文件)
-------------------------------------------------------------------------------
[plain]  view plain  copy
  1. # For more information on configuration, see:  
  2. #   * Official English Documentation: http://nginx.org/en/docs/  
  3. #   * Official Russian Documentation: http://nginx.org/ru/docs/  
  4.   
  5.   
  6. user nginx;  
  7. worker_processes auto; #(默认为自动,可以自己设置,一般不大于cpu核数)  
  8. error_log /var/log/nginx/error.log; #(错误日志路径)  
  9. pid /run/nginx.pid; #(pid文件路径)  
  10.   
  11.   
  12. # Load dynamic modules. See /usr/share/nginx/README.dynamic.  
  13. include /usr/share/nginx/modules/*.conf;  
  14.   
  15.   
  16. events {  
  17.     accept_mutex on; #(设置网路连接序列化,防止惊群现象发生,默认为on)  
  18.     multi_accept on; #(设置一个进程是否同时接受多个网络连接,默认为off)  
  19.     worker_connections 1024; #(一个进程的最大连接数)  
  20. }  
  21.   
  22.   
  23. http {  
  24.     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  25.                       '$status $body_bytes_sent "$http_referer" '  
  26.                       '"$http_user_agent" "$http_x_forwarded_for"';  
  27.   
  28.   
  29.     access_log  /var/log/nginx/access.log  main;  
  30.   
  31.   
  32.     sendfile            on;  
  33.     # tcp_nopush          on; (这里注释掉)  
  34.     tcp_nodelay         on;  
  35.     keepalive_timeout   65; #(连接超时时间)  
  36.     types_hash_max_size 2048;  
  37.     gzip on; #(开启压缩)  
  38.     include             /etc/nginx/mime.types;  
  39.     default_type        application/octet-stream;  
  40.   
  41.   
  42.     # Load modular configuration files from the /etc/nginx/conf.d directory.  
  43.     # See http://nginx.org/en/docs/ngx_core_module.html#include  
  44.     # for more information.  
  45.     include /etc/nginx/conf.d/*.conf;  
  46.   
  47.   
  48. # 这里设置负载均衡,负载均衡有多中策略,nginx自带的有轮询,权重,ip-hash,响应时间等粗略。  
  49. # 默认为平分http负载,为轮询的方式。  
  50. # 权重则是按照权重来分发请求,权重高的负载大  
  51. # ip-hash,根据ip来分配,保持同一个ip分在同一台服务器上。  
  52. # 响应时间,根据服务器都nginx 的响应时间,优先分发给响应速度快的服务器。  
  53. 集中策略可以适当组合  
  54.     upstream tomcat { #(tomcat为自定义的负载均衡规则名)  
  55.         ip_hash; (ip_hash则为ip-hash方法)  
  56.         server 192.168.14.132:8080 weight=5; #(weihgt为权重)  
  57.         server 192.168.14.133:8080 weight=3;  
  58.   
  59.   
  60. ## 可以定义多组规则  
  61. }  
  62.   
  63.   
  64.   
  65.   
  66.     server {  
  67.         listen       80 default_server; #(默认监听80端口)  
  68.         listen       localhost; #(监听的服务器)  
  69.         server_name  _;  
  70.         root         /usr/share/nginx/html;  
  71.   
  72.   
  73.         # Load configuration files for the default server block.  
  74.         include /etc/nginx/default.d/*.conf;  
  75.   
  76.   
  77.         location / { #( /  表示所有请求,可以自定义来针对不同的域名设定不同负载规则 和服务)  
  78.           proxy_pass    http://tomcat; (反向代理,填上你自己的负载均衡规则名)  
  79.           proxy_redirect off; #(下面一些设置可以直接复制过去,不要的话,有可能导致一些 没法认证等的问题)  
  80.           proxy_set_header Host $host;  
  81.           proxy_set_header X-Real-IP $remote_addr;  
  82.           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
  83.           proxy_connect_timeout 90; #(下面这几个都只是一些超时设置,可不要)  
  84.           proxy_send_timeout 90;  
  85.           proxy_read_timeout 90;  
  86.         }  
  87.    # location ~\.(gif|jpg|png)$ { #(比如,以正则表达式写)    
  88.    #  root /home/root/images;  
  89.    #  }  
  90.   
  91.   
  92.         error_page 404 /404.html;  
  93.             location = /40x.html {  
  94.         }  
  95.   
  96.   
  97.         error_page 500 502 503 504 /50x.html;  
  98.             location = /50x.html {  
  99.         }  
  100.     }  
  101.   
  102.   
  103. # Settings for a TLS enabled server.  
  104. #  
  105. #    server {  
  106. #        listen       443 ssl http2 default_server;  
  107. #        listen       [::]:443 ssl http2 default_server;  
  108. #        server_name  _;  
  109. #        root         /usr/share/nginx/html;  
  110. #  
  111. #        ssl_certificate "/etc/pki/nginx/server.crt";  
  112. #        ssl_certificate_key "/etc/pki/nginx/private/server.key";  
  113. #        ssl_session_cache shared:SSL:1m;  
  114. #        ssl_session_timeout  10m;  
  115. #        ssl_ciphers HIGH:!aNULL:!MD5;  
  116. #        ssl_prefer_server_ciphers on;  
  117. #  
  118. #        # Load configuration files for the default server block.  
  119. #        include /etc/nginx/default.d/*.conf;  
  120. #  
  121. #        location / {  
  122. #        }  
  123. #  
  124. #        error_page 404 /404.html;  
  125. #            location = /40x.html {  
  126. #        }  
  127. #  
  128. #        error_page 500 502 503 504 /50x.html;  
  129. #            location = /50x.html {  
  130. #        }  
  131. #    }  
  132.   
  133.   
  134. }  

猜你喜欢

转载自blog.csdn.net/weixin_41690905/article/details/80745774