Nginxconfiguration不能在子目录中使用wordpress

我有一个nginx安装,我需要在开发中托pipe网站,以便其他人可以testing出来。

现在,我已经为单个子域设置了一个虚拟主机,并且在这个目录中,我需要打开一个符号链接项目,以便testing。 正如你所看到的,我限制了访问整个网站,但启用了特定的目录,但保持私有一个简单的auth_basic。

PHP的运行,但我一直得到的东西除了/或/ wp-admin /之外的其他东西,所有其他永久链接给404s。 我已经做了所有的事情来完成这个工作,但我不知道我做错了什么。 请在下列configuration中指出我的错误:

server { listen 80; server_name dev.example.com; client_max_body_size 20m; server_tokens off; root /srv/dev.example.com; index index.php index.html index.htm; location / { deny all; } location /my-site { allow all; try_files $uri $uri/ /index.php?$args; auth_basic "Restricted"; auth_basic_user_file /etc/nginx/auth.d/.htpasswd-my-site; location ~ \.php$ { include fastcgi_params; fastcgi_index index.php; fastcgi_pass php5-fpm-sock; fastcgi_param SCRIPT_FILENAME $request_filename; fastcgi_intercept_errors on; fastcgi_param HTTPS $https; } } } 

谢谢!

更新:

这个请求可以工作: http://dev.example.com/my-site//srv/dev.example.com/my-site/index.php ,它会导致加载文件/srv/dev.example.com/my-site/index.php

wordpresspipe理界面的请求也可以工作: http://dev.example.com/my-site/wp/wp-admin/srv/dev.example.com/my-site/wp/wp-admin/index.php ,也直接到索引文件: /srv/dev.example.com/my-site/wp/wp-admin/index.php 。 pipe理界面中的所有东西都可以工作,因为它不使用永久链接,而是使用原始的GETinput。

不过,只要我尝试加载永久链接(我使用最简单的格式/%postname%/ ),它就无法find它,而location location /my-site {}的try_files指令应该匹配它们。 这意味着如下路线不起作用:

 http://dev.example.com/my-site/about http://dev.example.com/my-site/contact http://dev.example.com/my-site/etc 

我会假设你的文件系统中不存在以下文件:

 /srv/dev.example.com/my-site/about/index.{php,html,htm} /srv/dev.example.com/my-site/contact/index.{php,html,htm} /srv/dev.example.com/my-site/etc/index.{php,html,htm} 

你的问题来自于你的php fallback位置嵌套在/my-site位置,而try_files指令可能不像你所期望的那样工作。

事实上, try_files接受作为最后一个参数:

  1. 一个URI
  2. 一个命名的位置
  3. 一个HTTP代码

对于选项1和2,它将暗示内部redirect到指定的元素。 在你的情况下, $uri/index.php /index.php?$args被解释为一个URI,如果$uri$uri/$uri/index.php$uri/index.html$uri/index.htm ,nginx将内部redirect到它缺失。

现在,另一件事是:你使用一个嵌套的位置来处理PHP文件处理。 但是,由于您的try_files最后一个参数是/index.php?$args URI,它不会匹配apex位置/my-site所以它由服务器块处理,如果URI不能parsing为本地文件,那么您最终得到一个HTTP 404答复。

请注意, index指令也意味着内部redirect到指定的索引文件,但由于URI以/my-site开头,它将始终落在你的php位置。

所以你必须将这个位置移动到与其他位置相同的位置,或者将你的try_files URI作为你的顶点位置的前缀,并确保php文件位于正确的位置。