Welcome! Log In Create A New Profile

Advanced

Help with locations and regex

October 20, 2017 06:37PM
Hi,

I'm trying to serve up static files for some paths and proxy_pass for others and I can't figure it out.

=== Requirements ===

Location 1: the following paths should all load the requested file from /home/user/project/dist/ and fall back to /home/user/project/dist/index.html if the explicit filename was not found (ideally, without a redirect):

/admin
/admin/
/admin/favicon.ico
/admin/foo
/admin/foo/logo.png
etc. - basically, anything beginning with /admin/ or /admin itself (but not /admin123)

Location 2: the root path should proxy_pass to a process running on another port. This is separate from location 3 because it has different location settings (such as auth).

Location 3: all first-level paths off the root should proxy_pass to the process on the other port, for example:

/foo
/bar123
/foo/baz # this would NOT be proxy passed, it can just fall through and 404 or whatever

=== End of Requirements ===

So far, the closest I have come is a situation where location 2 and 3 are working, but location 1 only partially works. In my current configuration for location 1, it will accept anything beginning with /admin (including /admin123) and it will 301 redirect /admin to /admin/ (note the trailing slash). Here is that config:

server {
listen 80 default_server;
server_name my.domain;
root /home/user/project/dist;
index index.html;

# Location 1
location ^~ /admin {
alias /home/user/project/dist;
try_files $uri $uri/ /admin/index.html;
}

# Location 2
location = / {
# other stuff omitted for brevity
proxy_pass http://127.0.0.1:8080;
}

# Location 3
location ~ /([^\/]+)$ {
# other stuff omitted for brevity
proxy_pass http://127.0.0.1:8080;
}
}

I thought I should be able to use ^/admin(?:/.*)?$ for the location 1 match, but that causes /admin/ to fall through to the default nginx 404 while /admin and /admin/foo are caught by location 3. Using that new match and changing the location modifier from ^~ to ~ catches everything correctly, but then every location 1 request results in the following error:

rewrite or internal redirection cycle while internally redirecting to "/admin/index.html"

I assume that's because I shouldn't be using alias and/or the try_files directive is wrong when using that regex, but I haven't been able to find the magic combination.

Is there any saint out there who knows how to satisfy all 3 requirements?

Thanks,
Anonymous Coward
Subject Author Posted

Help with locations and regex

anon59682 October 20, 2017 06:37PM

Re: Help with locations and regex

Francis Daly October 21, 2017 05:08AM

Re: Help with locations and regex

Francis Daly October 21, 2017 05:22AM

Re: Help with locations and regex

anon59682 October 21, 2017 01:41PM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 257
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready