天天看點

ngnix+tomcat簡單的負載均衡

參考部落格:http://blog.csdn.net/lsp1357/article/details/25806127

在window下:

1.下載下傳ngnix

http://nginx.org/en/download.html (最好下載下傳穩定版)

2.解壓,啟動nginx.exe

3.配置nginx.conf

#user  nobody;
worker_processes  ;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events {
    worker_connections  ;
}
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  ;
    gzip  on;
    upstream localhost{
    #根據ip計算将請求配置設定給哪個後端tomcat,許多人誤認為可以解決session問題,其實并不能
    #同一機器在多網情況下,路由切換,ip可能不同
    #ip_hash
    server ;
    server ;
    }
    server {
        listen       ;
        server_name  ;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            proxy_pass http://localhost;
            root   html;
            index  index.html index.htm;
        }    
        location ~ \.jsp$ {
            proxy_pass  http://localhost;#這裡的localhost要跟upstream後面跟的一樣
        }     
        #error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        #
        error_page        /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;
    #    }
    #}
}
           

4.ngnix -s reload

5.複制一份tomcat,命名為apache-tomcat-1、apache-tomcat-2,删除日志

修改它們的server.xml中的檔案

<Server port="18006" shutdown="SHUTDOWN">
  <Connector port="18081" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="18443" />
  <Connector port="8009" protocol="AJP/1.3" redirectPort="18443" />
  <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
           
<Server port="18007" shutdown="SHUTDOWN">
  <Connector port="18080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
  <Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
  <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">
           

修改一下webapps中的ROOT的index.jsp,随便加點東西即可,使通路apache-tomcat-1和apache-tomcat-2的index.jsp有差別就行,啟動它們

通路http://localhost

ngnix+tomcat簡單的負載均衡
ngnix+tomcat簡單的負載均衡

會在1、2之間來回切換