初学Nginx(三)upstream初步体验

新建四个springboot工程
都添加
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.4.RELEASE</version>
</dependency>
</dependencies>
工程1  名称为8021  端口为8021
@SpringBootApplication
@RestController
public class Main8021
{
    public static void main(String[] args)
    {
        SpringApplication.run(Main8021.class, args);
    }


    @RequestMapping("/a")
    public String index()
    {
        return "8021";
    }
}


工程2  名称为8022  端口为8022、跟1一样、只是端口和名字改了


工程3  名称为8023  端口为8023  *注意@RequestMapping("/a")变成了@RequestMapping("/")
@SpringBootApplication
@RestController
public class Main8023
{


    public static void main(String[] args)
    {
        SpringApplication.run(Main8023.class, args);
    }


    @RequestMapping("/")
    public String index()
    {
        return "8023";
    }
}
工程4  名称为8024  端口为8024、跟3一样、只是端口和名字改了
nginx.conf
worker_processes  1;


events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;


    sendfile        on;
    keepalive_timeout  65;


upstream a {
server localhost:8021;
server localhost:8022;
}

upstream b {
server localhost:8023;
server localhost:8024;
}

    server {
        listen       80;
        server_name  localhost;
location /a/
{
proxy_pass http://a;
proxy_set_header X-Forwarded-For $remote_addr;
}
#处a之外的代理给b
location /
{
proxy_pass http://b;
proxy_set_header X-Forwarded-For $remote_addr;
}
    }
}
访问http://localhost/a/会分别负载到http://localhost:8021/a和http://localhost:8022/a上
访问http://localhost/会分别负载到http://localhost:8023/和http://localhost:8024/上


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值