我的PHP应用程序会自动检测到请求通过网址中的关键字manage路由到管理区域,即:
此目录实际上并不存在。
请求应该路由到index.php文件,该文件在管理/的初始请求上执行,但是任何链接都会生成“未指定输入文件”。 nginx中的错误。
我需要一个适用于不存在的网址段的位置块。
我尝试将其重写为主索引文件,如下所示:
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>
答案 0 :(得分:1)
根据您发布的错误日志和配置,/manage/index.php?route=common/forgotten
的请求将与/
和php
位置块匹配。
鉴于如何设置Nginx Location Matching Order,PHP位置块将始终优先,因此请求将传递给FastCGI进行处理,并且由于site.com/manage/index.php
不存在,您将收到错误。也就是说,try_files
中的/
和@front
中的重写根本不会进入图片。
您需要做的是添加位置匹配顺序比位置块更高的位置块,以处理此类请求。
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
是正则表达式类型块,将优先于/Manage
,因为它首先出现,将优先并相应地重定向。
对于重定向,请求将与/
和php
位置块匹配。 php
将优先,并且site.com/index.php
确实存在,因此会进行处理。
PS。我注意到您的配置中有_route_
,日志中有route
。拿它只是为了测试。
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 ~ \.php$ {
include fastcgi.conf;
fastcgi_pass myapp;
}
location ~ ^/asset/(.*)$ {
rewrite ^ public/asset/$1;
}
location ~ ^/image/(.*)$ {
rewrite ^ public/image/$1;
}
location / {
rewrite ^/(.+)/$ /$1 permanent;
try_files $uri $uri/ @front;
}
location @front {
rewrite ^/([^?]*)$ /index.php?_route_=$1;
}
}
答案 1 :(得分:0)
事实证明,这一直是在我的鼻子底下。
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的所有帮助。