您的位置:首页 > 运维架构 > 反向代理

Nginx实现反向代理

2020-06-29 04:26 671 查看

1.需求

客户端发送a.com和b.com两个不同的请求,经过nginx反向代理服务器分发到具体的tomcat服务器,由具体的tomcat服务器提供服务,如下图

2.环境准备

nginx作为反向代理服务器,tomcat作为目标服务器,Linux下需安装nginx和tomcat。tomcat实例需要两份,在linux下先安装一份tomcat,再复制一份,为了区分,需要修改复制的tomcat配置文件,凡是涉及到端口的如80开头的都改一下,一台tomcat端口设为8070,另一台设为8080,然后启动这两个tomcat测试配置是否成功。为了便于测试,我们在tomcat的webapps下创建两个页面。

1.分别tomcat8070和tomc8080目录下创建myweb文件夹

[code]mkdir -p ./tomcat8070/webapps/myweb
mkdir -p ./tomcat8080/webapps/myweb

2.index.html文件

[code]touch ./tomcat8070/webapps/myweb/index.html
touch ./tomcat8080/webapps/myweb/index.html

3.编辑index.html内容 ,tomcat8070的index.html内容为

[code]<html>
<body>
<h1>Welcome to 8070</h1>
<body>
</html>

 tomcat8080的index.html内容为

[code]<html>
<body>
<h1>Welcome to 8080</h1>
<body>
</html>

4.启动两个tomcat

[code]./tomcat8070/bin/catalina.sh start
[code]​./tomcat8080/bin/catalina.sh start

3.nginx反向代理配置

1.修改nginx配置文件(/usr/local/nginx/conf/nginx.conf),添加两个server结点。

proxy_pass:配置的是目标服务器的地址

[code]server{
listen 80;
server_name a.com;
location / {
proxy_pass http://192.168.2.10:8070;
index index.html index.htm;
}
}
server{
listen 80;
server_name b.com;
location / {
proxy_pass http://192.168.2.10:8080;
index index.html index.htm;
}
}

2.启动nginx

[code]cd /usr/local/nginx/sbin/
./nginx

4.测试

客户端通过a.com/myweb和b.com/myweb访问,分别会显示“Welcome to 8070”和“Welcome to 8080”。

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: