blacklabelops
blacklabelops

Reputation: 4958

Jenkins With Nginx Reverse Proxy And Resolver

I am running Jenkins inside Docker behind an Nginx reverse proxy. Now I got a problem with the resolver.

When I activate the resolver with:

set $backend "http://jenkins:8080/";
proxy_pass $backend;

I will get the following error for all javascript files:

Refused to execute script from 'http://localhost/static/....js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.

When I simply proxy pass without resolver:

proxy_pass http://jenkins:8080/;

It works but without resolver. The resolver is mandatory otherwise the setup won't work when the host jenkins changes it's Ip (Docker DNS Server).

My configuration:

resolver 127.0.0.11  ipv6=off valid=30s;
client_max_body_size  100m;
keepalive_timeout     65;
types_hash_max_size   2048;

server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  _;
    location / {
        set $backend "http://jenkins:8080/";
        proxy_pass $backend;
        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;
    }
}

Upvotes: 3

Views: 1260

Answers (1)

cnst
cnst

Reputation: 27238

Based on the error message you receive, it sounds like you're getting HTML pages in place of JavaScript.

Using a proxy_pass paradigm with a variable, you're telling nginx that it shouldn't mess with the value any further, e.g., regardless of the location and the URI, all requests to your backend will always be just as the variable says (e.g., with the same URI in your case).


The best option would probably be to use $uri, $is_args and $args, as per NGINX proxy_pass remove path prefix & resolve DNS:

-        set $backend "http://jenkins:8080/";
-        proxy_pass $backend;
+        proxy_pass http://jenkins:8080$uri$is_args$args;

Another option, which potentially could be less secure, is to use $uri_request, which has a slightly different meaning than the plain $uri in certain limited cases, as per Nginx pass_proxy subdirectory without url decoding:

proxy_pass http://jenkins:8080$request_uri;

Upvotes: 1

Related Questions