revproxy

variables

$host vs $http_host vs $server_name

From: What is the difference between Nginx variables $host, $http_host, and $server_name?

You should almost always use $host, as it's the only one guaranteed to have something sensible regardless of how the user-agent behaves, unless you specifically need the semantics of one of the other variables.

The difference is explained in the nginx documentation:

  • $host contains “in this order of precedence: host name from the request line, or host name from the 'Host' request header field, or the server name matching a request”
  • $http_host contains the content of the HTTP “Host” header field, if it was present in the request
  • $server_name contains the server_name of the virtual host which processed the request, as it was defined in the nginx configuration. If a server contains multiple server_names, only the first one will be present in this variable.

app behind path

Search tags:

  • proxy_pass subfolder
  • path based routing

When hosted app requests assets from absolute URI like: /app.png, all requests are directed to root / location.

Analysis of sent headers shows that there is only one header preserving original subfolder: Referer:.

NginX: Serve application under sub path

    location /mqttx {
        return 302 /mqttx/;
    }

    location /mqttx/ {
        proxy_set_header        Host $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 $scheme;

        proxy_pass             http://mqttx-web/;
    }

    # mqttx-web loads assets:
    # - /app.js
    # - /app.png
    # - /favicon.ico
    # from absolute paths!
    # so it is impossible to host it under /mqttx/ without this trick:
    if ($http_referer ~ ^https://mqtt.grinndev.ovh/mqttx/) {
       rewrite ^ /mqttx$uri;
    }