Nginx和PHP的子目录

我在nginx后面有一个应用程序。 但我需要在这个应用程序redirect到一个WordPress博客的具体path

例如:

example.com/ ——->redirect到我的应用程序

example.com/whatever/ ——->也redirect到我的应用程序

example.com/blog/ ——->redirect到我的WordPress的博客

所以,我添加了一个匹配这个子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 } } 

而当我尝试获取页面,我有一个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" 

与/博客是重复的。

我该如何解决这个问题?

编辑:

现在我有了这个(感谢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; } } 

但是现在,我得到了index.php事件,如果我尝试创build另一个文件(例如toto.html)

如果我replace

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

  try_files $uri $uri/; 

我得到了一个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" 

在日志中

编辑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 

谢谢你的耐心!

由于/blog是URI的第一个组件,您需要将其从root删除。 在内部location ^~ /blog使用root /path/to/my

详情请参阅此文件 。

另外,你的.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; } } 

详情请参阅此文件 。

最后,网站的其他部分可以使用正常的位置,比如location /作为您的location ^~ /blog上的^~修饰符。 location ^~ /blog会以任何以/blog开头的URI为优先。 请参阅我的第二个参考了解详情