您的位置:首页 > 运维架构 > Tomcat

nginx+tomcat负载均衡

2015-05-29 00:48 489 查看
仅作记录以备后用。

1、修改nginx配置文件

nginx的配置文件在nginx安装目录的conf下,集群配置需要修改nginx.conf文件,linux上可以直接使用vi命令进行修改,下面是nginx.conf完整配置,也是经过优化的配置,可以直接拿过来就用的,重点是其中两个中文注释的地方:

user  www www;
worker_processes  auto;

error_log  /alidata/log/nginx/error.log crit;
pid        /alidata/server/nginx/logs/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;

events
{
  use epoll;
  worker_connections 65535;
}

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

		#charset  gb2312;

		server_names_hash_bucket_size 128;
		client_header_buffer_size 32k;
		large_client_header_buffers 4 32k;
		client_max_body_size 8m;

		sendfile on;
		tcp_nopush     on;

		keepalive_timeout 15;

		tcp_nodelay on;

		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_http_version 1.0;
		gzip_comp_level 2;
		gzip_types       text/plain application/x-javascript text/css application/xml;
		gzip_vary on;
		gzip_disable msie6;
		#limit_zone  crawler  $binary_remote_addr  10m;
		log_format '$remote_addr - $remote_user [$time_local] "$request" '
					  '$status $body_bytes_sent "$http_referer" '
					  '"$http_user_agent" "$http_x_forwarded_for"';
					  
		#在http模块中加入upstream模块
		upstream tomcat {
			server 127.0.0.1:18081 weight=1 max_fails=2 fail_timeout=30s;
			server 127.0.0.1:18082 weight=1 max_fails=2 fail_timeout=30s;
		}
		
		server {
				listen      80;
				server_name localhost;

				charset utf-8;

				root   /alidata/www/default;
				index  index index.html index.htm index.php;
				#在server模块中加入location /{}所有请求都被转发到upstream tomcat{}中配置的地址
				location / {
						#proxy config
						proxy_redirect          off;
						proxy_set_header        Host $host;
						proxy_set_header        X-Real-IP $remote_addr;
						proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
						client_max_body_size    256k;
						client_body_buffer_size 128k;
						proxy_connect_timeout   30;
						proxy_send_timeout      30;
						proxy_read_timeout      60;
						proxy_buffer_size       64k;
						proxy_buffers           4 64k;
						proxy_busy_buffers_size 64k;
						proxy_temp_file_write_size 64k;
						proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
						proxy_pass http://tomcat; 				}

                location ~ .*\.(php|php5)?$ {
						fastcgi_pass  127.0.0.1:9000;
						fastcgi_index index.php;
						include fastcgi.conf;
				}
				location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
				{
						expires 30d;
				}
				location ~ .*\.(js|css)?$
				{
						expires 1h;
				}
				access_log  /alidata/log/nginx/access/default.log;
		}

}


若是用vi命令修改完成后,按Esc后再输入:wq!即可保存退出。

进入到nginx安装目录的sbin目录下,需要重新nginx才能使修改的配置生效,重启命令:./nginx -s reload

2、配置tomcat

tomcat不需要安装,使用linux命令wget下载后,再使用tar -zxvf apache-tomcat-8.0.22.tar.gz进行解压后就可以用了。若一台服务器上启动多个tomcat则需要修改指定端口以免端口冲突,通过修改tomcat的conf文件夹中的server.xml配置,如下:

*关闭端口

<Server port="8005" shutdown="SHUTDOWN">
......

*tomcat线程池,这个默认是被注释的,打开这个注释

<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="500" minSpareThreads="20" maxIdleTime="30000"/>


......

*连接端口,这个默认是被打开的,注释掉

<!-- <Connector port="18081" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"/>
.....

*链接端口,这个默认是被注释掉的,打开它

<Connector executor="tomcatThreadPool"
			port="18081" protocol="HTTP/1.1"
			connectionTimeout="20000" 
			redirectPort="8443" 
			maxKeepAliveRequests="1" 
			acceptCount="100" 
			enableLookups="false" 
			maxHttpHeaderSize="8192" 
			URIEncoding="UTF-8" />


*AJP端口

<Connector port="8020" protocol="AJP/1.3" redirectPort="8443" />
再未开启SSL/TLS时,只要保证关闭端口、链接端口、AJP端口不冲突即可,若开始了SSL/TLS则还要保证https端口不能冲突

<!--
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" 
			   keystoreFile="/usr/local/www/tomcat8.0/keystore" 
			   keystorePass="123456"/>
	-->
tomcat默认没有开启https服务,若我们想使用需要先用jdk提供的工具生成keystore,再将如上配置打开即可。

以上tomcat配置也是经过优化的可以直接拿来使用。

最后进入到tomcat目录的bin目录下

*启动tomcat:./startup.sh | tail -f -n400 ../logs/catalina.out

*停止tomcat:./shutdown.sh

DONE!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: