nginx反向代理java項(xiàng)目方式
nginx反向代理java項(xiàng)目
server
{
listen 80;
server_name ***.***.com;
location / {
proxy_pass http://127.0.0.1:8686;
}
#一鍵申請(qǐng)SSL證書驗(yàn)證目錄相關(guān)設(shè)置
location ~ \.well-known{
allow all;
}
access_log /www/wwwlogs/***.***.com.log;
error_log /www/wwwlogs/***.***.com.error.log;
}很簡(jiǎn)單,核心就是 proxy_pass
Java實(shí)現(xiàn)反向代理的坑
公司是做車輛物聯(lián)網(wǎng)相關(guān)業(yè)務(wù)的,需要一個(gè)網(wǎng)關(guān)系統(tǒng)對(duì)設(shè)備上傳的報(bào)文進(jìn)行解析。最近換了個(gè)一個(gè)新的開(kāi)源網(wǎng)關(guān)系統(tǒng),里面集成了一些對(duì)設(shè)備操作的API接口。由于網(wǎng)關(guān)系統(tǒng)不能對(duì)外,所以需要另一個(gè)系統(tǒng)鑒權(quán)后將接口反向代理到網(wǎng)關(guān)的API接口,類似于Nginx的反向代理。
在網(wǎng)上找了半天,發(fā)現(xiàn) smiley-http-proxy-servlet可以實(shí)現(xiàn)類似功能且實(shí)現(xiàn)非常簡(jiǎn)單,實(shí)現(xiàn)步驟如下:
導(dǎo)入smiley-http-proxy-servletMaven依賴
<dependency>
<groupId>org.mitre.dsmiley.httpproxy</groupId>
<artifactId>smiley-http-proxy-servlet</artifactId>
<version>1.12.1</version>
</dependency>在配置文件中編寫代理的前綴和目標(biāo)地址
proxy:
solr:
servlet_url: /proxy/*
target_url: http://solrserver:8983/proxy編寫配置類
@Configuration
public class SolrProxyServletConfiguration implements EnvironmentAware {
@Bean
public ServletRegistrationBean servletRegistrationBean() {
Properties properties= (Properties) bindResult.get();
// 設(shè)置代理前綴
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new ProxyServlet(), properties.getProperty("servlet_url"));
// 設(shè)置代理目標(biāo)地址
servletRegistrationBean.addInitParameter(ProxyServlet.P_TARGET_URI, properties.getProperty("target_url"));
// 設(shè)置是否打印日志
servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, properties.getProperty("logging_enabled", "false"));
return servletRegistrationBean;
}
private BindResult bindResult;
@Override
public void setEnvironment(Environment environment) {
Iterable sources = ConfigurationPropertySources.get(environment);
Binder binder = new Binder(sources);
BindResult bindResult = binder.bind("proxy.solr", Properties.class);
this.bindResult = bindResult;
}
}假設(shè)當(dāng)前代理服務(wù)器地址為 127.0.0.1:8001,當(dāng)訪問(wèn) http://127.0.0.1:8001/proxy/test 這個(gè)請(qǐng)求回被代理服務(wù)器轉(zhuǎn)發(fā)到 http://solrserver:8983/proxy/test 這樣我們就實(shí)現(xiàn)了一個(gè)請(qǐng)求的代理了。
當(dāng)時(shí)我喜滋滋的認(rèn)為大功告成開(kāi)始測(cè)試時(shí),發(fā)現(xiàn)當(dāng)時(shí)使用 POST 請(qǐng)求且攜帶的參數(shù)類型 x-www-form-urlencoded 會(huì)導(dǎo)致請(qǐng)求超時(shí)。后面在百度搜了半天也沒(méi)找到解決方法,于是我就想著去這個(gè)項(xiàng)目的GitHub主頁(yè)哪里看下有沒(méi)有大神遇到這個(gè)問(wèn)題。
當(dāng)我用 x-www-form-urlencoded作為關(guān)鍵詞在issues里搜索時(shí),真好發(fā)現(xiàn)有大佬也遇到了這個(gè)問(wèn)題。
出現(xiàn)這個(gè)問(wèn)題的原因很簡(jiǎn)單就是SpringBoot中過(guò)濾器太多,導(dǎo)致 ResponseBody 被提前消費(fèi)了,無(wú)法返回到客戶端。
在這條 issues下有個(gè)大佬提出來(lái)解決方案,就是繼承 ProxyServlet 并重寫 newProxyRequestWithEntity方法,代碼如下:
protected HttpRequest newProxyRequestWithEntity(
String method, String proxyRequestUri, HttpServletRequest servletRequest) throws IOException {
HttpEntityEnclosingRequest eProxyRequest =
new BasicHttpEntityEnclosingRequest(method, proxyRequestUri);
String contentType = servletRequest.getContentType();
boolean isFormPost =
(contentType != null
&& contentType.contains("application/x-www-form-urlencoded")
&& "POST".equalsIgnoreCase(servletRequest.getMethod()));
if (isFormPost) {
List<NameValuePair> queryParams = Collections.emptyList();
String queryString = servletRequest.getQueryString();
if (queryString != null) {
queryParams = URLEncodedUtils.parse(queryString, Consts.UTF_8);
}
Map<String, String[]> form = servletRequest.getParameterMap();
List<NameValuePair> params = new ArrayList<>();
OUTER_LOOP:
for (Iterator<String> nameIterator = form.keySet().iterator(); nameIterator.hasNext(); ) {
String name = nameIterator.next();
// skip parameters from query string
for (NameValuePair queryParam : queryParams) {
if (name.equals(queryParam.getName())) {
continue OUTER_LOOP;
}
}
String[] value = form.get(name);
if (value.length != 1) {
throw new RuntimeException("expecting one value in post form");
}
params.add(new BasicNameValuePair(name, value[0]));
}
eProxyRequest.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
} else {
eProxyRequest.setEntity(
new InputStreamEntity(servletRequest.getInputStream(), getContentLength(servletRequest)));
}
return eProxyRequest;
}SolrProxyServletConfiguration也需要重新設(shè)置使用的 ProxyServlet
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new MyProxyServlet(), properties.getProperty("servlet_url"));這就是我在使用 smiley-http-proxy-servlet遇到的坑,后續(xù)在使用過(guò)程中還有其他的坑還是會(huì)與大家分享。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Nginx 如何部署指定文件夾下的項(xiàng)目(本地測(cè)試)
這篇文章主要介紹了Nginx 如何部署指定文件夾下的項(xiàng)目(本地測(cè)試),分為配置vue.config.js,指定生成環(huán)境的包,配置路由模式為hash(history模式刷新后,找不到頁(yè)面),本文講解的非常詳細(xì),需要的朋友可以參考下2024-01-01
Jenkins實(shí)現(xiàn)集群化管理以及流水線項(xiàng)目配置
這篇文章主要為大家介紹了Jenkins基本概念,配置實(shí)現(xiàn)集群化管理以及配置流水線項(xiàng)目的實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03
使用Kubernetes部署Springboot或Nginx的詳細(xì)教程
這篇文章主要介紹了用Kubernetes部署Springboot或Nginx的詳細(xì)教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
如何使用nginx充當(dāng)mysql的負(fù)載均衡器
這篇文章主要介紹了使用nginx充當(dāng)mysql的負(fù)載均衡器過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-06-06
Nginx?502?Bad?Gateway錯(cuò)誤解決詳細(xì)指南與實(shí)例
這篇文章主要介紹了Nginx?502?Bad?Gateway錯(cuò)誤解決的相關(guān)資料,502BadGateway錯(cuò)誤是Web開(kāi)發(fā)和運(yùn)維中常見(jiàn)的錯(cuò)誤,表示一個(gè)服務(wù)器在充當(dāng)網(wǎng)關(guān)或代理時(shí),從上游服務(wù)器收到了一個(gè)無(wú)效的響應(yīng),需要的朋友可以參考下2024-11-11
Nginx+PHP(FastCGI)搭建高并發(fā)WEB服務(wù)器(自動(dòng)安裝腳本)第二版
Nginx 0.7.x + PHP 5.2.10(FastCGI)搭建勝過(guò)Apache十倍的Web服務(wù)器(第5版) 編寫2011-04-04

