springboot nginx简单实现反向代理

1.主机ip:192.168.200.108 8080端口启动服务
controller

@Controller
public class FilterController {
    @RequestMapping("/out")
    @ResponseBody
    public String out(){
        return "success";
    }
}

2.虚拟机ip:192.168.200.93 nginx端口做了映射8080:80

3.docker安装启动nginx可参考:
https://www.runoob.com/docker/docker-install-nginx.html

4.修改配置文件:启动nginx

docker exec -it 容器名/容器id /bin/bash 进入容器
nginx配置文件默认放在 etc/nginx/nginx.conf,默认include了etc/nginx/conf.d/default.conf,简单做路由的话修改default.conf即可
vim etc/nginx/conf.d/default.conf(一般需要先安装vim,执行apt-get update,然后apt-get install -y vim)


server {
    listen       80;#nginx的真实端口
    listen  [::]:80;
    server_name  localhost;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    location /proxy/ {
	proxy_pass http://192.168.200.108:8080/;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}



5.修改保存后,docker restart nginx容器id 重启nginx(docker ps 如果port没显示,一般为配置文件有问题)
访问 http://192.168.200.93:8080/proxy/out 依据新增的localtion,实际跳转到了http://192.168.200.108:8080/out 可参考:https://www.jianshu.com/p/b010c9302cd0

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值