Nginx + tomcat Load Balancing Cache Server Cluster

Source: Internet
Author: User
Tags failover md5 hash

Based on the company's needs and future visits, nginx is used as the Server Load balancer server and cache Server. The backend uses two tomcat servers to provide web services, and user information data is stored in oracle, audio files are stored on a separate application server. The following is the configuration of nginx and tomcat. For oracle installation and configuration, refer to the centos + oracle10g Installation File instructions in this blog. 1. Environment Description nginx and tomcat1 server address: 192.168.81.131 tomcat2 server address: 192.168.81.128 tomcat3 server address: 192.168.81.132 2. Nginx versions 0.7.48 and later support the Squid-like caching function. This cache uses the URL and related combinations as the Key and stores them on the hard disk after md5 hash. Therefore, it supports any URL link, it also supports non-404/301 status codes such as 302/200. Although the official Nginx Web Cache service can only set an expiration time for a specified URL or status code, unlike the Squid PURGE command, you can manually clear the specified cache page. However, A third-party Nginx module can clear the cache of a specified URL.
The Nginx Web Cache service consists of the proxy_cache instruction sets and fastcgi_cache instruction sets. The former is used to cache the back-end content source server for reverse proxy, the latter is mainly used to cache FastCGI dynamic programs. The two functions are basically the same.
The latest Nginx, proxy_cache, and fastcgi_cache are well-developed. In addition, the third-party ngx_cache_purge module is used to clear the cache of the specified URL. Squid can be completely replaced. We have used the proxy_cache cache function of Nginx in the production environment for more than two months, which is very stable and not inferior to Squid.
In terms of functions, Nginx already has the Web cache acceleration function of Squid and the function of clearing the specified URL cache. In terms of performance, Nginx's use of multi-core CPU is much better than Squid. In addition, Nginx is much more powerful than Squid in reverse proxy, Server Load balancer, health check, backend server failover, Rewrite, and ease of use. This allows an Nginx instance to be used as both the "Server Load balancer" and "Web Cache Server. (1) Compilation and installation of Nginx Server Load balancer and cache server in Linux: ulimit-SHn 65535 echo "ulimit-SHn 65535">/etc/rc. local ## apply the configuration for the next restart
Wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.00.tar.gz
Tar zxvf pcre-8.00.tar.gz
Cd pcre-8.00/
./Configure
Make & make install
Cd ../wget http://labs.frickle.com/files/ngx_cache_purge-1.0.tar.gz
Tar zxvf ngx_cache_purge-1.0.tar.gzwget http://nginx.org/download/nginx-0.8.32.tar.gz
Tar zxvf nginx-0.8.32.tar.gz
Cd nginx-0.8.32/
./Configure -- user = www -- group = www -- add-module = ../ngx_cache_purge-1.0 -- prefix =/usr/local/webserver/nginx -- with-http_stub_status_module -- with-http_ssl_module --
Make & make install
Cd .. /(2) Complete nginx configuration file: user www; worker_processes 10; error_log/usr/local/webserver/nginx/logs/nginx_error.log crit; pid/usr/local/webserver/nginx. pid; # maximum file descriptor
Worker_rlimit_nofile 51200; events
{
Use epoll; worker_connections 51200;
} Http
{
Include mime. types;
Default_type application/octet-stream; keepalive_timeout 60; tcp_nodelay on;
Sendfile on; server_names_hash_bucket_size 128;
Client_header_buffer_size 32 k;
Large_client_header_buffers 4 32 k;
Client_max_body_size 300 m;
Gzip on;
Gzip_min_length 1 k;
Gzip_buffers 4 16 k;
Gzip_http_version 1.1;
Gzip_comp_level 2;
Gzip_types text/plain application/x-javascript text/css application/xml;
Gzip_vary on; proxy_connect_timeout 5;
Proxy_read_timeout 60;
Proxy_send_timeout 5;
Proxy_buffer_size 16 k;
Proxy_buffers 4 64 k;
Proxy_busy_buffers_size 128 k;
Proxy_temp_file_write_size 128 k; upstream tomcat
{
Server 192.168.81.131: 8080;
Server 192.168.81.128: 8080; server 192.168.81.132: 8080;
}

# Note: The paths specified by proxy_temp_path and proxy_cache_path must be in the same partition.
Proxy_temp_path/data0/proxy_temp_dir;
# Set the name of the Web cache area to cache_one. The size of the memory cache space is 50 MB. The content not accessed within one day is automatically cleared. The size of the hard disk cache space is 10 GB.
Proxy_cache_path/data0/proxy_cache_dir levels = 1:2 keys_zone = cache_one: 50 m inactive = 1d max_size = 10g; server
{
Listen 80;
Server_name tomcat.hxqm.com;
# Automatic completion "/"
If (-d $ request_filename)
{
Rewrite ^/(. *) ([^/]) $ http: // $ host/$1 $2/last;
} # Implement static and dynamic separation of webpages
Location/
{Root/data0/htdocs/tomcat;
Index index.html index.htm; if (! -F $ request_filename)
{
Rewrite ^/([a-zA-Z]+).html/$ 1.jsp last;
Proxy_pass http: // tomcat;
Break;
}
} # Web Cache for website images, Flash, JavaScript, CSS, static HTML
Location ~ . * \. (Gif | jpg | jpeg | png | bmp | swf | js | css | html | shtml) $
{
# If the backend server returns errors such as 502, 504, and execution timeout, the request is automatically forwarded to another server in the upstream Server Load balancer pool for failover.
Proxy_next_upstream http_502 http_504 error timeout invalid_header;
Proxy_cache cache_one;
# Set different cache times for different HTTP Status Codes
Proxy_cache_valid 200 10 m;
Proxy_cache_valid 304 1 MB;
Proxy_cache_valid 301 302 1 h;
Proxy_cache_valid any 1 m;
# Combine domain names, Uris, and parameters into the Key values of the Web cache. Nginx hashes the cached content to the second-level cache directory based on the Key values.
Proxy_cache_key $ host $ uri $ is_args $ args;
Proxy_set_header Host $ host;
Proxy_set_header X-Forwarded-For $ remote_addr;
Proxy_pass http: // tomcat;
}
# Used to clear the cache. Assume that a URL is disabled.
Location ~ /Purge (/.*)
{
Allow 127.0.0.1;
Allow 192.168.81.0/24;
Deny all;
Proxy_cache_purge cache_one $ host $1 $ is_args $ args;
}
# Dynamic applications whose extensions end with. php,. jsp, And. cgi are not cached.
Location ~ . * \. (Php | jsp | cgi )? $
{
Proxy_set_header Host $ host;
Proxy_set_header X-Forwarded-For $ remote_addr;
Proxy_pass http: // tomcat;
} Location ~ . * \. (Gif | jpg | jpeg | png | bmp | swf) $
{
Expires 30d;
} Location ~ . * \. (Js | css )? $
{
Expires 1 h;
} Log_format tomcatlogs '$ remote_addr-$ remote_user [$ time_local] "$ request "'
'$ Status $ body_bytes_sent "$ http_referer "'
'"$ Http_user_agent" $ http_x_forwarded_for ';
Access_log/data1/logs/tomcatlogs. log tomcatlogs;
 
} (3) Start the nginx service and first test whether the nginx configuration file is correct, run the/usr/local/webserver/nginx/sbin/nginx-t test. If the following prompt is displayed, the nginx server can be started if the configuration file is correct: [root @ hxqm-tomcat local] #/usr/local/webserver/nginx/sbin/nginx-t
The configuration file/usr/local/webserver/nginx/conf/nginx. conf syntax is OK
Configuration file/usr/local/webserver/nginx/conf/nginx. conf test is successful if an error is reported during startup, you can view your configuration file based on the actual number of error lines. 3. Set the jdk file and tomcat file to the/usr/local/src directory, you can customize) (1), install jdk (I installed jdk with jdk-6u17-linux-i586.bin) cd/usr/local/src; chmod + x jdk-6u17-linux-i586.bin ;. /jdk-6u17-linux-i586.bin in the installation process of a few spaces, and then enter "yes", and then midway back press ENTER jdk installed. Make a soft connection: ln-s/usr/local/jdk1.6.0 _ 17 // usr/local/jdk (2), configure java environment variables vi/etc/profile, write the following content to the end of the file: JAVA_HOME = "/usr/local/jdk"
CLASS_PATH = "$ JAVA_HOME/lib: $ JAVA_HOME/jre/lib"
PATH = ".: $ PATH: $ JAVA_HOME/bin"
CATALINA_HOME = "/usr/local/tomcat"
Export JAVA_HOME CATALINA_HOME and save and exit. Execute source/etc/profile to make the environment variable that you just configured take effect or restart the machine. Select the method that suits you. (3) EDIT tomcat configuration file server. xml adds the following fields to the Hosts file: <Context path = "" docBase = "/data0/htdocs/tomcat/ROOT" debug = "0" reloadable = "true"/> explanation: tomcat goes back to the "appBase" directory by default to find the file to be accessed. If not, go to the "docBase" directory to find the file. This section can also be left blank, and you can freely. This is the configuration information of a tomcat server, which is the same as the other two. (4) Start the tomcat service/usr/local/tomcat/bin/startup. sh 4, add a record in the local hosts file: 192.168.81.131 tomcat.hxqm.com 5, Test Access http://tomcat.hxqm.com, if the page can be accessed normally, it means that nginx proxy and tomcat services are no problem. 6. test example of clearing the specified URL cache: If the prompt appears at the address you access, it means that your nginx can clear the specified url, if you see a 404 page error or other prompts, the reason may be that you have captured the cache. Please check your nginx configuration file.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.