1、反向代理
需求:
两个tomcat服务通过nginx反向代理
nginx服务器:192.168.101.3
tomcat1服务器:192.168.101.5
tomcat2服务器:192.168.101.6
如下图:
1.1. 启动tomcat
tomcat使用apache-tomcat-7.0.57版本,在192.168.101.5和192.168.101.6虚拟机上启动tomcat。
1.2. nginx反向代理配置
根据上边的需求在nginx.conf文件中配置反向代理,如下:
#配置一个代理即tomcat1服务器 upstream tomcat_server1 {
server 192.168.101.5:8080; } #配置一个代理即tomcat2服务器 upstream tomcat_server2 { server 192.168.101.6:8080; }
#配置一个虚拟主机
server {
listen 80; #监听端口
server_name aaa.test.com; #对外提供的域名,如果对内部,就用内网IP,或localhost
location / {
#域名aaa.test.com的请求全部转发到tomcat_server1即tomcat1服务上
proxy_pass http://tomcat_server1;
#欢迎页面,按照从左到右的顺序查找页面
index index.jsp index.html index.htm;
}
}
server {
listen 80;
server_name bbb.test.com;
location / {
#域名bbb.test.com的请求全部转发到tomcat_server2即tomcat2服务上
proxy_pass http://tomcat_server2;
index index.jsp index.html index.htm;
}
}
注:如果你的应用没在根目录但在根目录的子目录,不想用户每次访问输入www.aa.com/yingyong ,就需用到重写.
#重写url
location =/ {
rewrite ^ /yingyong last; #如果你的应用没在根目录但在根目录的子目录,不想用户每次访问输入www.aa.com/yingyong ,就需用到重写.
} location /yingyong { #获取重写位置
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://yingyong_proxy; #把请求转向真实的后端服务
}
如果在同一个域名下有多台服务器提供服务,此时需要nginx负载均衡。
2. 负载均衡
需求
nginx作为负载均衡服务器,用户请求先到达nginx,再由nginx根据负载配置将请求转发至 tomcat服务器。
nginx负载均衡服务器:192.168.101.3
tomcat1服务器:192.168.101.5
tomcat2服务器:192.168.101.6
2.1. nginx实现负载均衡配置
根据上边的需求在nginx.conf文件中配置负载均衡,如下:
upstream tomcat_server_pool{ server 192.168.101.5:8080 weight=10; #weight默认为1,权重越高处理的请求越高 server 192.168.101.6:8080 weight=5; } server { listen 80; server_name aaa.test.com; location / { proxy_pass http://tomcat_server_pool; index index.jsp index.html index.htm; } }
注:
节点说明:
在http节点里添加:
#定义负载均衡设备的 Ip及设备状态
upstream myServer {
server 127.0.0.1:9090 down;
server 127.0.0.1:8080 weight=2;
server 127.0.0.1:6060;
server 127.0.0.1:7070 backup;
}
在需要使用负载的Server节点下添加
proxy_pass http://myServer;
upstream 每个设备的状态:
down 表示单前的server暂时不参与负载
weight 默认为1.weight越大,负载的权重就越大。
max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
fail_timeout:max_fails 次失败后,暂停的时间。
backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
附贴一个我目前使用的配置,方便以后查看。
#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; upstream tomcat_server {
server XXX.XX.XX.XX:8080;
} #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; server {
listen 80;
server_name laoyeye.net; #charset koi8-r; #access_log logs/host.access.log main; location / {
rewrite (.*) http://www.laoyeye.net;
} #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;
#}
}
server { listen 80; server_name www.laoyeye.net; location / { #域名www.laoyeye.net的请求全部转发到tomcat_server即tomcat服务上
proxy_pass http://tomcat_server;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-PORT $remote_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
index index.jsp index.html index.htm; } } # 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;
# }
#} }