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證書(shū)驗(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)反向代理的坑
公司是做車(chē)輛物聯(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接口,類(lèi)似于Nginx的反向代理。
在網(wǎng)上找了半天,發(fā)現(xiàn) smiley-http-proxy-servlet可以實(shí)現(xiàn)類(lèi)似功能且實(shí)現(xiàn)非常簡(jiǎn)單,實(shí)現(xiàn)步驟如下:
導(dǎo)入smiley-http-proxy-servletMaven依賴(lài)
<dependency>
<groupId>org.mitre.dsmiley.httpproxy</groupId>
<artifactId>smiley-http-proxy-servlet</artifactId>
<version>1.12.1</version>
</dependency>在配置文件中編寫(xiě)代理的前綴和目標(biāo)地址
proxy:
solr:
servlet_url: /proxy/*
target_url: http://solrserver:8983/proxy編寫(xiě)配置類(lèi)
@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)訪(fǎ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ù)類(lèi)型 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ú)法返回到客戶(hù)端。
在這條 issues下有個(gè)大佬提出來(lái)解決方案,就是繼承 ProxyServlet 并重寫(xiě) 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實(shí)現(xiàn)404頁(yè)面的配置方法的兩種方法
在使用Nginx作為Web服務(wù)器時(shí),配置404頁(yè)面是一項(xiàng)基本需求,本文介紹了兩種配置404頁(yè)面的方法,這兩種方法各有特點(diǎn),可以根據(jù)實(shí)際需求選擇適合的配置方式2024-09-09
安裝OpenResty(Nginx倉(cāng)庫(kù))
這篇文章主要介紹了安裝OpenResty(Nginx倉(cāng)庫(kù)),需要的朋友可以參考下2023-06-06
nginx幾種網(wǎng)頁(yè)重定向(rewirte)的配置方法詳解
這篇文章主要詳細(xì)介紹了nginx幾種網(wǎng)頁(yè)重定向(rewirte)的配置方法,文中通過(guò)代碼示例和圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-02-02
Nginx實(shí)現(xiàn)集群的負(fù)載均衡配置過(guò)程解析
這篇文章主要為大家詳細(xì)介紹了Nginx實(shí)現(xiàn)集群的負(fù)載均衡配置過(guò)程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
nginx實(shí)現(xiàn)靜態(tài)文件的token認(rèn)證過(guò)程
這篇文章主要介紹了nginx實(shí)現(xiàn)靜態(tài)文件的token認(rèn)證過(guò)程,2024-06-06
排查Openresty獲取不到Host請(qǐng)求頭解決過(guò)程詳解
這篇文章主要為大家介紹了排查Openresty獲取不到Host請(qǐng)求頭解決過(guò)程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11

