上篇文章已经安装好了Tomcat,一个端口为8080,另外一个端口为8081,但是我们线上使用的都是80端口,那么我们怎么办呢?又不能把Tomcat的端口都改成80!
所以接下来我们就要实现Nginx的反向代理了,由Nginx来决定用户访问的Tomcat是哪个?
6.1. 什么是反向代理
通常的代理服务器,只用于代理内部网络对Internet的连接请求,客户机必须指定代理服务器,并将本来要直接发送到Web服务器上的http请求发送到代理服务器中由代理服务器向Internet上的web服务器发起请求,最终达到客户机上网的目的。
而反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。
如何安装多个Tomcat在Linux机上:https://blog.csdn.net/yihuaiyan/article/details/88862327
首先分别启动我们的Tomcat
之后修改我们的Nginx的配置文件
vim nginx.conf
#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指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
#长连接超时时间,单位是秒
keepalive_timeout 65;
#启用Gizp压缩
#gzip on;
#服务器的集群
#服务器集群名字rj.nginx.com(可以自行命名!!!)
#upstream rj.nginx.com {
upstream tomcatService {
#服务器配置 weight是权重的意思,权重越大,分配的概率越大。
server 127.0.0.1:8080 weight=1;
server 127.0.0.1:8081 weight=2;
}
#当前的Nginx的配置
server {
#监听80端口,可以改成其他端口
listen 80;
# 当前服务的域名
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#location / {
# root html;
#index index.html index.htm;
#}
location / {
# http://tomcat服务器的集群名
#proxy_pass http://rj.nginx.com;
proxy_pass http://tomcatService;
#代理页面跳转路径,本身是默认!
#proxy_redirect default;
index index.jsp index.html index.htm;
}
#我把下面的注释掉了,因为tomcat首页加载不到图片和css
##location ~ \.(jpg|png|jpeg|bmp|gif|swf|css)$
##{
## expires 30d;
## root /nginx-1.8.0;#root:
## break;
##}
#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;
#}
}
#基于IP的虚拟主机配置(一个nginx控制两个ip访问不同的网站)
##server {
## listen 80;
## server_name 192.168.1.30;
#charset koi8-r;
#access_log logs/host.access.log main;
## location / {
## root html-30;
## index index.html index.htm;
## }
##}
##server {
## listen 80;
## server_name 192.168.1.31;
#charset koi8-r;
#access_log logs/host.access.log main;
## location / {
## root html-31;
## index index.html index.htm;
## }
##}
#基于端口进行虚拟机的配置(不同端口访问的内容不同!)
##server {
## listen 80;
## server_name 192.168.1.30;
#charset koi8-r;
#access_log logs/host.access.log main;
## location / {
## root html-30;
## index index.html index.htm;
## }
##}
##server {
## listen 81;
## server_name 192.168.1.30;
#charset koi8-r;
#access_log logs/host.access.log main;
## location / {
## root html-31;
## index index.html index.htm;
## }
##}
#基于域名进行虚拟机的配置(最实用,一个IP可以绑定多个域名!)
##server {
## listen 192.168.1.32:80;
## server_name huaiyan.test.com;
#charset koi8-r;
#access_log logs/host.access.log main;
## location / {
## root html01;
## index index.html index.htm;
## }
## }
## server {
## listen 192.168.1.32:8080;
## server_name huaiyan.test.cn;
#charset koi8-r;
#access_log logs/host.access.log main;
## location / {
## root html02;
## index 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;
# }
#}
}
节点说明:
在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机器。所以这台机器压力会最轻。