日期:2014-05-16  浏览次数:20778 次

Linux下使用Tomcat7搭建高性能Java服务器

1. 架构说明: nginx + 4个tomcat

nginx作为前端代理,并且肩负负载均衡的作用,多个tomcat可以解决单台服务器高并发的性能问题,至于后端放几个tomcat要看你的服务器有多大内存,我的服务器是4核的CPU,8G内存。

?

2. nginx安装简单说明:

编译参数:

--prefix=/opt/mysql/ --enable-assembler --with-extra-charsets=complex \

--enable-thread-safe-client --with-big-tables --with-readline --with-ssl --with-embedded-server \

--enable-local-infile --with-plugins=innobase

可以根据自己的环境修改配置

?

nginx.conf配置:

?

user  www;
worker_processes	8;
error_log	/opt/logs/nginx/default/error/log;

pid        logs/nginx.pid;

worker_rlimit_nofile	51200; 

events {
	use	epoll;
	worker_connections	1024; #有的设置到65535
}

http {

    include	mime.types;
    default_type	application/octet-stream;

	server_names_hash_bucket_size	128;
	client_header_buffer_size		128k;
	large_client_header_buffers	8	128k;
	client_max_body_size	200m;
	client_body_buffer_size	128k;
	proxy_connect_timeout	600;
	proxy_read_timeout	600;
	proxy_send_timeout	600;
	proxy_buffer_size	16k;
	proxy_buffers	4	32k;
	proxy_busy_buffers_size	64k;
	log_format main '$remote_addr $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"';
	access_log	 /opt/logs/nginx/default/access/log	main;

	sendfile       on;
	tcp_nopush     on;
	tcp_nodelay	   on;

	keepalive_timeout	65;

	fastcgi_connect_timeout 300;
	fastcgi_send_timeout 300;
	fastcgi_read_timeout 300;
	fastcgi_buffer_size 64k;
	fastcgi_buffers 4 64k;
	fastcgi_busy_buffers_size 128k;
	fastcgi_temp_file_write_size 128k;

	gzip  on;
	gzip_min_length		1k;
	gzip_buffers		4 16k;
	gzip_comp_level		8;
	gzip_http_version	1.1;
	gzip_types		text/plain	application/xml;
	gzip_vary		on;

	proxy_temp_path	/opt/nginx_cache/proxy_temp_path;
	proxy_cache_path  /opt/nginx_cache/proxy_cache_path levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;

    upstream testservers { 
        server 127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=30s; 
        server 127.0.0.1:8081 weight=1 max_fails=2 fail_timeout=30s;
        server 127.0.0.1:8082 weight=1 max_fails=2 fail_timeout=30s;
        server 127.0.0.1:8083 weight=1 max_fails=2 fail_timeout=30s;
    }
 
    server {
	    listen	80;
	    server_name test.com;

        index index.html index.htm index.php index.jsp;

		charset	utf-8;

		location ~ .*\.(js|jpg|JPG|jpeg|JPEG|bmp|gif|GIF)$ {

			proxy_cache cache_one;
			proxy_cache_valid 200 304 1h;
			proxy_cache_valid 301 302 1m;
			proxy_cache_valid any 1m;
			proxy_cache_key $host$uri$is_args$args;

			proxy_pass          http://testervers;
			proxy_redirect      default;
			proxy_set_header    X-Forwarded-For            $proxy_add_x_forwarded_for;
			proxy_set_header    X-Real-IP $remote_addr;
			proxy_set_header    Host $http_host;
			proxy_set_header	Range $http_range;

			proxy_next_upstream http_502 http_504 error timeout invalid_header;
		}
		
		location / {

			proxy_pass          http://testservers;
			proxy_redirect      default;
			proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_set_header    X-Real-IP $remote_addr;
			proxy_set_header    Host $http_host;
            proxy_set_header    Range $http_range;

			proxy_next_upstream http_502 http_504 error timeout invalid_header;
		}
                # 对管理后台进行访问限制
        location ^~/manager/ {
            allow 你的IP;
            deny all;

            proxy_pass          http://testservers;
            proxy_redirect      default;
            proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header    X-Real-IP $remote_addr;
            proxy_set_header    Host $http_host;
            proxy_set_header    Range $http_range;

            proxy_next_upstream http_502 http_504 error timeout invalid_header;

        }

        }
    }

}

?

从上面配置可以看出,nginx会把ht