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;
}
}
}
实验结果