Handling subdirectories location

Hi,

I have the following in my nginx configuration:

server {
listen 8080;
server_name myservername.com;
root /data/www/myservername.com;

location / {
    index index.php index.html index.htm;
    try_files $uri $uri/ /index.html;
}

# redirect server error pages to the static page /50x.html
#
error_page   500 502 503 504  /50x.html;
location = /50x.html {
    root   html;
}

# Pass the PHP scripts to FastCGI server listening on 

127.0.0.1:9000.
location ~ .php$ {
try_files $uri = 404;
# fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
include fastcgi_params;
}
}

But how do I configure ‘location’ so /subdir/index.php is processed
instead of /index.php?

Basically I’m trying to put third_party apps in their /subdirs/ to
test them, but when they navigate to index.php I’m thrown back to the
root /index.html for example.

Thanks in advance,


Thiago F.

What I’m trying to do is something like the following:

http://domainame.com/site1/index.php
http://domainame.com/site2/index.php
http://domainame.com/site3/index.php

The closest I could find on Google was
http://programmersjunk.blogspot.com.br/2013/11/nginx-multiple-sites-in-subdirectories.html

Ruslan, could you help me setup the nginx config for this?


Thiago F.

The issue I’m facing is that
http://myservername.com/foo/index.php/secure/login redirects to
http://myservername.com/index.html.

Can someone help me fix this?


Thiago F.

On Mon, Jun 08, 2015 at 02:07:54PM -0300, Thiago F. wrote:

Hi there,

What I’m trying to do is something like the following:

http://domainame.com/site1/index.php
http://domainame.com/site2/index.php
http://domainame.com/site3/index.php

In nginx, one request is handled in one location.

See http://nginx.org/r/location, for example, for the rules by which
one location is chosen.

In this case, you probably want one

location ^~ /site1/ {}

block, plus one

location ^~ /site2/ {}

block; and nested in those, one or more blocks which relate to requests
which should be handled by php.

If you want “every request should be handled by /site1/index.php”,
you write one configuration. If you want “every request that ends
in .php should be handled by a matching file”, you write a different
configuration. If you want "every request that ends in .php, or that
includes the string ‘.php/’, should be handled by a matching file,
you write yet another configuration.

From what I’ve read in these mails, I’m afraid that I do not know what
exactly it is that you want.

For example: does “/foo/index.php/secure/login” refer to exactly that
request; or to any request that starts with “/foo/index.php/;”, or to
any request that starts with “/foo/” and does not exactly name a file
on the filesystem; or to something else?

Possibly something like

root /var/www/html;
include fastcgi.conf;
location ^~ /site1/ {
location ~ .php($|/) {
fastcgi_split_path_info (..php)(/.);
fastcgi_pass unix:php.sock;
}
}
location ^~ /site2/ {
location ~ .php($|/) {
fastcgi_split_path_info (..php)(/.);
fastcgi_pass unix:php.sock;
}
}

will do some of what you want.

Note that trying to configure a “php app” to work in a subdirectory does
require help from the php app. Some insist on being installed in /.

Good luck with it,

f

Francis D. [email protected]

Hi Francis,

First, thanks for replying.

On Mon, Jun 8, 2015 at 7:46 PM, Francis D. [email protected] wrote:

In nginx, one request is handled in one location.
location ^~ /site2/ {}

  fastcgi_split_path_info (.*.php)(/.*);
  fastcgi_pass unix:php.sock;
}

}

As you suggested I tried the following config:
server {
listen 80;
server_name domainame.com;

root /data/www/domainame.com;
include fastcgi.conf;

location ^~ /gocart/ {
  location ~ \.php($|/) {
    fastcgi_split_path_info (.*.php)(/.*);
    fastcgi_pass   127.0.0.1:9000; #unix:/var/run/php5-fpm.sock;
  }
}

}

But when I navigate to http://domainame.com/gocart nginx returns 403
Forbidden.

Basically what I’m trying to do is to run gocart
(https://gocartdv.com) and phorum (www.phorum.org) in
domainame.com/gocart and domainame.com/forum (they are in
/data/www/domainname.com/gocart/ and /data/www/domainame.com/forum).


Thiago F.

On Mon, Jun 8, 2015 at 8:26 PM, Francis D. [email protected] wrote:

root /data/www/domainame.com;

But when I navigate to http://domainame.com/gocart nginx returns 403 Forbidden.

What response do you want?

Be as specific as possible.

(I suspect that the answer is “a http redirect to
http://domainame.com/gocart/”; but I’m reluctant to guess.)

Isn’t obvious what I want? I want the page to load, so if it is “http
redirect to http://domainame.com/gocart/”, then yes.

Perhaps adding “index index.html index.php;” at server-level will help?

Yes, it helps load the start page. But all the links redirects to
http://domainame.com/gocart/.

What I’m trying to do is not common? I thought it would be pretty
common to do what I’m trying to do, but the solution seems to be very
complicated for something that does not look so complicated at glance.

Trying once more, what I’m trying to setup is pretty much exactly this
https://gist.github.com/LkeMitchll/b6d8aea6c0845e3a341f,
http://stackoverflow.com/questions/24820657/nginx-multiple-php-sites-in-sub-directories.

The machine will do exactly what you tell it to. It will only do what
you want it to, if you tell it what you want.

Basically what I’m trying to do is to run gocart
(https://gocartdv.com) and phorum (www.phorum.org) in

I don’t see any obvious “how to install this on a web server, from scratch”
documentation on either of those web sites. Perhaps I’m looking in the
wrong place.

That seems unrelated to this thread. But they all have install
instructions.


Thiago F.

On Mon, Jun 08, 2015 at 08:08:10PM -0300, Thiago F. wrote:

On Mon, Jun 8, 2015 at 7:46 PM, Francis D. [email protected] wrote:

On Mon, Jun 08, 2015 at 02:07:54PM -0300, Thiago F. wrote:

Hi there,

    fastcgi_pass   127.0.0.1:9000; #unix:/var/run/php5-fpm.sock;
  }
}

}

But when I navigate to http://domainame.com/gocart nginx returns 403 Forbidden.

What response do you want?

Be as specific as possible.

(I suspect that the answer is “a http redirect to
http://domainame.com/gocart/”; but I’m reluctant to guess.)

Perhaps adding “index index.html index.php;” at server-level will help?

The machine will do exactly what you tell it to. It will only do what
you want it to, if you tell it what you want.

Basically what I’m trying to do is to run gocart
(https://gocartdv.com) and phorum (www.phorum.org) in

I don’t see any obvious “how to install this on a web server, from
scratch”
documentation on either of those web sites. Perhaps I’m looking in the
wrong place.

(Or perhaps the authors don’t want them to be installed anywhere.)

f

Francis D. [email protected]

On Mon, Jun 08, 2015 at 08:53:45PM -0300, Thiago F. wrote:

On Mon, Jun 8, 2015 at 8:26 PM, Francis D. [email protected] wrote:

On Mon, Jun 08, 2015 at 08:08:10PM -0300, Thiago F. wrote:

Hi there,

    fastcgi_pass   127.0.0.1:9000; #unix:/var/run/php5-fpm.sock;

(I suspect that the answer is “a http redirect to
http://domainame.com/gocart/”; but I’m reluctant to guess.)

Isn’t obvious what I want? I want the page to load, so if it is “http
redirect to http://domainame.com/gocart/”, then yes.

It’s presumably obvious to you. If you are explicit, it avoids anyone
else having to guess.

What does “page to load” mean?

In this context, consider every question to be “what did you do; what
did you see; what did you expect to see?”.

So: “curl -i http://domainame.com/gocart”. Expect http 301 to
http://domainame.com/gocart/. Get that? Good.

“curl -i http://domainame.com/gocart/”. Expect the content of the file
/data/www/domainame.com/gocart/index.html? Or the php-processed output
of the file /data/www/domainame.com/gocart/index.php? Do you get one of
those, or something else? Does a http header indicate that the php
server
was involved?

Type the curl command. If the response is what you expect, report that
and be happy. If the response is not what you expect, report how it
differs. If you do not know what you expect, seek more help – what is
the
response when the thing is installed in the author-intended environment?

Perhaps adding “index index.html index.php;” at server-level will help?

Yes, it helps load the start page. But all the links redirects to
http://domainame.com/gocart/.

What links are they?

Again: be explicit. Spell everything out very slowly. Maybe these links
come from something in the php that expects a specific variable to be
set to a special value; maybe they come from an application config file;
maybe you can adjust your nginx config to make everything work; maybe
you need to change something outside of nginx.

What curl command do you issue? What response do you get? What response
do you want instead?

What I’m trying to do is not common? I thought it would be pretty
common to do what I’m trying to do, but the solution seems to be very
complicated for something that does not look so complicated at glance.

What does your favourite search engine return for “install [this
application] in a subdirectory on nginx”?

If it is not very much, perhaps that is because the config is so
trivially
obvious that it is not worth documenting; or perhaps no-one has ever
tried it before. Which is more likely?

It looks like you are a trailblazer. This means you have to do the
legwork to be the first person to ever get it working.

(So far, I’m seeing about a dozen lines of config, which does not strike
me as especially complicated.)

Trying once more, what I’m trying to setup is pretty much exactly this
https://gist.github.com/LkeMitchll/b6d8aea6c0845e3a341f,

http://stackoverflow.com/questions/24820657/nginx-multiple-php-sites-in-sub-directories.

The first link confuses me: there’s an 84-line config, and I don’t see
how anything after line 42 will ever be used.

The second link doesn’t show an obvious problem.

I don’t see any obvious “how to install this on a web server, from scratch”
documentation on either of those web sites. Perhaps I’m looking in the
wrong place.

That seems unrelated to this thread. But they all have install instructions.

If the authors will describe how they expect things to be configured
in their intended environment (web server and whatever supporting
infrastructure is required), that may help work out how things should
be configured in any other environment (such as nginx).

Good luck with it,

f

Francis D. [email protected]

Hi Francis,

On Mon, Jun 8, 2015 at 9:59 PM, Francis D. [email protected] wrote:

On Mon, Jun 08, 2015 at 08:53:45PM -0300, Thiago F. wrote:

On Mon, Jun 8, 2015 at 8:26 PM, Francis D. [email protected] wrote:

On Mon, Jun 08, 2015 at 08:08:10PM -0300, Thiago F. wrote:

Hi there,

OK. Based on your response I will hold on on what I was trying to do for
now.

It seems it is not the way I should go.


Thiago F.

On Wed, Jun 10, 2015 at 05:00:56PM -0300, Thiago F. wrote:

On Mon, Jun 8, 2015 at 9:59 PM, Francis D. [email protected] wrote:

Hi there,

OK. Based on your response I will hold on on what I was trying to do for now.

It seems it is not the way I should go.

Ok.

I was intrigued to see what was happening, so I fetched the gocart zip
file, extracted it to /usr/local/nginx, did

mv gocart-GoCart-324eccb gc

and looked in the “gc” directory. There seems to be one main php file
which is intended to handle everything, so I added the following to
nginx.conf:

==
include fastcgi.conf;
index index.php index.html;
location ^~ /gc/ {
root .;
error_page 404 = /gc/index.php;
location = /gc/index.php {
fastcgi_pass unix:php.sock;
}
}

reloaded nginx.conf, then pointed my browser at
http://localhost:8000/gc/

I see a page which asks for database details in order to do an
installation; I give the details and I see that 29 tables are added in
my database before I get the http response content of

“”"
Fatal error: Call to undefined function locale_get_default() in
/usr/local/nginx/gc/gocart/migrations/003_gocart2_3.php on line 252
“”"

Rather than solve it properly, I edit that line of that file to
hard-code
a valid locale, and go back to the /gc/ url. It invites me to login and
says my cart is empty. The “login” page invites me to log in, or
register,
and has a “forgot password” link. All links seem to do something useful.

Now there are also “.htaccess” files below the directory “gc”, which say
“Deny from all”; so presumably this nginx config will allow some files
be fetched that the gocart authors would prefer not be fetched – extra
config will be needed to cater for those.

But, unless I’m missing something, it looks like it is working, to the
extent that “things that should succeed do succeed”.

If you’re still willing to test, does that work for you?

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