1

I am having some problems to configure a nginx reverse proxy with some url. My configuration is:

events {
      worker_connections 768;
}

http {
     include       mime.types;
     default_type  application/octet-stream;

     sendfile        on;
     keepalive_timeout  65;
     access_log /dev/stdout combined;
     error_log /dev/stdout warn;

  server {
    ssl_certificate        /opt/ssl/ca.crt;
    ssl_certificate_key    /opt/ssl/ca.key;
    listen 443 ssl;

location /api/v1/namespaces/mynamespace/services/prometheuslb/proxy/ {
          resolver 127.0.0.1 valid=30s;
          set $endpoint "http://prometheuslb.mynamespace.svc.skydns.local:9090/";
          proxy_pass $endpoint;
        }

        location /api/v1/namespaces/mynamespace/services/pushgatewaylb/proxy/ {
          resolver 127.0.0.1 valid=30s;
          set $endpoint "http://pushgatewaylb.mynamespace.svc.skydns.local:9091/";
          proxy_pass $endpoint;
        }

        location /api/v1/namespaces/mynamespace/services/cratedb/proxy/ {
          resolver 127.0.0.1 valid=30s;
          set $endpoint "http://cratedb.mynamespace.svc.skydns.local:4200/";
          proxy_pass $endpoint;
          proxy_http_version 1.1;
          proxy_set_header Connection "upgrade";
        }
}
}
  • For the Prometheus url I have an infinite loop so the page doesnt load.

  • For Pushgateway it redirects wrong, becuase it should look for styles in /api/v1/namespaces/mynamespace/services/pushgatewaylb/proxy/ but it dismisses that url part, for example it should look for styles in /api/v1/namespaces/mynamespace/services/pushgatewaylb/proxy/static/jquery-2.1.4.min.js

  • And for cratedb I receive this error: Resource interpreted as Stylesheet but transferred with MIME type text/html

I can modify the existing server block, but I cannot create a separate server block.

I tested pinging targets and they work ok, also if I dont use varibles they work ok, but I need to use variables with resolver becuase we need nginx start ok even though endpoints are not available.

How can I fix these errors? Thank you.

Edit1:

Thank you very much @Richard Smith for the response, I am not expert in nginx and help is very appreciated: The new config:

location ~ ^/api/v1/namespaces/mynamespace/services/prometheuslb/proxy(/.*)$ {
          resolver 127.0.0.1 valid=30s;
          set $endpoint "http://prometheuslb.mynamespace.svc.skydns.local:9090";
          proxy_pass $endpoint$1;
        }

        location ~ ^/api/v1/namespaces/mynamespace/services/pushgatewaylb/proxy(/.*)$ {
          resolver 127.0.0.1 valid=30s;
          set $endpoint "http://pushgatewaylb.mynamespace.svc.skydns.local:9091";
          proxy_pass $endpoint$1;
        }

        location ~ ^/api/v1/namespaces/mynamespace/services/cratedb/proxy(/.*)$ {
          resolver 127.0.0.1 valid=30s;
          set $endpoint "http://cratedb.mynamespace.svc.skydns.local:4200";
          proxy_pass $endpoint$1;
          proxy_http_version 1.1;
          proxy_set_header Connection "upgrade";
        }

fixed the cratedb style error and Prometheus infinite loop.

Now I have other errors in prometheus in inspect of browser says GET https://domain/api/v1/namespaces/clautagsfed/services/prometheuslb/proxy/api/v1/query?query=time()&_=1541522597396 400 (Bad Request) ,

so seems there is something missing in config yet (without variables it works ok).

And regarding Pushgateway the same problem as explained above that it should look style in /api/v1/namespaces/mynamespace/services/pushgatewaylb/proxy/static/jquery-2.1.4.min.js but it looks in jquery-2.1.4.min.js

Thanks

1 Answer 1

3

When using a variable the behaviour of the proxy_pass directive changes. Basically, if you specify a URI, the full URI needs to be specified. See this document for details.

  • Remove the trailing / from your $endpoint variable
  • Either translate the URI using a rewrite...break statement, or convert your locations to regular expressions and use a capture

For example:

location ~ ^/api/v1/namespaces/mynamespace/services/prometheuslb/proxy(/.*)$ {
    resolver 127.0.0.1 valid=30s;
    set $endpoint "http://prometheuslb.mynamespace.svc.skydns.local:9090";
    proxy_pass $endpoint$1$is_args$args;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you Richard Smith, that fixed a lot part of the problem I am not expert in nginx so that was very helpful. In another hand I still have other problems explained in the edit about.
I forgot to append the query string. Answer updated.
Thank you very much for the help! the prometheus is all fixed. The only still having problems is the pushgateway, I tried adding the $is_args$args as well in pushgateway, but it is still looking styles without /api/v1/namespaces/mynamespace/services/pushgatewaylb/proxy/ , any clue?
Does pushgateway work with proxy_pass http://pushgatewaylb.mynamespace.svc.skydns.local:9091/;?
I tried without variables and the same style error: location /api/v1/namespaces/clautagsfed/services/pushgatewaylb/proxy/ { proxy_pass pushgatewaylb.clautagsfed.svc.skydns.local:9091; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.