Php 动态Url段的Nginx位置块

Php 动态Url段的Nginx位置块,php,nginx,Php,Nginx,我的PHP应用程序自动检测到请求通过url ie中的关键字manage路由到管理区域: 这个目录实际上不存在 请求应该路由到index.php文件,该文件在最初的管理请求中没有,但任何链接都会在nginx中产生“未指定输入文件”错误 我需要一个位置块,将工作在一个不存在的url段 我尝试将其重新写入主索引文件,如下所示: location /manage { try_files $uri $uri/ @back; } location @back { rewrite ^/ma

我的PHP应用程序自动检测到请求通过url ie中的关键字manage路由到管理区域:

这个目录实际上不存在

请求应该路由到index.php文件,该文件在最初的管理请求中没有,但任何链接都会在nginx中产生“未指定输入文件”错误

我需要一个位置块,将工作在一个不存在的url段

我尝试将其重新写入主索引文件,如下所示:

location /manage {
    try_files $uri $uri/ @back;
}

location @back {
    rewrite ^/manage/(.*)$ /index.php?_route_=$1 last;
}
这对Apache很好,但在Nginx中会产生500错误

如有任何建议,将不胜感激

更新:

注释中请求的完整配置:

upstream myapp {
    server unix:/srv/users/serverpilot/run/myapp.php-fpm.sock;
}

server {
    listen 80;
    server_name my.domain.com;

    root /srv/users/serverpilot/apps/myapp/public;
    index index.php index.html index.htm;

    access_log /srv/users/serverpilot/log/myapp/myapp_nginx.access.log;
    error_log  /srv/users/serverpilot/log/myapp/myapp_nginx.error.log;

    location /asset {
        rewrite ^/asset/(.*)$ /public/asset/$1 break;
    }

    location /image {
        rewrite ^/image/(.*)$ /public/image/$1 break;
    }

    location / {
        try_files $uri $uri/ @front;
    }

    location @front {
        rewrite ^/(.+)$ /index.php?_route_=$1 last;
    }

    location ~ \.php$ {
        include fastcgi.conf;
        fastcgi_pass myapp;
    }
}
更新:

请求错误日志

15/05/14 10:42:04 [error] 32704#0: 
*5 FastCGI sent in stderr: 
"Unable to open primary script: /srv/users/username/apps/myapp/public/manage/index.php (No such file or directory)" 
while reading response header from upstream, client: 129.349.569.789, 
server: myapp.example.com, 
request: "GET /manage/index.php?route=common/forgotten HTTP/1.1", 
upstream: "fastcgi://127.0.0.1:9002", 
host: "myapp.example.com", 
referrer: "http://myapp.example.com/manage"
更新:

请求的
.htaccess
文件:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^(.*)/$ /$1 [L,R=301]
    RewriteRule ^asset/(.*)$ public/asset/$1 [L,QSA]
    RewriteRule ^image/(.*)$ public/image/$1 [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|txt|html|woff|ttf|eot|svg|css|js)
    RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
</IfModule>

重新启动发动机
重写规则^(.*)/$/$1[L,R=301]
重写规则^asset/(.*)$public/asset/$1[L,QSA]
重写规则^image/(.*)$public/image/$1[L]
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写条件%{REQUEST\u URI}!。*\。(ico | gif | jpg | jpeg | png | txt | html | woff | ttf | eot | svg | css | js)
RewriteRule^([^?]*)index.php?_route_uu=$1[L,QSA]

从您发布的错误日志和配置中,对
/manage/index.php?route=common/forget
的请求将与
/
php
位置块匹配

考虑到设置的方式,PHP位置块将始终优先,因此请求将传递给FastCGI进行处理,并且由于
site.com/manage/index.PHP
不存在,您将得到一个错误。也就是说,在
/
中的
try_文件和在
@front
中的重写根本就不会出现在图片中

您需要做的是添加一个位置块,该位置块在位置匹配顺序中的位置高于PHP位置块,以处理此类请求

upstream myapp {
    server unix:/srv/users/serverpilot/run/myapp.php-fpm.sock;
}

server {
    listen 80;
    server_name example.com;

    root /srv/users/serverpilot/apps/myapp/public;
    index index.php index.html index.htm;

    access_log /srv/users/serverpilot/log/myapp/myapp_nginx.access.log;
    error_log  /srv/users/serverpilot/log/myapp/myapp_nginx.error.log;

    location /asset {
        rewrite ^/asset/(.*)$ /public/asset/$1 break;
    }

    location /image {
        rewrite ^/image/(.*)$ /public/image/$1 break;
    }

    location / {
        try_files $uri $uri/ @front;
    }

    location ~ ^/manage {
        rewrite ^([^?]*)$ /index.php?_route_=$1;
    }

    location ~ \.php$ {
        include fastcgi.conf;
        fastcgi_pass myapp;
    }

    location @front {
        rewrite ^([^?]*)$ /index.php?_route_=$1;
    }
}
通过这种安排,请求将匹配
/
/Manage
php
位置块
/Manage
php
是regex类型的块,它们将优先,在这两个块中,
/Manage
将优先,因为它出现在前面,并相应地重定向

对于重定向,请求将匹配
/
php
位置块
php
将优先,并且由于
site.com/index.php
确实存在,因此将相应地进行处理

另外,我注意到您的配置中有
\u路线
,日志中有
路线
。拿着它,那只是为了测试

****替代配置****
结果它一直就在我的眼皮底下

upstream myapp {
    server localhost:9002;
}

server {
    listen 80;
    server_name myapp.example.com;

    root /srv/users/serverpilot/apps/myapp/public;
    index index.html index.htm index.php;

    access_log /srv/users/serverpilot/log/myapp/myapp_nginx.access.log;
    error_log  /srv/users/serverpilot/log/myapp/myapp_nginx.error.log;

    location / {
        try_files $uri $uri/ @front;
    }

    location ~ ^/manage {
        rewrite ^/(.+)$ /index.php?_route_=$1 last;
    }

    location ~ \.php$ {
        include fastcgi.conf;
        fastcgi_pass myapp;
    }

    location @front {
        rewrite ^/(.+)$ /index.php?_route_=$1 last;
    }
}

感谢@Dayo提供的所有帮助。

以及这些链接指向何处?顺便说一句,如果您确定
manage
目录不存在,您可以跳过
try_文件
,并将rewrite放在第一个位置所有带有关键字
manage
的URL通过index.php路由到管理员控制器。是的,我确定目录不存在。显示完整的nginx配置。还有其他php可用吗?问题中添加了配置。是的,所有php都适用于应用程序前端。不,这不适用。同时启用了
manage
rewrites,并且只启用了第二个,url只加载正常的索引页面。如果我注释掉第二个
manage
rewrite,我会得到一个404。管理区域的整个url需要保持不变。除了
manage
段之外,管理区根本不使用任何url重写。该段告诉应用程序加载管理模块。如果url中没有该段,则应用程序知道加载前端模块。@VinceK这实际上是肯定的,因为这意味着
manage
块现在正在处理请求,只需正确重写即可。我原以为您想将表单的传入请求,
site.com/manage/ABC/XYZ
更改为
site.com/index.php?\u route\uu=ABC/XYZ
,但事实似乎并非如此。那么,您能否给出一个传入请求/链接示例以及应用程序的期望值?有了这些信息,相应地调整重写规则就变得微不足道了。指向管理员的链接应该保持原样
example.com/manage/index.php?route=xxx/xxx&token=1234&product_id=5678
,依此类推。顺便说一句,感谢您的帮助。仅为澄清起见,
manage
段可由最终用户选择。可以将其设置为
steve
aardvarks
,无论应用程序的用户想要什么,以便对其管理区域保密。
upstream myapp {
    server localhost:9002;
}

server {
    listen 80;
    server_name myapp.example.com;

    root /srv/users/serverpilot/apps/myapp/public;
    index index.html index.htm index.php;

    access_log /srv/users/serverpilot/log/myapp/myapp_nginx.access.log;
    error_log  /srv/users/serverpilot/log/myapp/myapp_nginx.error.log;

    location / {
        try_files $uri $uri/ @front;
    }

    location ~ ^/manage {
        rewrite ^/(.+)$ /index.php?_route_=$1 last;
    }

    location ~ \.php$ {
        include fastcgi.conf;
        fastcgi_pass myapp;
    }

    location @front {
        rewrite ^/(.+)$ /index.php?_route_=$1 last;
    }
}