Nginx——实现反向代理服务

一、新建springboot项目供nginx使用
@RestController
public class HelloWorldController {

@RequestMapping("/test1")
public String sayHello1() {
return "test1";
}
@RequestMapping("/test2")
public String sayHello2() {
return "test2";
}
}
请求接口如下:
二、配置nginx配置文件
1、本地nginx配置文件/usr/local/etc/nginx/
修改nginx.conf文件
最后一行修改为 include /usr/local/etc/nginx/vhost.d/*.conf;
加载vhost.d文件夹下所有conf文件,当多域名多项目时这样避免配置混乱

2、vhost.d下新增springboot.test.conf
配置如下:目前本地测试本代理服务器为本机一台

upstream spring-api { #配置后端服务器组
server 10.33.101.202:8081; #虚拟机地址+应用端口
}

server {
listen 80; #nginx监听端口
server_name www.wuxi.com; #定义代理域名
access_log /Users/wuxi/Documents/nginxlogs/spring-api.log; #日志地址
error_log /Users/wuxi/Documents/nginxlogs/spring-api-error.log; #错误日志地址

location / { #跳转监听路径
proxy_set_header Host $host; #真实客户端ip传给后端服务器
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://spring-api; #使用upstream定义的服务器组
}

}

启动nginx:sudo nginx -c /usr/local/etc/nginx/nginx.conf

本机绑定host 10.33.101.202 www.wuxi.com
通过域名访问:nginx.conf打开日志
查看日志
在其他客户端访问查看日志:前面ip是真实客户端ip


猜你喜欢

转载自blog.csdn.net/wx19900503/article/details/80948893