nginx+tomcat 负载均衡配置

Posted jixu8

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx+tomcat 负载均衡配置相关的知识,希望对你有一定的参考价值。

一、下载nginx

http://nginx.org/en/download.html

二、配置tomcat

   2.1 tomcat01 修改三个端口,http端口为 8088,主页修改内容为:111111
   2.2 tomcat02 修改三个端口,http端口为 8089,主页修改内容为:22222

三、配置nginx

编辑 E:\nginx-1.16.1\conf>nginx.conf 文件:


#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 myservers    
        server 127.0.0.1:8088 weight=1; 
        server 127.0.0.1:8089 weight=1; 
     

    server 
        listen       80;
        server_name  127.0.0.1;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / 
            proxy_pass http://myservers;
            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;
    #    
    #

其中:、

    #集群
    upstream myservers    
        server 127.0.0.1:8088 weight=1; 
        server 127.0.0.1:8089 weight=1; 
     

是添加的,upstream 用来配置tomcat集群weight是权重weight权重 权重越大访问的概率越大,myservers 为名称,自己命名。在

        location / 
            proxy_pass http://myservers;
            proxy_redirect default;
        
中需要引用。注意 http://后面的名字需要与  upstream myServe 的一致。
        server 
        listen       80;
        server_name  127.0.0.1;

listen监听端口,server_name为域名或者IP。

四、启动

cmd 命令行启动
    E:\nginx-1.16.1>nginx.exe

五、关闭

 cmd 命令行关闭
    E:\nginx-1.16.1>taskkill /im nginx.exe /f

tasklist查看当前系统中的进程列表,然后针对你要杀的进程使用taskkill命令

根据服务名

taskkill /im nginx.exe /f

  /im 立即
  /f 强制结束进程

根据pid

taskkill /pid pid

注意,就算关闭了cmd命令行窗口,nginx也无法关闭,必须杀死进程。(用命令杀才可以,手动杀也不行)

以上是关于nginx+tomcat 负载均衡配置的主要内容,如果未能解决你的问题,请参考以下文章

nginx + tomcat配置负载均衡

Nginx配置Nginx的负载均衡

[tomcat] tomcat+nginx 负载均衡配置

Nginx和tomcat实现负载均衡

负载均衡 | Nginx+Tomcat 动静分离实现负载均衡

Nginx+Tomcat负载均衡集群