I am trying to configure nginx to proxy to a cadvisor docker instance running on host port 8000.

I have the following configuration:

upstream cadvisor {
server {
    location /cadvisor {
        rewrite ^([^.]*[^/])$ $1/ permanent;
        rewrite ^/cadvisor$ /cadvisor/ permanent;
        rewrite ^/cadvisor(/.*) $1 break;
        auth_basic "Restricted"; #For Basic Auth
        auth_basic_user_file /etc/nginx/.htpasswd; #For Basic Auth
        proxy_pass http://cadvisor;
        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 https;
        proxy_redirect ~^/(.*)/ /cadvisor/$1;

This works for all URLs except for the following (which is one of the links cadvisor generates) https://myserver.com/cadvisor/containers//docker/a11c7edcc131d92c6ec3476329e5a54c82d2c31ee3f5592ba3fcb9386cb4f06a

This is probably because the double slashes before 'docker' in the URL messes up css path. My problem would be solved if I could redirect these URLs to single slash version of the url (i.e. https://myserver.com/cadvisor/containers/docker/a11c7edcc131d92c6ec3476329e5a54c82d2c31ee3f5592ba3fcb9386cb4f06a)

Is there any way to redirect the URL to a single slash version?


The rewrite directive seems to operate on a normalised version of the URI which does not see sequences of double /. It is possible to correct your double / situation by operating on the $request_uri variable instead:

if ($request_uri ~ ^(.*)//(.*)$) { return 302 $1/$2; }

See this caution regarding the if directive.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.