nginx invalid URL prefix with rewrite

Viewed 63

I'm using docker and running nginx alongside varnish.

Because I'm running docker, I've set the resolver manually at the top of the nginx configuration (resolver 127.0.0.11 ipv6=off valid=10s;) so that changes to container IPs will be picked up without needing to restart nginx.

This is the relevant part of the config that's giving me trouble:

        location ~^/([a-zA-Z0-9/]+)$ {
            set $args ''; #clear out the entire query string
            set $card_name $1;
            set $card_name $card_name_lowercase;
            rewrite ^ /cards?card=$card_name break;
            proxy_set_header x-cache-key card-type-$card_name;
            proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $remote_addr;
                proxy_set_header Host $host;
                proxy_set_header REQUEST_URI $request_uri;
                proxy_http_version 1.1;
                set $backend "http://varnish:80";
                proxy_pass $backend;
                proxy_intercept_errors on;
                proxy_connect_timeout 60s;
                proxy_send_timeout   86400s;
                proxy_read_timeout   86400s;
                proxy_buffer_size 128k;
                proxy_buffers 4 256k;
                proxy_busy_buffers_size 256k;
                error_log /var/log/nginx/error.log;
                access_log /var/log/nginx/access.log;
            error_page 503 /maintenance.html;
        }

When I visit a URL for this, e.g. https://example.com/Test, I get 500 internal server error.

In the nginx error log, I see the following:

2022/04/27 23:59:45 [error] 53#53: *1 invalid URL prefix in "", client: 10.211.55.2, server: example.com, request: "GET /Test HTTP/2.0", host: "example.com"

I'm not sure what's causing this issue -- http:// is included in the backend, so it does have a proper prefix.

If I just use proxy_pass http://varnish:80, it works fine, but the backend needs to be a variable in order to force docker to use the resolver.

0 Answers
Related