Nginx+Tomcat负载均衡简单配置

本文章暂时不涉及SESSION管理,只是简单的负载均衡,很简单。

1.新建2个WEB工程,内容简单,只有首页面一个JSP,index.jsp.

 一个首页面显示11111:,另一个显示22222:

将工程到处成war包。

2.2个相同的TOMCAT,修改cong/server.xml里面的端口号,一个默认8080,一个是9080.

3.2个工程的war包分别放入TOMCAT.

4.启动TOMCAT,分别打开首页,证明TOMCAT正常运行.

我的工程名字是tre,访问路径分别是localhost:8080/tre/和localhost:9080/tre/

5.启动nginx,在浏览器输入localhost,会看到初始页面,证明nginx运行成功.

6.修改nginx配置文件:

关键点在于 cong/nginx.conf中server的配置:

upstream test中的test需要和location中的proxy_pass对应。

upstream中的server 只需要IP+端口号即可,工程的名字 放在location中去写,http://test/tre.

我有试过直接在upstream中挂上工程名,但是nginx无法正常启动.

upstream中的 weight为权重,在其他文章里面有讲怎么用。

默认是server 127.0.0.1:8080 ;后面是什么也不用追加的。

如果是多个节点

server 127.0.0.1:8080 ;

server 127.0.0.1:8090 ;

则需要进行访问划分,因为实际情况下,有的服务器性能强 就多分配些流量,有的性能弱 就少分配。

其中默认不写weight则是1:1,轮询负载,加上weight=xxx以后,则按比例去分配负载。

还有涉及session的时候,可以用ip_hash.

每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session不能跨服务器的问题。

这种只是简单的session解决方法,复杂的还需要用redis进行session管理。

还有fair关键字。

upstream test{
server 192.168.1.10:8080;
server 192.168.1.11:8080;
fair;
}

这样按后端服务器的响应时间来分配请求,响应时间短的优先分配。

当然还有其他关键字/方法来调整负载,我暂时只了解这么多。

这样,修改完以后,使用cmd命令重新启动nginx,   nginx -s reload

然后访问 localhost,因为nginx默认的端口是80,然后不停的刷新页面,你会看到页面上会交替显示不同tomcat下的工程,这就说名负载均衡配置成功了。

至于,session管理,放到后面研究。

Nginx+Tomcat负载均衡简单配置_第1张图片

Nginx+Tomcat负载均衡简单配置_第2张图片Nginx+Tomcat负载均衡简单配置_第3张图片



#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
	upstream	test{
			server	127.0.0.1:8080 weight=1;
			server	127.0.0.1:9080 weight=4;
		}
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            #root   html;
            #index  index.html index.htm;
			proxy_pass  http://test/tre/;
        }
		
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

你可能感兴趣的:(nginx)