Reputation: 46422
On the server (embedded Jetty), I need to redirect to a different port leaving everything else unchanged, e.g., redirect from
http://com.example.myserver:1234/whatever?with=params#and-hash?and=whoknowswhat
to
http://com.example.myserver:5678/whatever?with=params#and-hash?and=whoknowswhat
It look like I have to compose the resulting URL from things I don't really know:
Upvotes: 4
Views: 8012
Reputation: 16638
Adding more to Serge Ballesta explanation why you should use VirtualHost
. It always comes down to the responsibility of an application which will improve the efficiency of the whole system. I recommend reading What does architect know?
section in this link. The point that I want to highlight from that section is:
What components are in the system and what are their responsibilities
So in this question, the responsibly of redirecting the request should be with server and you don't want your application to deny all the request in case one server requires restart (in case you have cluster). Also you might consider changing the server, you might consider adding load balancer, in that case handling it on application side does not make any sense.
This file is for apache configuration for VirtualHost
. which redirect request based on context path, in this file /splunk
#LoadModule proxy_module modules/mod_proxy.so
#LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
#LoadModule proxy_http_module modules/mod_proxy_http.so
# Uncomment these to proxy FTP or HTTPS
#LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
#LoadModule proxy_connect_module modules/mod_proxy_connect.so
<VirtualHost *:80>
DocumentRoot "/var/www"
# Your domain name
ServerName Domain_NAME_HERE
ProxyPreserveHost On
ProxyPass "/splunk" "http://localhost:8000/splunk"
ProxyPassReverse "/splunk" "http://localhost:8000/splunk"
</VirtualHost>
So the request coming at port 80 with context path /splunk
will be redirected to http://localhost:8080/splunk
The above apache2 configuration will handle request coming on port 80 from DocumentRoot /var/www
[you might have PHP files in this location]
You might have to connect your application to apache in this case change Splunk splunk/etc/system/default/web.conf
configuration:
root_endpoint = /splunk
In case of tomcat:
<Connector port="<port_number>" enableLookups="false" redirectPort="<redirect_port>" protocol="AJP/1.3" />
Upvotes: 1
Reputation: 25872
Out-Of-The-Box Rewrite Handlers
http://wiki.eclipse.org/Jetty/Feature/Rewrite_Handler
I had a quick look at the rewrite handlers provided by Jetty out of the box. From what I can gather from the documentation/examples, they all appear to do the actual rewriting on only the path part of the URL (i.e. everything after the ports, not quite what we want) (please correct me if I'm wrong!).
Writing a Request Handler
A rudimentary example to get you started, if you want to only use jetty embedded, you could write a request handler that would redirect all requests to the given port.
The way it works is the PortRedirector handles HTTP requests using the handle method. It builds the original request URL, changes the port to the target "to" port, and redirects the client to the new URL.
In the following example, the server listens to port 1234, and redirects all requests to port 8080.
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class Redirector {
public static void main(String[] args) throws Exception {
Server server = new Server(1234);
server.setHandler(new PortRedirector(8080));
server.start();
server.dumpStdErr();
server.join();
}
static class PortRedirector extends AbstractHandler {
int to;
PortRedirector(int to) {
this.to = to;
}
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String uri = request.getScheme() + "://" +
request.getServerName() +
":" + to +
request.getRequestURI() +
(request.getQueryString() != null ? "?" + request.getQueryString() : "");
response.sendRedirect(uri);
}
}
}
References:
Upvotes: 6
Reputation: 148965
That kind or redirection if normally not the job of the application server (jetty, tomcat, glassfish, etc.), but of a reverse proxy you add on the chain before the servlet of JEE container. Among the most common, you find the excellent Apache HTTPserver, or nginx.
For exemple, with Apache httpd, you would use the mod_proxy module and a virtual host :
<VirtualHost *:1234>
ServerName com.example.myserver
RequestHeader set X-Forwarded-Proto "http"
ProxyPass / http://com.example.myserver:5678/
ProxyPassReverse / http://com.example.myserver:5678/
</VirtualHost>
Those kink of reverse proxies are currently used to secure application servers : only the proxy is accessible from the outside.
Upvotes: 2