Home  >  Article  >  Backend Development  >  Java cluster optimization - Nginx+tomcat cluster configuration - practice

Java cluster optimization - Nginx+tomcat cluster configuration - practice

WBOY
WBOYOriginal
2016-08-10 08:48:36928browse

针 Previous article, we introduced the theory of Nginx+Tomcat technical theory in the cluster. Today, let's complete a simple cluster to set up a preview.

Configuration steps:

1. Install Download the latest Nginx from the Nginx official website download page (http://nginx.org/en/download.html) version (currently 1.9 .3 version) installation package, unzip it and copy it to the deployment directory.

2. Start and stop Nginx

Nginx currently only supports command line operations. Before operating, enter the Dos command environment (cmd command) and enter the Nginx deployment directory. Start Nginx: start nginx

Stop Nginx: nginx -s stop Restart after modifying the configuration: nginx -s reload
These three commands can be made into bat files and placed in the deployment directory to facilitate subsequent operations.
Start nginx.bat file content: start nginx

Stop nginx.bat file content: nginx -s stop

reload nginx.bat file content: nginx -s reload



3. Reverse proxy configuration


Modify the content of the nginx.conf file (such as nginx-1.5.13confnginx.conf) in the conf subdirectory under the deployment directory to adjust related configurations.

Reverse proxy configuration example:

location / {

             #设置主机头和客户端真实地址,以便服务器获取客户端真实IP
             proxy_set_header Host $host;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

             #禁用缓存
             proxy_buffering off;

             #设置反向代理的地址
             proxy_pass http://192.168.1.1;       
      }
4. Load balancing configuration

By default, nginx’s upstream implements load balancing in a polling manner. In this way, each request is allocated to the server one by one in chronological order. Different backend servers can be automatically eliminated if the backend server goes down. Another method is ip_hash: each request is allocated according to the hash result of the accessed IP, so that each visitor has fixed access to a back-end server, which can solve the session problem.

Load balancing configuration example
upstream xvshu.com{
             #ip_hash;
             server 192.168.121.251 1;
             server 192.168.121.252 1;
             server 192.168.121.253 1;
         }

server {
        listen       80
        server_name  trffweb;

        location / {

             #反向代理的地址
             proxy_pass http://<span style="font-family: Arial, Helvetica, sans-serif;">xvshu.com</span><span style="font-family: Arial, Helvetica, sans-serif;">;     </span>
        }

}

5. Complete configuration example
nginx.conf:
#Nginx所用用户和组,window下不指定  
#user  niumd niumd;  
  
#工作的子进程数量(通常等于CPU数量或者2倍于CPU)  
worker_processes  1;  
  
#错误日志存放路径  
#error_log  logs/error.log;  
#error_log  logs/error.log  notice;  
error_log  logs/error.log  info;  
  
#指定pid存放文件  
pid        logs/nginx.pid;  
  
events {  
    #使用网络IO模型linux建议epoll,FreeBSD建议采用kqueue,window下不指定。  
    #use epoll;  
      
    #允许最大连接数  
    worker_connections  2048;  
}  
  
http {  
    include       mime.types;  
    default_type  application/octet-stream;  
  
        #定义日志格式  
    #log_format  main  '$remote_addr - $remote_user [$time_local] $request '  
    #                  '"$status" $body_bytes_sent "$http_referer" '  
    #                  '"$http_user_agent" "$http_x_forwarded_for"';  
  
    #access_log  off;  
    #access_log  logs/access.log;  
  
    #client_header_timeout  3m;  
    #client_body_timeout    3m;  
    #send_timeout           3m;  
   
    #client_header_buffer_size    1k;  
    #large_client_header_buffers  4 4k;  
  
    sendfile        on;  
    #tcp_nopush      on;  
    #tcp_nodelay     on;  
  
    keepalive_timeout  75;  
  
    #include    gzip.conf;  
    upstream xvshu.cn{  
      #根据ip计算将请求分配各那个后端tomcat,许多人误认为可以解决session问题,其实并不能。  
      #同一机器在多网情况下,路由切换,ip可能不同  
      #ip_hash;   
      server 192.168.112.250:18080 weight=1;  
server 192.168.112.251:18080 weight=1;  
     }  
  
    server {  
            listen       80;  
            server_name  localhost;     
  
#定义server_name  localhost中的请求都叫给xvshu.cn处理
            location / {  
                    #proxy_connect_timeout   3;  
                    #proxy_send_timeout      30;  
                    #proxy_read_timeout      30;  
                    proxy_pass http://xvshu.cn;
proxy_redirect default;
            }  
              
   }  
}  



Problems encountered:

Reloading nginx service failed, prompting that there is no corresponding service, you can try to restart manually at this time Server

                                                                       

A good tool not only implements good functions, but more importantly, it must have simpler business logic so that users A smooth transition can only be achieved when using this tool, and Nginx is a software that exactly meets these two requirements for developers. If you don’t pay attention, users will realize it. If you pay attention, users will not realize it and cannot live without it! Isn’t this the thinking of Internet products?

Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above introduces Java cluster optimization - Nginx+tomcat cluster configuration - practice, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn