由於這久完成了一個分佈式文件系統的內網外穿部署,使用fastdfs,該應用部署到8088端口,它的後臺管理系統部署在8089端口(同一個內網服務器),該後臺管理系統的服務要請求fastdfs服務,能夠直接請求(同一個服務器的資源),可是咱們如今只外穿了後臺管理系統(Springboot項目),不經過公網分配的IP只能訪問到後臺管理系統,要直接訪問fastdfs服務的接口的話必須鏈接服務器公網,可是這限制了咱們用戶的訪問(我沒辦法每次都去你的服務器的地方訪問,我想在其餘地方也能夠訪問)nginx
代理服務器:SockerServer監聽某個端口,根據http報文鏈接到指定服務器端口,進行數據請求spring
- HTTP代理api
http請求通過代理服務器,代理服務器只要負責轉發相應的http響應體就能夠了。springboot
- HTTPS代理服務器
https請求通過代理服務器,會發送一個CONNECT報文,用於和代理服務器創建隧道,若是代理服務器返回HTTP 200,則創建成功,後續代理服務器只要負責轉發數據就行,實際上SSL/TLS握手仍是發生在客戶端和真實服務器。分佈式
ProxyServlet
把指定請求代理到服務器8088端口 Spring boot 的主 Servlet 爲 SpringMVC的DispatcherServlet,其默認的url-pattern爲「/」,若是咱們想要爲某個url添加不一樣的調用(其餘服務器接口),則須要建立新的代理servlet,會使用到
ServletRegistrationBean
,建立一個新的ProxyServlet
來處理不一樣端口的監聽和數據的發送,並將它註冊到springboot管理的servletContext
中(設置指定服務器和端口,請求轉發的接口)url
<dependency>
<groupId>org.mitre.dsmiley.httpproxy</groupId>
<artifactId>smiley-http-proxy-servlet</artifactId>
<version>1.7</version>
</dependency>複製代碼
### 配置代理
#請求resource時代理轉發到端口8088項目中
proxy.test.servlet_url_one= /resource/*
proxy.test.target_url_one= https://localhost:8088複製代碼
@Component
@Data
public class ProxyFilterServlet {
@Value("${proxy.test.target_url_one}")
private String targetUrl;
@Value("${proxy.test.servlet_url_one}")
private String servletUrl;
}
複製代碼
@Configuration
public class ProxyServletConfig {
@Autowired
private ProxyFilterServlet proxyFilterServlet;
//多個代理servlet能夠配置多個bean
@Bean
public ServletRegistrationBean servletRegistrationBean(){
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new ProxyServlet(), proxyFilterServlet.getServletUrl());
//這個setName必需要設置,而且多個的時候,名字須要不同
servletRegistrationBean.setName("go_backend");
servletRegistrationBean.addInitParameter("targetUri", proxyFilterServlet.getTargetUrl());
servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, "false");
return servletRegistrationBean;
}
}複製代碼
經過servlet容器來創建與目標服務器鏈接,畢竟沒有nginx這樣的專業代理服務器強spa
把須要請求的外網的請求轉到同一服務器內網端口.net
server {
listen 80;
server_name 127.0.0.1;
location / {
proxy_pass http://127.0.0.1:3000;
}
location ~ /api/ {
proxy_pass http://172.30.1.123:8081;
}
}複製代碼
參考博文:代理
https://blog.csdn.net/wohaqiyi/article/details/81327512
https://juejin.im/entry/58e8b6f344d904006d43360f