Nginx的反向代理配置实例

Nginx的反向代理配置实例

实例1

实现效果:

浏览器访问 http://ip:80/,访问到配置在 8080 端口的tomact服务

准备工作:

  • tomact下 webapps目录下新建test文件夹,并在文件夹放置 test.html

  • 设置tomact启动端口为8080

  • 测试,浏览器输入:http://ip:8080/test/test.html 能访问到对应的html

配置Nginx流程

1.在Nginx的配置文件 nginx.conf 的 http 中增加内容

 # reverse proxy example-1
 server {
   listen       80;
   server_name  localhost;

   location / {
     root   html;
     proxy_pass   http://localhost:8080/test/test.html;
     index  index.html index.htm;
   }
 }

2.重启Nginx服务

cd 至 sbin 目录下,执行 ./nginx -s reload

3.到浏览器进行测试

在浏览器地址栏输入: http://ip:80/test/test.html 可访问到 http://ip:8080/test/test.html 对应的html

实例2:

实现效果:

浏览器访问 http://ip:9000/edu/test.html,访问到配置在 8081 端口的tomact服务

浏览器访问 http://ip:9000/gov/test.html,访问到配置在 8082 端口的tomact服务

准备工作:

  • 配置两个 tomact ,配置端口分别为:8081端口 和 8082 端口

  • 8081的tomact的webapp目录下,创建edu文件夹,放置test.html ,启动服务

  • 8082的tomact的webapp目录下,创建gov文件夹,放置test.html ,启动服务

  • http://ip:9000/edu/test.htmlhttp://ip:9000/gov/test.html 均能访问到对应的内容

配置Nginx流程

1.在Nginx的配置文件 nginx.conf 的 http 中增加内容

# reverse proxy example-2
server {
  listen       9000;
  server_name  localhost;

  location ~ /edu/ {
    proxy_pass   http://localhost:8081;
  }

  location ~ /gov/ {
    proxy_pass   http://localhost:8082;
  }
}

2.重启Nginx服务

cd 至 sbin 目录下,执行 ./nginx -s reload

3.到浏览器进行测试

在浏览器地址栏输入:

猜你喜欢

转载自blog.csdn.net/m0_37633306/article/details/109434795