Configuring a Reverse Proxy
Introduction
A reverse proxy is a server that receives the requests for your Mendix app and forwards them to the Mendix Runtime. You can use a reverse proxy, for example, to do the following:
- Serve your app over HTTPS
- Host your app on a subpath of a domain
- Serve static content
- Restrict access to certain paths
A load balancer that forwards HTTP requests to the Mendix Runtime acts as a reverse proxy, so this document also applies to load balancers.
This document describes how the Mendix Runtime works with a reverse proxy, gives an example configuration, and links to the configuration for specific platforms. You can also use a reverse proxy when you run your app locally. For more information, see the Using a Reverse Proxy When You Run Your App Locally section below.
Headers the Mendix Runtime Uses
A reverse proxy can be configured to pass the following headers to the Mendix Runtime:
| Header | Description |
|---|---|
X-Forwarded-Proto |
The scheme users connect with, for example https. |
X-Forwarded-Scheme |
Used in conjunction with X-Forwarded-Proto—see below. |
X-Forwarded-Host |
The host name users connect to. It can include a port, for example app.example.com:8443. |
X-Forwarded-Port |
The port users connect to. The Runtime uses it only if X-Forwarded-Host does not include a port. |
X-Forwarded-Prefix |
The subpath the app is hosted on, if any, for example /my/sub/path. |
The Runtime uses these headers to determine the app URL and to decide on cookie security. It applies different rules for each.
App URL
The Runtime determines the app URL as follows:
- If
ApplicationRootUrlis set, the Runtime uses it and ignores the forwarded headers. - If
ApplicationRootUrlis not set, the Runtime builds the app URL from the forwarded headers. If bothX-Forwarded-ProtoandX-Forwarded-Schemeare set,X-Forwarded-Prototakes precedence. This only works if the proxy sendsX-Forwarded-Host. Without it, the Runtime ignores the forwarded headers and falls back to theHostheader and the scheme of the connection between the proxy and the Runtime.
Cookie Security
The Runtime only marks cookies as Secure (and adds the __Host- prefix and the SameSite attribute) if it considers the connection to be HTTPS. It decides this as follows:
- If either
X-Forwarded-ProtoorX-Forwarded-Schemeishttps, cookies are secure. - If not, and either header is
http, cookies are not secure. - If neither header is
httporhttps, the Runtime uses the scheme of theApplicationRootUrl.
This means that for cookie security, X-Forwarded-Proto and X-Forwarded-Scheme take precedence over ApplicationRootUrl.
Generating Public URLs
When a reverse proxy forwards requests to the Mendix Runtime, the Runtime only sees the internal address that the proxy forwards to. Most app content uses relative paths and works without knowing the public URL. However, some Runtime features need to generate full public URLs for your app, for example in published OData services.
To make sure the Mendix Runtime generates the correct public URLs, configure your reverse proxy to set the headers that match your public URL on every forwarded request.
In Mendix version 10 and above, the ApplicationRootUrl Runtime setting takes precedence over these headers. For more information, see the Multiple External Domains section in Runtime Customization.
Serving Your App over HTTPS
A reverse proxy can accept HTTPS connections from users and forward requests to the Mendix Runtime over HTTP. You can use any reverse proxy that accepts HTTPS connections using a certificate for your app's domain and forwards every request to the Mendix Runtime.
Making the Mendix Runtime Aware of HTTPS
When the reverse proxy forwards requests to the Mendix Runtime over HTTP, the Runtime is not aware that users access the app over HTTPS and does not set the Secure attribute on its cookies. To make the Runtime aware of HTTPS, use one of the following methods:
- Configure the ApplicationRootUrl Runtime setting with an
https://URL - Set the
X-Forwarded-ProtoorX-Forwarded-Schemeheader tohttpson every request that the reverse proxy forwards
The behavior of these two methods varies depending on your Mendix version:
- Mendix versions 10.17 and below – Setting ApplicationRootUrl to an
https://URL does not make the Runtime aware that the app is served over HTTPS. You must use theX-Forwarded-ProtoorX-Forwarded-Schemeheader instead. - Mendix versions 10.18 to 10.23 – Setting ApplicationRootUrl to an
http://URL takes precedence over theX-Forwarded-ProtoandX-Forwarded-Schemeheaders. - Mendix versions 10.24 and above – The
X-Forwarded-ProtoandX-Forwarded-Schemeheaders take precedence over ApplicationRootUrl. Mendix recommends usingApplicationRootUrlas the default approach and setting theX-Forwarded-ProtoorX-Forwarded-Schemeheader only if needed.
Example Configuration
The following example shows a configuration for nginx:
events {
worker_connections 1024;
}
http {
server {
listen 443 ssl;
server_name app.example.com;
ssl_certificate /path/to/certificate.pem;
ssl_certificate_key /path/to/private-key.pem;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection upgrade;
}
}
}In this example, nginx accepts HTTPS connections for app.example.com and forwards every request to the Mendix Runtime on 127.0.0.1:8080, adding the X-Forwarded-Proto header with the value https. To use this example, do the following:
- Replace
app.example.comwith the domain of your app - Replace
/path/to/certificate.pemand/path/to/private-key.pemwith the paths to the certificate and the private key for that domain - Replace
127.0.0.1:8080with the address and port of your Mendix Runtime
Other Considerations
Make sure that users cannot bypass the reverse proxy by connecting to the Mendix Runtime directly. For more information, see the Using a HTTP Reverse Proxy with SSL Support and Configuring Your Firewall sections in Security for Your On-Premises Installation.
Hosting Your App on a Subpath
If you host your app on a subpath, the reverse proxy needs to forward the request from https://subdomain.domain/my/sub/path to the internal address where the Mendix Runtime is running, and set the X-Forwarded-Prefix header. The following example shows a location block for nginx:
# Location block for the subpath "/my/sub/path".
location /my/sub/path/ {
# Make the Mendix runtime aware of https, see the section on serving your app over HTTPS for more information.
proxy_set_header X-Forwarded-Proto https;
# Forward the host to the Mendix Runtime.
proxy_set_header X-Forwarded-Host $host;
# Forward the prefix `/my/sub/path` to the Mendix Runtime.
proxy_set_header X-Forwarded-Prefix /my/sub/path;
# Optional: forward a port when not running on standard ports.
# proxy_set_header X-Forwarded-Port 3000;
# Required for Mendix DevTools to work.
proxy_http_version 1.1;
# Proxy the request to the Mendix Runtime.
proxy_pass http://mendix-runtime:8080/;
}The Mendix Runtime also needs to know the public URL of your app. For more information, see Generating Public URLs above.
Serving Static Content
Mendix recommends configuring the reverse proxy to serve static content directly, so that the Mendix Runtime only handles dynamic content. For more information, see the Letting the HTTP Reverse Proxy Serve Static Content section in Security for Your On-Premises Installation.
Restricting Access to Paths
You can use a reverse proxy to restrict access to certain paths of your app. For example, the Mendix Runtime has no setting to disable its documentation endpoints, such as /rest-doc. For more information, see the Restricting Access to Documentation Endpoints section in Request Handlers.
Platform-Specific Configuration
The following documents describe how to configure a reverse proxy on specific platforms:
- Reverse Proxy Inbound Rules in Microsoft Windows – Configure IIS as a reverse proxy
- Reverse Proxy in Reference Guide for Docker Deployment – Configure nginx or Traefik as a reverse proxy in Docker
- Ingress Controllers in Mendix on Kubernetes – Configure an ingress controller, which acts as a reverse proxy in Kubernetes
Using a Reverse Proxy When You Run Your App Locally
When you run your Mendix app locally, it is served over HTTP. You can put a reverse proxy on your own machine in front of the app, for example to set secure cookies or to run the app over HTTPS. Use the example configuration as a starting point, with localhost as the server_name and the port of your locally running app, for example 8080, in proxy_pass.
You can run nginx directly on your machine or in a Docker container. If you run nginx in a Docker container, 127.0.0.1 refers to the container, not to your machine. In that case, change the address in proxy_pass to one that reaches your machine from the container, such as host.docker.internal in Docker Desktop, and publish the port that nginx listens on.
Wherever you need the Mendix Runtime URL, use the URL of the reverse proxy instead of the URL of the locally running app.
Setting Secure Cookies
You need secure cookies, for example, when you embed the Mendix Client in a host application that runs on HTTPS. In that case, the Mendix Runtime has to set its cookies as Secure and SameSite=None.
To only set secure cookies, the reverse proxy does not have to accept HTTPS connections. It only has to set the X-Forwarded-Proto or X-Forwarded-Scheme header to https. The following example shows a configuration for nginx:
events {
worker_connections 1024;
}
http {
server {
listen 8081;
server_name localhost;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection upgrade;
}
}
}In this example, nginx listens on localhost:8081 and forwards every request to the Mendix app on localhost:8080, adding the X-Forwarded-Proto header. Use http://localhost:8081 as the Mendix Runtime URL. Because nginx is not configured for SSL in this example, https://localhost:8081 does not work; if you need an https:// URL, see Running Your App over HTTPS below.
Running Your App over HTTPS
You need HTTPS, for example, to test device features of a progressive web app (PWA) that are only available over HTTPS. For more information, see the Accessing Device Features section in Progressive Web App.
You can make your locally running app available on an https:// URL in the following ways:
- Use a reverse proxy on your own machine that accepts HTTPS connections
- Use an HTTPS tunnel that gives your app a public URL
Using a Reverse Proxy That Accepts HTTPS Connections
To use the example configuration on your own machine, you need a certificate and a private key for localhost. The following example uses OpenSSL to create a self-signed certificate that is valid for 365 days:
openssl req -x509 -newkey rsa:2048 -nodes -days 365 \
-keyout localhost-key.pem -out localhost.pem \
-subj "/CN=localhost" \
-addext "subjectAltName=DNS:localhost,IP:127.0.0.1"This command creates the certificate file localhost.pem and the private key file localhost-key.pem in the current folder. The certificate is valid only for localhost and 127.0.0.1, so you can use it only in a browser on the same machine. To open the app on another device, use an HTTPS tunnel.
In the example configuration, above, make the following changes:
- Change
listen 443 ssl;to a free port, such aslisten 8443 ssl; - Set
ssl_certificateandssl_certificate_keyto the paths oflocalhost.pemandlocalhost-key.pem
With these changes, nginx accepts HTTPS connections on localhost:8443 and forwards every request to the Mendix app on localhost:8080. Use https://localhost:8443 as the Mendix Runtime URL. If you run nginx in a Docker container, also make the certificate and the private key available in the container.
Using an HTTPS Tunnel
Instead of a reverse proxy on your own machine, you can use a tool like ngrok to expose your locally running app through a secure HTTPS tunnel. The tool gives your app a public https:// URL, so you do not need to create a certificate, and you can also open the app on other devices, such as a phone.
For example, the following ngrok command creates a tunnel to a Mendix app on localhost:8080:
ngrok http 8080ngrok shows the https:// URL that you can use to open your app.