Nginx 1.7.0: location @php

Hi all,

I’m trying to understand why the use of this configuration code does not
work:
location ^~ /setup {
auth_basic “Restricted Access”;
auth_basic_user_file htpasswd;
try_files $uri $uri/
/setup/index.php?$uri&$args;
location ~ .php$ {
try_files @php =404;
}
}
location @php {
try_files $uri =404;
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass fastcgi;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED
$document_root$fastcgi_script_name;
include fastcgi.conf;
}

This works:
location ^~ /setup {
auth_basic “Restricted Access”;
auth_basic_user_file htpasswd;
try_files $uri $uri/
/setup/index.php?$uri&$args;
location ~ .php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass fastcgi;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED
$document_root$fastcgi_script_name;
include fastcgi.conf;
}
}

Thank you for your help.

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

On Sun, May 25, 2014 at 05:49:33AM -0400, TECK wrote:

Hi there,

I’m trying to understand why the use of this configuration code does not
work:

Answer #1: what does “does not work” mean?

What request do you make; what response do you get; what response do
you want?

try_files               @php =404;

Answer #2: what do you think that directive does? What does the
documentation say that that directive does? Hint: the final argument to
try_files is uri or =code. All of the other ones are file.

f

Francis D. [email protected]

Hi Francis,

Answer #1: what does “does not work” mean?
When I process an URI request, it downloads the file instead of
executing
the PHP code.

What I try to achieve is very simple, use @php as location to execute
PHP
code instead of repeating it over and over in various locations. Here it
is
a better example of a functional setup:
http://pastie.org/private/5gkl1pti1co0onzhgb8jpg

What I try to achieve is replace the location ~ .php$ location contents
with @php ones so I only execute try_files:
http://pastie.org/private/iuz58naozk6xo92ukqmwsg

Regards,

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

Francis,

I’m guessing that you may want something like
try_files i-dislike-macro-include @php;
What you posted is some deprecated configuration available on Google.

Perhaps the request that you made did not match the location blocks that
you showed?
If that would be the case, the proper code posted earlier would not
work, so
this is not the case. I’m very familiar with Nginx and I understand the
try_files concept very well. My original question was how to avoid
repeating
a segment of configuration code endless times, more exactly this:
try_files $uri =404;
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass fastcgi;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
include fastcgi.conf;

Using logic and the examples provided into Nginx documentation what I
posted
previously should work. Can you provide a solution to my question?

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

On Sat, May 31, 2014 at 01:59:18AM -0400, TECK wrote:

Hi there,

Answer #1: what does “does not work” mean?
When I process an URI request, it downloads the file instead of executing
the PHP code.

Perhaps the request that you made did not match the location blocks that
you showed?

That’s the way I can get the specific unwanted response that you
report here.

What I try to achieve is very simple, use @php as location to execute PHP
code instead of repeating it over and over in various locations. Here it is
a better example of a functional setup:
http://pastie.org/private/5gkl1pti1co0onzhgb8jpg

If it matters to the question, can you include the (minimal)
configuration
in the mail directly? That way, it will be available for the next person
who searches the archives.

Thanks.

I’m guessing that you may want something like

try_files i-dislike-macro-include @php;

Good luck with it,

f

Francis D. [email protected]

On Sat, May 31, 2014 at 06:31:09AM -0400, TECK wrote:

Hi there,

Perhaps the request that you made did not match the location blocks that
you showed?

If that would be the case, the proper code posted earlier would not work, so
this is not the case.

That’s a reasonable assumption to make, if I can reproduce your reported
problem.

When I use

location ^~ /setup {
try_files $uri $uri/ /setup/index.php?$uri&$args;
location ~ .php$ {
try_files @php =404;
}
}

and I request /setup/real.php, I get back 404, not the unparsed content
of /usr/local/nginx/html/setup/real.php or anything else.

When I request /setup/fake.php, I get back 404.

When I request /other/real.php, I get back the unparsed content of
/usr/local/nginx/html/other/real.php.

Because I have shown you the config, the request, and the response, you
(and anyone else) can repeat the tests. If you get a different response,
then there is something extra involved that should be investigated
before
the problem can be understood.

Perhaps different versions of nginx are involved. Perhaps different
modules are compiled in. Perhaps other configuration applies that was
not provided in the original example. After the reproducible test case
shows itself to be really reproducible, those differences possibly can
be ignored.

I understand the try_files concept very well.

The older http://forum.nginx.org/read.php?2,217122 (and
replies) suggests that you didn’t; and this thread (at
http://forum.nginx.org/read.php?2,250342 plus replies) suggests that
you don’t.

My original question was how to avoid repeating
a segment of configuration code endless times,

No, it wasn’t.

It may have been what you intended, but it wasn’t what you asked.

http://forum.nginx.org/read.php?2,250342

more exactly this:
try_files $uri =404;
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass fastcgi;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
include fastcgi.conf;

Using logic and the examples provided into Nginx documentation what I posted
previously should work.

No, it shouldn’t. http://nginx.org/r/try_files.

“file”, “uri”, and “=code” are different things. “…” means “optional
repetition of the previous” – so there can be multiple “file”
parameters,
and exactly one “uri” or exactly one “=code”.

If there is a phrasing that would make the documentation clearer on that
point, I’m sure it would be welcomed. (But it seems clear to me already,
so I’m not sure what to change.)

Can you provide a solution to my question?

http://nginx.org/r/include

f

Francis D. [email protected]

Francis,

We are going in circles without reaching a solution. I think what I
asked is
very clear and simple:
How do I avoid repeating a segment of configuration code assigned to
@php
into various locations:
location @php {
try_files $uri =404;
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass fastcgi;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
include fastcgi.conf;
}

The above configuration will never change, regardless in what location
is
used:
location ^~ /alpha {
auth_basic “Restricted Access”;
auth_basic_user_file htpasswd;
try_files $uri $uri/ /alpha/index.php?$uri&$args;
location ~ .php$ {
try_files @php =404;
}
}

location ^~ /beta {
try_files $uri $uri/ /beta/index.php?$uri&$args;
location ~ .php$ {
try_files @php =404;
}
}

If I replace the @php contents into /beta location, everything works.
location ^~ /beta {
try_files $uri $uri/ /beta/index.php?$uri&$args;
location ~ .php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass fastcgi;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
include fastcgi.conf;
}
}

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

I have the same problem in my php-application. Admin folder is protected
with auth_basic and the rest folders - without auth. I have not found
any solution except code duplication for php location.

— Original message —
From: “TECK” [email protected]
Date: 2 June 2014, 06:47:47

yes.

update and test

02.06.2014 10:24, wishmaster пишет:

On 2 June 2014 04:47, TECK [email protected] wrote:

Francis,

We are going in circles without reaching a solution

Fortunately, this being a public mailing list, and Francis
(along with almost every other subscriber) giving his time, experience
and opinions for free, you are definitely no worse off than when you
started.

Actually, however, you’re demonstrably better off, as Francis has both
attempted to help you go through the kind of troubleshooting process
that will serve you well if you apply it (Message-ID
[email protected]) and …

I think what I asked is
very clear and simple:
How do I avoid repeating a segment of configuration code assigned to @php
into various locations:

… he has given you the answer to this question - that you clearly
thought you began by asking, but didn’t. In Message-ID
[email protected]

HTH.

Jonathan M. Wrote:

I think what I asked is
very clear and simple:
How do I avoid repeating a segment of configuration code assigned to
@php
into various locations:

… he has given you the answer to this question - that you clearly
thought you began by asking, but didn’t. In Message-ID
[email protected]

HTH.

I’m sorry, I did not understood nothing. Can you provide an example of
how
to avoid repeating the php configuration through @php location?
IMO the logic presented is clear: define a @php location and call it in
other locations with try_files, like explained into Nginx documentation.
In
my case the end result is unexpected, the php file contents will be
downloaded, instead of being executed. This works:
http://pastie.org/private/5gkl1pti1co0onzhgb8jpg

This does not:
http://pastie.org/private/iuz58naozk6xo92ukqmwsg

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

I’m sorry, I did not understood nothing. Can you provide an example of how
to avoid repeating the php configuration through @php location?

As someone said in earlier mails you can always use include and put the
repeating parts in seperate files.

For example put this into php.conf:

location ~ .php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass fastcgi;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
include fastcgi.conf;
}

And then your main config will look like:

location ^~ /alpha {
auth_basic “Restricted Access”;
auth_basic_user_file htpasswd;
try_files $uri $uri/ /alpha/index.php?$uri&$args;
include php.conf;
}

location ^~ /beta {
try_files $uri $uri/ /beta/index.php?$uri&$args;
include php.conf;
}

rr

support Wrote:

yes.

update and test

02.06.2014 10:24, wishmaster пишет:

I have the same problem in my php-application. Admin folder is
protected with auth_basic and the rest folders - without auth. I have
not found any solution except code duplication for php location.

Can you please explain what to update and test? Is unclear.

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

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