mikemaccana
mikemaccana

Reputation: 123610

Why is my nginx rewrite rule returning a 400?

I have a small issue I'm working around, where some static URLs are being created as, for example /_static//images/logo.svg rather than /images/logo.svg.

There's a proper fix coming out, but in the meantime I need to make a rewrite rule, so /_static//images/logo.svg becomes /images/logo.svg.

I've added a rewrite rule as follows:

location / {  
    # Fix a stupid bug 
    # where static URLs are '/_static//images/logo.svg'
    # rather than '/images/logo.svg' on Linux
    rewrite ^(/_static/)(.*) $2 break;

    proxy_pass http://127.0.0.1:3333;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

Requesting https://myapp.domain/_static//images/logo.svg however still fails - with a 400 error. I can see the equest in access.log, but there's nothing in the error.log.

How can I make the rewrite rule work?

Upvotes: 1

Views: 425

Answers (1)

Richard Smith
Richard Smith

Reputation: 49812

The rewrite and location directives process a normalised URI which includes the merging of consecutive /s into a single / (unless merge_slashes is turned off).

As all URIs in Nginx have a leading /, I suspect that the lack of one in the rewritten URI is the cause of the 400 response.

Change the regular expression to address these two issues, for example:

rewrite ^(/_static)(/.*) $2 break;

Upvotes: 1

Related Questions