nginx反向代理java項目方式
nginx反向代理java項目
server
{
listen 80;
server_name ***.***.com;
location / {
proxy_pass http://127.0.0.1:8686;
}
#一鍵申請SSL證書驗證目錄相關(guān)設(shè)置
location ~ \.well-known{
allow all;
}
access_log /www/wwwlogs/***.***.com.log;
error_log /www/wwwlogs/***.***.com.error.log;
}很簡單,核心就是 proxy_pass
Java實現(xiàn)反向代理的坑
公司是做車輛物聯(lián)網(wǎng)相關(guān)業(yè)務(wù)的,需要一個網(wǎng)關(guān)系統(tǒng)對設(shè)備上傳的報文進(jìn)行解析。最近換了個一個新的開源網(wǎng)關(guān)系統(tǒng),里面集成了一些對設(shè)備操作的API接口。由于網(wǎng)關(guān)系統(tǒng)不能對外,所以需要另一個系統(tǒng)鑒權(quán)后將接口反向代理到網(wǎng)關(guān)的API接口,類似于Nginx的反向代理。
在網(wǎng)上找了半天,發(fā)現(xiàn) smiley-http-proxy-servlet可以實現(xiàn)類似功能且實現(xiàn)非常簡單,實現(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)訪問 http://127.0.0.1:8001/proxy/test 這個請求回被代理服務(wù)器轉(zhuǎn)發(fā)到 http://solrserver:8983/proxy/test 這樣我們就實現(xiàn)了一個請求的代理了。
當(dāng)時我喜滋滋的認(rèn)為大功告成開始測試時,發(fā)現(xiàn)當(dāng)時使用 POST 請求且攜帶的參數(shù)類型 x-www-form-urlencoded 會導(dǎo)致請求超時。后面在百度搜了半天也沒找到解決方法,于是我就想著去這個項目的GitHub主頁哪里看下有沒有大神遇到這個問題。
當(dāng)我用 x-www-form-urlencoded作為關(guān)鍵詞在issues里搜索時,真好發(fā)現(xiàn)有大佬也遇到了這個問題。
出現(xiàn)這個問題的原因很簡單就是SpringBoot中過濾器太多,導(dǎo)致 ResponseBody 被提前消費(fèi)了,無法返回到客戶端。
在這條 issues下有個大佬提出來解決方案,就是繼承 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ù)在使用過程中還有其他的坑還是會與大家分享。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
nginx幾種網(wǎng)頁重定向(rewirte)的配置方法詳解
這篇文章主要詳細(xì)介紹了nginx幾種網(wǎng)頁重定向(rewirte)的配置方法,文中通過代碼示例和圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-02-02
Nginx實現(xiàn)集群的負(fù)載均衡配置過程解析
這篇文章主要為大家詳細(xì)介紹了Nginx實現(xiàn)集群的負(fù)載均衡配置過程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02
nginx實現(xiàn)靜態(tài)文件的token認(rèn)證過程
這篇文章主要介紹了nginx實現(xiàn)靜態(tài)文件的token認(rèn)證過程,2024-06-06

