Forum: NGINX How to combine try_files with multiple proxy destinations

2974d09ac2541e892966b762aad84943?d=identicon&s=25 rrrrcccc (Guest)
on 2014-01-08 10:23
(Received via mailing list)
I have the folllowing requirement:
1. if /usr/share/nginx/html/maintenance.html exists, then always show
this
file to browser.
2. if this is the static file which located in the sub-directories of
/usr/share/nginx/html/, then show this static file to browser.
3. if the URI begins with /testapp1/, then proxy to
http://127.0.0.1:8080;
else proxy to http://127.0.0.1:8081

And I tried the following two configurations but both failed:
1. Config1
server {
  ...
  root   /usr/share/nginx/html/;
  location / {
    try_files /maintenance.html $uri @proxy;
  }

  location @proxy {
    location /testapp/ {
      proxy_pass http://127.0.0.1:8080;
    }

    location / {
      proxy_pass http://127.0.0.1:8081
    }
  }
}

2. Config2
server {
  ...
  root   /usr/share/nginx/html/;
  try_files /maintenance.html $uri @proxy;

  location /testapp/ {
    proxy_pass http://127.0.0.1:8080;
  }

  location / {
    proxy_pass http://127.0.0.1:8081
  }
}

For Config1, it throws the following error:
nginx: [emerg] location "/testapp/" cannot be inside the named location
"@proxy" in /etc/nginx/nginx.conf:41

For Config2, it does not work as expected: it seems that when try_files
is
together with location directives, try_file has the lowerest priority
and so
I can never get /maintenance.html shown.

So can anybody know how to config to meet my requirement ? Thanks in
advance!

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,246125,246125#msg-246125
36a8284995fa0fb82e6aa2bede32adac?d=identicon&s=25 Francis Daly (Guest)
on 2014-01-09 22:45
(Received via mailing list)
On Wed, Jan 08, 2014 at 04:22:40AM -0500, rrrrcccc wrote:

Hi there,

(This is all untested, so handle with care.)

> I have the folllowing requirement:
> 1. if /usr/share/nginx/html/maintenance.html exists, then always show this
> file to browser.

That is probably best done with an "if" and a "return 503" -- there are
a
few approaches you can take, with their own advantages and
disadvantages.

> 2. if this is the static file which located in the sub-directories of
> /usr/share/nginx/html/, then show this static file to browser.
> 3. if the URI begins with /testapp1/, then proxy to http://127.0.0.1:8080;
> else proxy to http://127.0.0.1:8081

You could use "error_page" for 404 here, following
http://ngnix.org/r/error_page.

But I'll suggest using try_files with a named location as fallback.

Either way, you'll probably want one prefix and one named location,
per set of urls.

Something like:

  location ^~ / {
    try_files $uri $uri/ @proxyslash;
  }
  location @proxyslash {
    proxy_pass http://127.0.0.1:8080;
  }
  location ^~ /testapp1/ {
    try_files $uri $uri/ @proxytestapp1;
  }
  location @proxytestapp1 {
    proxy_pass http://127.0.0.1:8081;
  }

  f
--
Francis Daly        francis@daoine.org
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.