简体   繁体   English

使用Nginx和Tomcat支持URL中的动态路径

[英]Support for Dynamic path in URL with Nginx and Tomcat

The use case I am trying to implement. 我正在尝试实现的用例。
Display different content based on the {{random_string}} in the URL path. 根据网址路径中的{{random_string}}显示不同的内容。
Users will see different content based on the {{random_string}} that the URL contains. 用户将根据URL包含的{{random_string}}看到不同的内容。

eg: 例如:

www.example.com/{{random_string}}/index.jsp

The URLS will look like these below. URLS如下所示。 ( They include random characters before the JSP) (它们在JSP之前包含随机字符)

www.example.com/xc/index.jsp www.example.com/xc/index.jsp
www.example.com/2b/index.jsp www.example.com/2b/index.jsp
www.example.com/43/index.jsp www.example.com/43/index.jsp

My question 我的问题

  1. How do I setup nginx and tomcat to be able to support the {{random_string}} in the URL without throwing 404? 如何设置nginx和tomcat以能够在URL中支持{{random_string}}而又不会抛出404?

My Current Environment/Setup (this works fine) 我当前的环境/设置(可以正常工作)

Nginx along with Tomcat. Nginx和Tomcat。 Requests that come to nginx are then redirected to tomcat to access ROOT.war e,g - www.example.com/index.jsp 然后,到达nginx的请求将重定向到tomcat以访问ROOT.war e -g-www.example.com/index.jsp

You shouldn't have to change anything in Nginx or Tomcat config. 您无需在Nginx或Tomcat配置中进行任何更改。 What you could do is to create a servlet that will intercept the requests and extract the {{random_string}} before forwarding to the JSP. 您可以做的是创建一个Servlet,该服务器将拦截请求并提取{{random_string}},然后转发给JSP。 Here are the basic steps: 以下是基本步骤:

1) Create a servlet with a URL pattern of /* so that all requests go to it. 1)创建一个URL模式为/*的Servlet,以便所有请求都可以到达它。

2) In the servlet's doGet() method, use request.getPathInfo() to retrieve the URL path and parse it to extract the {{random_string}}. 2)在Servlet的doGet()方法中,使用request.getPathInfo()检索URL路径并对其进行解析以提取{{random_string}}。

3) Use request.setAttribute() to set attributes for the data you want to display in the JSP page. 3)使用request.setAttribute()设置要在JSP页面中显示的数据的属性。

4) Forward the request to the JSP using a RequestDispatcher, eg: 4)使用RequestDispatcher将请求转发到JSP,例如:

RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");  
dispatcher.forward(request, response);  

5) In the JSP, use the request attributes that you have set in step 3 to display the content. 5)在JSP中,使用在步骤3中设置的请求属性来显示内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM