This is a Canonical Question about reverse proxies, how they work and how they are configured.
How can I serve requests from a service on a different port or from a different server with the same webserver and distinguish it via the URL?
This is a Canonical Question about reverse proxies, how they work and how they are configured.
How can I serve requests from a service on a different port or from a different server with the same webserver and distinguish it via the URL?
If you want to serve serve content from multiple services or multiple servers from the same domain you can configure your webserver to act as a reverse proxy. The services (often called backend server or application server) do not necessarily need to be directly reachable from the client, it is common that only the webserver that actually serves the client is directly reachable. It is also possible to have different schemes or protocols (http, https, ajp, ...) between client and server and between server and backend.
Examples
The reverse proxy can be configured as virtual directories or as subdomains.
Example configuration for Apache
You need to load the necessary apache modules to be able to use it as a reverse proxy. These are at least:
For more protocols you will need to enable more modules.
You can enable the modules with Debian/Ubuntu based distributions like this:
With RedHat/CentOS based distributions you will need to find the proper configuration files in
/etc/httpd/
and enable the modules manually.A configuration for the examples above could look like this:
Example configuration for nginx
Backend configuration
One thing to keep in mind is that the backend server should be configured accordingly. If, for example, Jenkins is configured to serve requests with the URL
http://192.168.42.18:8080/
, it will use this base URL for paths to CSS, JS, and image files, resulting in a lot of 404 errors or timeouts and a non working server. It has to be configured with the proxy URLhttps://example.com/jenkins/
as a base URL to make it work.Other backends require a similar configuration. This is the preferred method of resolving issues with URLs pointing to the backend server instead of the frontend server. If for some reason it is not possible to change the configuration of the backend server, you can configure the frontend server to rewrite the HTML before serving it to the client.
However, this should be seen as a last resort method, as it adds unnecessary load to the frontend server.
Apache
With Apache you can use the module
mod_proxy_html
. This module provides the directiveProxyHTMLURLMap
, which you can use to rewrite the response.This should rewrite all occurences of the localserver URL in the answer with the publichostname URL.
nginx
With nginx a similar result can be achieved with the ngx_http_sub_module: