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