4

I'm trying to setup Nginx to forward requests to several backend services using proxy_pass.

Loading https://example.com/monit works, however the links within the page are to https://example.com/sshd instead of https://example.com/monit/sshd

I'm running monit 5.2.5

I've tried with and without the rewrite rule below.

Config files;

proxy.conf

location /monit {
#       rewrite /monit/(.*) /$1 break;
        proxy_pass        http://localhost:2812/;
        include proxy.inc;
}
.... more entries ....

sites-enabled/main

server {
    listen 443;

    server_name example.com;
    server_name_in_redirect off;

    include proxy.conf;

    ssl on;
}

proxy.inc

proxy_connect_timeout   59s;
proxy_send_timeout      600;
proxy_read_timeout      600;
proxy_buffer_size       64k;
proxy_buffers           16 32k;
proxy_pass_header       Set-Cookie;
proxy_redirect          off;
proxy_hide_header       Vary;

proxy_busy_buffers_size         64k;
proxy_temp_file_write_size      64k;

proxy_set_header        Accept-Encoding         '';
proxy_ignore_headers    Cache-Control           Expires;
proxy_set_header        Referer                 $http_referer;
proxy_set_header        Host                    $host;
proxy_set_header        Cookie                  $http_cookie;
proxy_set_header        X-Real-IP               $remote_addr;
proxy_set_header        X-Forwarded-Host        $host;
proxy_set_header        X-Forwarded-Server      $host;
proxy_set_header        X-Forwarded-For         $proxy_add_x_forwarded_for;
proxy_set_header        X-Forwarded-Ssl         on;
proxy_set_header        X-Forwarded-Proto       https;
Thermionix
  • 907
  • 2
  • 15
  • 28

2 Answers2

2

I've modified the rewrite rule, and ensure cleared cache, and this works;

location /monit {
        rewrite ^/monit/(.*) /$1 break;
        proxy_pass        http://localhost:2812/;
        include proxy.inc;
}
Thermionix
  • 907
  • 2
  • 15
  • 28
  • Although links like `/monit/sshd` will work, the links on the pages would stay unprefixed. So it's not a complete solution (they have similar code in monit docs now). I wonder if I can patch monit to have urls prefixed. – leemour Dec 14 '18 at 19:44
0

Sadly monit has no configuration that allows it to define the BASE_URL of its requests, which means it will always add its services directly under /, which breaks your /monit proxymatch. :( A workaround, but maybe ugly, is to name your monit services in a way that you can match THAT name in nginx.

Thats the only way you will be able to do it unless you somehow hack the monit source to change the URL to be http://localhost:2812/monit/ but I believe that is harder?

Another option might be if you can run nginx on a parallell port and just rewrite access against this port towards the monit URL/port, that way you can rewrite/proxypass the entire / instead of /monit which would make the links work. Depends on your public IP and available ports, but should do the trick?

Mattias Ahnberg
  • 4,039
  • 18
  • 19