Linux运维之(十二)nginx负载均衡实现tomcat动态静态页面分离.

nginx负载均衡实现tomcat动态静态页面分离.

实验步骤

Web节点配置:(IP:192.168.139.128 IP:192.168.139.126)

[root@localhost soft]# tar zxvf jdk-8u11-linux-x64.tar.gz
#解压文件
[root@localhost soft]# mv jdk1.8.0_11/ /usr/local/
#将文件移动到/usr/local
[root@localhost soft]# cd /usr/local/
[root@localhost local]# ll
#查看文件所有者所属组
[root@localhost local]# chown root.root jdk1.8.0_11/
#更改所有者和所属组
[root@localhost local]# ll

在这里插入图片描述

[root@localhost local]# cd jdk1.8.0_11/
[root@localhost jdk1.8.0_11]# vi /etc/profile

export JAVA_HOME=/etc/java/jdk1.8.0_11/      
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib
export PATH=$JAVA_HOME/bin:$PATH

在这里插入图片描述

[root@localhost jdk1.8.0_11]# source /etc/profile
#使/etc/profile的更改立即生效
[root@localhost jdk1.8.0_11]# cd ~/soft/
[root@localhost soft]# tar zxvf apache-tomcat-8.0.9.tar.gz
[root@localhost soft]# mv apache-tomcat-8.0.9 tomcat
#将文件改名为tomcat
[root@localhost soft]# mv tomcat/ /usr/local/
[root@localhost soft]# cd /usr/local/tomcat/bin/
[root@www bin]# ./startup.sh
#启动tomcat

负载均衡配置:

[root@google gohb]# cd /usr/local/nginx/conf/
[root@google conf]# vi nginx.conf

在这里插入图片描述
在这里插入图片描述

[root@google conf]# cd ..
[root@google nginx]# cd sbin/
[root@google sbin]# ./nginx -s stop
[root@google sbin]# ./nginx

小编的nginx.conf文件如下(含Linux网络配置之(十一)的配置)

user  nginx;
worker_processes  2;

#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 dvwaPool  {
    	server 192.168.139.128 weight=4 max_fails=3 fail_timeout=30s;
	server 192.168.139.127 weight=2 max_fails=3 fail_timeout=30s;
}
	upstream dvwaPools  {
	ip_hash;
    	server 192.168.139.128 weight=4 max_fails=3 fail_timeout=30s; 
    	server 192.168.139.127 weight=4 max_fails=3 fail_timeout=30s; 
}


upstream tomcatPools  {
        ip_hash;
        server 192.168.139.128:8080 weight=4 max_fails=3 fail_timeout=30s;
        server 192.168.139.126:8080 weight=4 max_fails=3 fail_timeout=30s;
}

    server {
        listen       80;
        server_name  www.bangniying.com.cn;
        #return 301 https://$server_name$request_uri;
	#charset koi8-r;

        #access_log  logs/host.access.log  main;
		#root   html;
       # index  index.html index.htm index.php;

        location / {
	   # location ~ \.php$ {
            proxy_pass   http://dvwaPool;
		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;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        proxy_max_temp_file_size 0;
        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   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  www.bangniying.com.cn;
        ssl_certificate      /usr/local/nginx/conf/3630279_www.bangniying.com.cn.pem;
        ssl_certificate_key  /usr/local/nginx/conf/3630279_www.bangniying.com.cn.key;
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
        location / {
	proxy_pass   http://dvwaPools;
            root   html;
            index  index.html index.htm index.php;
	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;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        proxy_max_temp_file_size 0;
        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;
        }
    }




#tomcat HTTPS server
    server {
        listen       8080 ;
        server_name  localhost;
	
	location /status {
        stub_status on;
        }

	location / {
            root   html;
            index  index.html index.htm;
        }

	location ~ \.(jsp|do)$ {
	#location / {
	proxy_pass   http://tomcatPools;
        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;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        proxy_max_temp_file_size 0;
        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;
        }
    }
}

实验结果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值