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:

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 – Module ngx_http_core_module

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/:
Custom Application Development Software for Business - Salesforce.com
Custom Application Development Software for Business - Salesforce.com
(same - but I had problems that the file was downloading as plaintext in
my
experiments)
Custom Application Development Software for Business - Salesforce.com (pretty urls)
Custom Application Development Software for Business - Salesforce.com (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 – Module ngx_http_core_module

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:

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:

Custom Application Development Software for Business - Salesforce.com

proxy to http://127.0.0.1:8081/

Custom Application Development Software for Business - Salesforce.com

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

Custom Application Development Software for Business - Salesforce.com (pretty urls)

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

Custom Application Development Software for Business - Salesforce.com (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]