windows下通过nginx实现tomcat集群负载均衡(入门)

一、目标

  1. Windows下,下载安装nginx
  2. Nginx常用命令
  3. Nginx负载均衡两个tomcat
  4. Nginx配置多个负载均衡服务

二、下载安装nginx

下载地址http://nginx.org/en/download.html

版本nginx-1.14.1

无需安装,解压后直接使用即可。

启动后,浏览器输入localhost, 验证nginx是否安装启动成功。

三、Nginx常用命令

1、启动(命令行):start nginx

2、关闭(命令行):nginx –s stop

3、重新加载配置:nginx –s reload

4、测试配置是否正确:nginx –t

5、注意

有时执行了关闭命令,nginx进程任然存在,需要在任务管理器中将nginx进程杀掉。否则会导致nginx的最新配置无法生效。

四、Nginx负载均衡两个tomcat

修改nginx-1.14.1\conf的nginx.conf

  1. 在#gzip  on;下方添加:

upstream www.hcbtest.com{

       server 127.0.0.1:8080 weight=2;       

server 127.0.0.1:9090 weight=3;

    }

  1. 修改location /,修改为:

location / {

            root   html;

            index  index.html index.htm;

           

            proxy_pass http://www.hcbtest.com; 

            proxy_redirect default; 

       }

  1. 修改server端口:

listen       10080;

 server_name  localhost;

  1. Tomcat调整:

保证tomcat的自带工程root是存在的;把两个tomcat的index.jsp内容进行修改,修改成不同的页面显示效果,用以体现负载均衡效果。

  1. 浏览器输入localhost:10080,多次刷新浏览器,可见有时呈现的是tomcat1的index.jsp页面,有时呈现的是tomcat2的index.jsp的页面。

 

五、Nginx配置多个负载均衡服务

只需基于上述的配置,继续添加如下配置即可:

1、

   upstream www.hcbtest2.com{

          server 127.0.0.1:8080 weight=3;

          server 127.0.0.1:9090 weight=2;

   }

2、 server {

        listen       10081;

        server_name  localhost;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

        location / {

            root   root;

            index  index.html index.htm index.jsp;

           

            proxy_pass http://www.hcbtest2.com/examples/; 

            proxy_redirect default; 

        }

3、保证tomcat自带的examples工程是存在的;

4、把两个tomcat的examples工程index.jsp内容进行修改,修改成不同的页面显示效果,用以体现负载均衡效果。

5、浏览器输入localhost:10081,多次刷新浏览器即可呈现不同的界面。

六、nginx完整配置文件


#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 www.hcbtest.com{
	       server 127.0.0.1:8080 weight=2;#默认为权重,访问7088和9080 端口的比例为1:1
	       server 127.0.0.1:9090 weight=3;
	}

	upstream www.hcbtest2.com{
	       server 127.0.0.1:8080 weight=3;#默认为权重,访问7088和9080 端口的比例为1:1
	       server 127.0.0.1:9090 weight=2;
	}
	
    server {
        listen       10080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
            
            proxy_pass http://www.hcbtest.com;  
            proxy_redirect default;  
        }

        #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;
        #}
    }

	server {
        listen       10081;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   root;
            index  index.html index.htm index.jsp;
            
            proxy_pass http://www.hcbtest2.com/examples/;  
            proxy_redirect default;  
        }

        #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;
    #    }
    #}

}

猜你喜欢

转载自blog.csdn.net/huangcangbai/article/details/83963330