Nested location, proxy, alias and multiple "/" locations

Ciao,

I’m setting up several isolated Laravel apps in sub-folders of a single site. Web root
folder
of Laravel is called “public”, and I want to access such installation by
URI
“/app1/”. There are static files, maybe few custom php, and a single
entry
point /index.php.

So I came up with a config like this:

location    ^~  /app1  {
    root    /var/www/apps.mydomain.com/Laravel_app1/public;
    rewrite ^/app1/?(.*)$   /$1  break;

    location ~* \.(jpg|gif|png)$ {
        try_files $uri =404;
        ...
    }

    location ~* !(\.(jpg|gif|png))$ {
        proxy_pass              http://127.0.0.1:8081;
        ...
    }
}

Two questions:

  1. what happens to an “alias” inside a “^~” location like “location ^~
    /app1
    { … }” – seems like $uri is not changed and “/abcdef” part remains in
    place.

  2. how can I write a nested default “/” location after a rewrite and a
    regexp location? Got [emerg] errors when trying to write it like this:
    location ^~ /app1 {
    rewrite ^/app1/?(.)$ /$1 break;
    location ~
    .(jpg|gif|png)$ { …static files instructions… }
    location / { proxy_pass …php files and folders go to
    Laravel… }
    }

Serge.

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,246584,246584#msg-246584

On Sat, Jan 18, 2014 at 03:58:22PM -0500, sergiks wrote:

Hi there,

Web root folder
of Laravel is called “public”, and I want to access such installation by URI
“/app1/”. There are static files, maybe few custom php, and a single entry
point /index.php.

I’m not sure what you’re trying do to.

Maybe your question is clear to other people, in which case, perhaps
they’ll answer.

If not, could you try again giving examples of “this url should result
in
nginx serving this file”, and “this url should result in nginx proxying
to this other url”, and whatever else you want nginx to do?

  1. what happens to an “alias” inside a “^~” location like “location ^~ /app1
    { … }” – seems like $uri is not changed and “/abcdef” part remains in
    place.

It should do what the docs say – http://nginx.org/r/alias

Can you describe what you see, and how it differs from what you
expect? (And show the config you use – there’s no “alias” in the sample
provided).

(Note that there are some bugs relating to “alias” and “try_files”.)

  1. how can I write a nested default “/” location after a rewrite and a
    regexp location? Got [emerg] errors when trying to write it like this:
    location ^~ /app1 {
    rewrite ^/app1/?(.)$ /$1 break;
    location ~
    .(jpg|gif|png)$ { …static files instructions… }
    location / { proxy_pass …php files and folders go to Laravel… }
    }

I’m not sure that that combination is possible.

Are you trying to do something different from what

proxy_pass http://127.0.0.1:8081/;

would do?

Cheers,

f

Francis D. [email protected]

I’m not sure what you’re trying do to.
My bad, I’ll explain it in other way.

There’s a web root /var/www/site/ that responds to http://www.site.com
Then there’s a Laravel (front controller php framework) installation in
/var/www/Laravel1, and its web root folder is in
/var/www/Laravel1/public/
(index.php and static files are there)
I want to let that Laravel app to respond to URIs under /app1/:
http://www.site.com/app1/
http://www.site.com/app1/index.php
(same - but I had problems that the file was downloading as plaintext in
my
experiments)
http://www.site.com/app1/api/method (pretty urls)
http://www.site.com/app1/css/bootstram.min.css (static files)

There will be more that one such app sitting under various subfolders of
a
single web site.

  1. what happens to an “alias” inside a “^~” location like
    “location ^~ /app1 { … }”
    – seems like $uri is not changed
    and “/abcdef” part remains in place.
    It should do what the docs say – http://nginx.org/r/alias

The docs only says about a simple “location /i/” case
and a regexp case.
My q is “location ^~ /i/” which seems to skip the replacement as in the
simple case:
location ^~ /app1/ {
alias /var/www/Laravel/public/;
proxy_pass http://127.0.0.1:8081;
This example passes unchanged “/app1/api/method” to the proxy, instead
of
“/api/method”

Re. #2 — figured that out, thanks.

Sergei Sokolov.

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,246584,246589#msg-246589

On Sun, Jan 19, 2014 at 04:57:27AM -0500, sergiks wrote:

Hi there,

I’ll describe what I think you want you want nginx to do. Please correct
me where I’ve guessed wrongly.

There’s a web root /var/www/site/ that responds to http://www.site.com
Then there’s a Laravel (front controller php framework) installation in
/var/www/Laravel1, and its web root folder is in /var/www/Laravel1/public/
(index.php and static files are there)

I want to let that Laravel app to respond to URIs under /app1/:

A request for /app1/one.png should return the file
/var/www/Laravel1/public/one.png or respond 404.

A similar mapping applies for every request that end in .png, .jpg,
and .gif, case-insensitively.

A request for /app1/two.txt should proxy_pass to
http://127.0.0.1:8081/two.txt and return whatever it returns.

A similar thing happens for every other request.

http://127.0.0.1:8081/ is a separate web server which is running “real”
Laravel.

So, for the following requests, nginx should:

http://www.site.com/app1/

proxy to http://127.0.0.1:8081/

http://www.site.com/app1/index.php

proxy to http://127.0.0.1:8081/index.php

http://www.site.com/app1/api/method (pretty urls)

proxy to http://127.0.0.1:8081/api/method

http://www.site.com/app1/css/bootstram.min.css (static files)

proxy to http://127.0.0.1:8081/css/bootstram.min.css

The docs only says about a simple “location /i/” case
and a regexp case.

“^~” is a prefix location. The “non-regex” documentation applies.

My q is “location ^~ /i/” which seems to skip the replacement as in the
simple case:
location ^~ /app1/ {
alias /var/www/Laravel/public/;
proxy_pass http://127.0.0.1:8081;
This example passes unchanged “/app1/api/method” to the proxy, instead of
“/api/method”

That’s clear, thanks.

That is working as intended. Your expectation is wrong.

“alias” (along with “root”) does not affect “proxy_pass”.

Does the following do what you want?

===
location ^~ /app1/ {
alias /var/www/Laravel1/public/;
proxy_pass http://127.0.0.1:8081/;
location ~* .(jpg|gif|png)$ {}
}

(In general, unless the proxied server is careful, there are likely to
be problems trying to change parts of the url as is done above.)

f

Francis D. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs