负载均衡之nginx+tomcat简单demo配置

1、工具nginx 下载地址:http://nginx.org/en/download.html 

2、Tomcat 下载地址:http://tomcat.apache.org

将下载的tomcat 复制一份解压修改相关端口号:

tomcat路径下\conf\server.xml

第一个tomcat修改以下三个地方的端口号:

<Server port="8055" shutdown="SHUTDOWN">

 <Connector connectionTimeout="20000" port="8088" protocol="HTTP/1.1" redirectPort="8443"/>

 <Connector port="8090" protocol="AJP/1.3" redirectPort="8443" URIEncoding ="utf-8"/>

第二个tomcat可以不修改。

修改ngnix配置:

nginx-1.10.3\conf\nginx.conf

找到http{}

在其括号中添加如下代码:

upstream nginxdemo{
server 127.0.0.1:8088;
server 127.0.0.1:8082;
}

修改server{}中的listen 监听为8080

找到localhost  / {} 添加proxy_pass http://nginxdemo;

整体配置如下:



#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 nginxdemo{
server 127.0.0.1:8088;
server 127.0.0.1:8082;
}

    server {
        listen       8080;
        server_name  localhost;


        #charset koi8-r;


        #access_log  logs/host.access.log  main;


        location / {
            root   html;
            index  index.html index.htm;
proxy_pass http://nginxdemo;
        }


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


}


启动两个tomcat

启动nginx  命令 nginx -s  reload

在浏览器中输入nginxdemo:8080

此配置为轮询访问 即:平均访问两台服务器




猜你喜欢

转载自blog.csdn.net/baidu_36342103/article/details/69261682