HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Nginx /foo and /foo/ redirects

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
andredirectsnginxfoo

Problem

I want both http://domain/foo and http://domain/foo/ requests to serve a given resource.
Here's the relevant part:

location / {
       rewrite /(.*) /ipns/QmdpoFuwY/$1 break;
       proxy_pass http://127.0.0.1:8080;
}


with this config only:

curl -X GET http://domain.io/foo/


returns a resource while:

curl -X GET http://domain.io/foo


gives a Path Resolve error.

Solution

One could replace

rewrite /(.*) /ipns/QmdpoFuwY/$1 break;


with

rewrite ^(.*[^/]) /ipns/QmdpoFuwY/$1 break;


and try again.

The issue was solved by inspecting the logs. If for example one navigated to /foo/ instead of /foo/ the log indicated:

2017/10/15 14:51:28 [error] 7#7: *1 "/etc/nginx/html/index.html/foo.html" is not found (20: Not a directory)


It turned out that the regex did not match /foo/. One could also enable the rewrite logging to facilitate debugging:

rewrite_log on;

Code Snippets

rewrite /(.*) /ipns/QmdpoFuwY/$1 break;
rewrite ^(.*[^/]) /ipns/QmdpoFuwY/$1 break;
2017/10/15 14:51:28 [error] 7#7: *1 "/etc/nginx/html/index.html/foo.html" is not found (20: Not a directory)
rewrite_log on;

Context

StackExchange DevOps Q#2312, answer score: 1

Revisions (0)

No revisions yet.