子目录中的 Nginx 和 PHP

Var*_*kal 5 php nginx php-fpm

我在 nginx 后面有一个应用程序。但我需要此应用程序中的特定路径重定向到 Wordpress 博客

例子 :

example.com/ -------> 重定向到我的应用程序

example.com/whatever/ -------> 也重定向到我的应用程序

example.com/blog/ ------->重定向到我的 Wordpress 博客

所以,我添加了一个匹配这个子路径的位置

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
        }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试获取该页面时,我在日志中有一个带有此错误的 404:

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"
Run Code Online (Sandbox Code Playgroud)

与 /blog 是重复的。

我该如何解决这个问题?

编辑 :

现在我有了这个(感谢理查德史密斯):

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;
                }
        }
Run Code Online (Sandbox Code Playgroud)

但是现在,如果我尝试获取另一个文件(例如 toto.html),我会收到 index.php 事件

如果我更换

            try_files $uri $uri/ /blog/index.php;
Run Code Online (Sandbox Code Playgroud)

            try_files $uri $uri/;
Run Code Online (Sandbox Code Playgroud)

我得到了一个 404

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"
Run Code Online (Sandbox Code Playgroud)

在日志中

编辑 2:

该文件存在且当前存在,我给它 777 权限(我将在投入生产之前将其删除):

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
Run Code Online (Sandbox Code Playgroud)

感谢您的耐心等待!

Ric*_*ith 6

由于/blog它是 URI 的第一个组成部分,您需要将其从root. root /path/to/my室内时使用location ^~ /blog

有关详细信息,请参阅此文档

另外,您的.php位置语法无效。你可以使用这样的东西:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅此文档

最后,网站的其余部分可以使用正常位置,例如location /your^~上的修饰符location ^~ /blog赋予它以 开头的任何 URI 的优先级/blog。详情请参阅我的第二个参考资料。