6

I have an app behind nginx. But i need a specific path in this app redirect to a Wordpress Blog

Example :

example.com/ -------> Redirect to my app

example.com/whatever/ -------> Redirect to my app too

example.com/blog/ ------->Redirect to my Wordpress Blog

So, I add a location matching this sub-path

server {
        listen 80 default_server;

        index index.php;

        server_name _;

        location ^~ /blog {
                root /path/to/my/blog;
                index index.php index.html;

                location ^~ /blog/(.*\.php)$ {
                   fastcgi_pass   127.0.0.1:9000;
                   fastcgi_index index.php;
                   fastcgi_param  SCRIPT_FILENAME  /path/to/my/blog/$fastcgi_script_name;
                   include fastcgi_params;
                }
        }

        location ~* /(.*) {
                #here the conf for the rest of the website
        }
}

And when i try to get the page, i have a 404 with this eror in the logs :

2016/05/22 15:27:24 [error] 21759#0: *1 open() "/path/to/my/blog/blog/index.php" failed (2: No such file or directory), client: XX.XX.XX.XX, server: _, request: "GET /blog/index.php HTTP/1.1", host: "example.com"

With /blog is duplicate.

How i can fix this ?

EDIT :

Now i have this (Thanks to Richard Smith) :

location ^~ /blog {
                root /path/to/my/;
                index index.php;
                try_files $uri $uri/ /blog/index.php;

                location ~ \.php$ {
                        try_files $uri =404;
                        include fastcgi_params;
                        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                        fastcgi_pass   127.0.0.1:9000;
                }
        }

But now, i got index.php event if i try to ge another file (toto.html for example)

If i replace

            try_files $uri $uri/ /blog/index.php;

with

            try_files $uri $uri/;

I got a 404 with

2016/05/22 20:57:21 [error] 22621#0: *1 "/path/to/my/blog/toto.html/index.php" is not found (20: Not a directory), client: 84.98.248.33, server: _, request: "GET /blog/toto.html/ HTTP/1.1", host: "example.com"

in the logs

EDIT 2 :

The file exist and currently, and i give it 777 rights (i will remove them later before go to production):

drwxrwxrwx  2 user group 4096 May 22 20:36 .
drwxr-xr-x 11 user group   4096 May 23 06:20 ..
-rwxrwxrwx  1 user group  126 May 22 13:30 index.php
-rwxrwxrwx  1 user group  102 May 22 10:25 old.index.html
-rwxrwxrwx  1 user group   12 May 22 12:24 toto.html

Thank you fo your patience !

Varkal
  • 163
  • 1
  • 1
  • 6
  • `try_files $uri $uri/;` is not really valid. See [this document](http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files). The error message implies that `/path/to/my/blog/toto.html` either does not exist or cannot be read by the `nginx` user process. – Richard Smith May 22 '16 at 22:28
  • I've checked : the file exist and rights isn't an issue – Varkal May 23 '16 at 06:27
  • Currently works... maybe a cache issue thanks you ! – Varkal May 23 '16 at 17:43

3 Answers3

6

As /blog is the first component of the URI, you need to remove it from the root. Use root /path/to/my when inside location ^~ /blog.

See this document for details.

Also, your .php location is invalid syntax. You can use something like this:

location ^~ /blog {
    root /path/to/my;
    index index.php;
    try_files $uri $uri/ /blog/index.php;

    location ~ \.php$ {
        try_files $uri =404;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass   127.0.0.1:9000;
    }
}

See this document for details.

Finally, the rest of the website can use normal locations, such as location / as the ^~ modifier on your location ^~ /blog gives it precedence for any URI beginning with /blog. See my second reference for details.

Richard Smith
  • 12,834
  • 2
  • 21
  • 29
  • First, thanks for your answer, this should works for php files but this will also work for media files inside /blog ? Or maybe i should add anlther location for media files ? – Varkal May 22 '16 at 16:42
  • @Varkal Sure, you can add more prefix or regex locations inside the `/blog` location, if your need `nginx` to do something special with those URIs. – Richard Smith May 22 '16 at 16:48
  • Thanks Richard, can you look my edit please ? – Varkal May 22 '16 at 21:04
  • I got this error `[emerg] invalid number of arguments in "try_files" directive in /etc/nginx/nginx.conf:121` – Salem F Jan 20 '22 at 22:09
0

I tried all methods to make subdirectory to run but only this code work for me

location ^~ /eng{ 
 root      /var/www/example.com;
 index index.php;
 try_files $uri $uri/ /eng/index.php$is_args$args; 

location ~ \.php$ { 
 root      /var/www/example.com;
 include fastcgi_params;
 fastcgi_buffers 256 4k;
 fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
 fastcgi_pass unix:/run/php-fpm/www.sock;
            }
   }

this will make your subdirectoy wordpress work like charm not wo-login.php redirection no page not found error . Notice I didn't put the subdirectory in root directive and this is what's make it's works without errors

Salem F
  • 173
  • 1
  • 10
0

I needed this for Matomo installation in a subdirectory:

location ^~ /matomo {
alias /usr/share/matomo;
index index.php;
try_files $uri $uri/ /matomo/index.php;

location ~ \.php$ {
    try_files $uri =404;
    include fastcgi_params;
    fastcgi_param HTTP_PROXY "";
    fastcgi_param SCRIPT_FILENAME /usr/share/$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param QUERY_STRING $query_string;
    fastcgi_intercept_errors on;
    fastcgi_read_timeout 300;
    fastcgi_buffers 32 256k;
    fastcgi_buffer_size 512k;       
    # PHP socket location.
    fastcgi_pass 127.0.0.1:9000;
}

}

Notice the /usr/share instead of /usr/share/matomo in the SCRIPT_FILENAME, that has taken me a while of tcpdumping to figure out.