Nginx+Tomcat负载均衡的配置

27 篇文章 0 订阅

这方面的技术主要还是围绕在配置nginx.conf文件上面。

一、首先,在这里http://nginx.org/en/download.html下载Nginx,解压到一个文件夹下。

二、配置安装路径到环境变量。

三、安装tomcat,此处略。

将安装好的nginx和tomcat都测试一遍。

nginx的默认端口号是80,可隐藏。tomcat的默认端口是8080,为了体现负载均衡,实现路由,可以把tomcat的文件夹复制多份,需要修改其中的server配置文件,把端口设置区别开来。

四、在nginx设置的站点文件夹(root 指向)下放置html静态页面,在tomcat下搁置放一个javaWeb程序,各自测试一遍。

五、重头戏:配置nginx.conf文件。重点关注两点

1.在http节点添加负载均衡的路由配置

#设定负载均衡的服务器列表  
    upstream mysvr {  
        #weigth参数表示权值,权值越高被分配到的几率越大  
        #根据ip计算将请求分配各那个后端tomcat,许多人误认为可以解决session问题,其实并不能。    
        #同一机器在多网情况下,路由切换,ip可能不同    
        server 192.168.1.101:8080 weight=1;  
        
    }  

在server一行即是我配置的一个tomcat服务器,有多个就按照格式添加,正确填写ip和端口,合理设置权重。

2.在监听80端口的server节点下设置location代理

#对本server"/"启用负载均衡  
        location / {
	    proxy_pass http://mysvr;  
            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 10m;  
            client_body_buffer_size 128k;  
            proxy_connect_timeout 90;  
            proxy_send_timeout 90;  
            proxy_read_timeout 90;  
            proxy_buffer_size 4k;  
            proxy_buffers 4 32k;  
            proxy_busy_buffers_size 64k;  
            proxy_temp_file_write_size 64k;
        }

完整的配置文件是这样的:

#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 mysvr {  
        #weigth参数表示权值,权值越高被分配到的几率越大  
        #根据ip计算将请求分配各那个后端tomcat,许多人误认为可以解决session问题,其实并不能。    
        #同一机器在多网情况下,路由切换,ip可能不同    
        server 192.168.1.101:8080 weight=1;  
        
    }  

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

	#对本server"/"启用负载均衡  
        location / {
	    root   F:\WebSite;
	    index  index.html index.htm;

	    proxy_pass http://mysvr;  
            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 10m;  
            client_body_buffer_size 128k;  
            proxy_connect_timeout 90;  
            proxy_send_timeout 90;  
            proxy_read_timeout 90;  
            proxy_buffer_size 4k;  
            proxy_buffers 4 32k;  
            proxy_busy_buffers_size 64k;  
            proxy_temp_file_write_size 64k;
        }

        #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   F:\WebSite;
        }

	location ~ \.(html|htm|js|css|png|gif)$ {  
	    root F:\WebSite;  
	}  

        # 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$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            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   F:\WebSite;
    #        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   F:\WebSite;
    #        index  index.html index.htm;
    #    }
    #}

}

其实要关注的就是http这个节点下面负载均衡和监听80端口的server节点下代理设置两个地方。另外我把root原来指向的站点文件夹html修改了。

测试:

1.输入192.168.1.101/index.html,服务器会根据静态页面,分发到nginx来解析;

2.输入192.168.1.101/apidemo/user/getUserInfo,这是一个java的SpringBoot测试接口,可以正确获得结果。这里是不需要输入端口号的,如同就在同一个站点内一样。


我尝试把php解析也加进去,不过好像比较有难度。以后再慢慢实现。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值