反向代理(Reverse Proxy)是Nginx最重要的功能之一,其重点在于客户端不必知道具体处理请求的原始服务器,通过访问反向代理服务器转发到具体的原始服务器处理。本篇博客用比较简单的方式来配置并验证Nginx提供的反向代理功能。
Tomcat是我们在学习Web时都曾经用到过的应用服务器,部署和配置都很方便,并且能动态部署放到Webapps文件夹下的静态文件。本博文将在两个Tomcat服务器部署静态资源,并通过在Nginx修改配置,使同一url能访问到不同服务器上的资源,体现反向代理的特点。
一、 准备 —— 部署两个tomcat应用服务器
首先,Tomcat是运行java环境中的,所以需要安装java运行环境;
-- 查看yum提供哪些可安装的java
yum list java*
-- 下载并安装
yum -y install java-1.8.0-openjdk.x86_64
-- 如果需要javac进行编译的话,还需要安装java-devel
yum -y install java-devel
-- 验证是否安装完成
java -version
然后,从官网下载Tomcat,并尝试运行起来两个tomcat应用服务器,一个监听的端口8080,另一个监听8081,tomcat监听端口的配置在conf/server.xml进行修改,默认监听的是8080端口;
# 下载tomcat
wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.0.16/bin/apache-tomcat-10.0.16.tar.gz
# 解压
tar -zxvf apache-tomcat-10.0.16.tar.gz
# 重命名tomcat目录
mv apache-tomcat-10.0.16 ./tomcatA
# 复制一份tomcat
cp -r tomcatA ./tomcatB/
# 进入tomcat目录,运行startup.sh启动tomcat服务
./bin/startup.sh
# 尝试访问
http://localhost:8080
修改tomcatB的监听端口,配置文件在conf/server.xml;
启动并尝试访问;
# 进入tomcat目录,运行startup.sh启动tomcat服务
.tomcatA/bin/startup.sh
.tomcatB/bin/startup.sh
# 尝试访问
http://localhost:8080
http://localhost:8081
下一步,将静态资源放在tomcat的webapps目录下,tomcat具有热部署的特点,将war等web应用包或静态资源放在webapps目录下,都能在不重启tomcat服务器的情况下运行起来。将内容不同但文件名同为test.html放到两个tomcat的webapps下的test目录。
<!-- test.html in server1 -->
<html>
<head>
<title>Good Title</title>
</head>
<body>Good</body>
</html>
<!-- test.html in server2 -->
<html>
<head>
<title>Bad Title</title>
</head>
<body>Bad</body>
</html>
此时,通过http://localhost:8080/test/test.html和http://localhost:8081/test/test.html可以分别访问到Good和Bad两个页面。
二、安装——安装Nginx
Nginx下载官网:http://nginx.org/en/download.html
# 下载Nginx
wget http://nginx.org/download/nginx-1.18.0.tar.gz
# 解压
tar -zxvf nginx-1.18.0.tar.gz
然后修改Nginx配置conf/nginx.conf,修改分为两部分:
- Part1. 将步骤1准备的两个tomcat应用服务器的地址配置在同一个upstream中
- Part2. 增加对/test/的路径处理,url匹配/test/的请求使用新增的upstream进行处理
具体配置如下(可通过"Part1"和"Part2"定位):
#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 on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
# Part1
upstream test-api {
server 192.168.255.129:8080 max_fails=0 fail_timeout=60 weight=4;
server 192.168.255.129:8081 max_fails=0 fail_timeout=60 weight=1;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
# Part2
location /test/ {
proxy_pass http://test-api/test/;
}
location / {
root html;
index index.html index.htm;
}
#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 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;
# }
#}
}
启动Nginx。
# 启动Nginx
./sbin/nginx
# 验证Nginx启动情况
ps -ef|grep nginx
Nginx默认监听80端口。我们尝试访问http://localhost/test/test.html,会发现客户端访问同一个url,却由不同的原始服务器进行处理,这就是Nginx反向代理的体现。
三、Nginx提供的负载均衡策略
细心的你可能发现到了每5次请求中,4次访问到8080端口,1次访问到8081端口,因为Part1还配置了Nginx提供的负载均衡策略。
1. 普通轮询
upstream test-api {
server 192.168.255.129:8080;
server 192.168.255.129:8081;
}
2. 比例加权轮询
upstream test-api {
server 192.168.255.129:8080 weight=4;
server 192.168.255.129:8081 weight=1;
}
3. 基于IP路由负载
upstream test-api {
server 192.168.255.129:8080;
server 192.168.255.129:8081;
ip_hash;
}
4. 备用
upstream test-api {
server 192.168.255.129:8080;
server 192.168.255.129:8081 backup;
}
以上就是本文的全部内容。
引用文献: