我的服务器的nginx配置如下,我想要
service/dev/sync/sync
/home/work/app-web/src/api/dev/sync.php/sync
$fastcgi_script_name
匹配为/dev/sync.php
,$fastcgi_path_info
为sync
~ /service/([a-zA-Z]*/[a-zA-Z]*)(/[a-zA-Z]*)$
。 但是当我请求/service/dev/sync/sync
时,它将返回404错误
*173 open() "/home/work/nginx/html/dev/sync.php/sync" failed (2: No such file or directory), client: 172.18.17.90, server: localhost, request: "POST /service/dev/sync/sync HTTP/1.1"
/home/work/nginx/html
是我的nginx服务器的默认根目录。 root
配置不是
工作。
这个配置有什么问题?
server {
set $work_dir /home/work/app-web;
listen 80;
server_name localhost;
location ~ /service/([a-zA-Z]*/[a-zA-Z]*)(/[a-zA-Z]*)$ {
root $work_dir/src/api;
set $file $1;
set $action $2;
try_files $uri $uri/ /$file.php$action$is_args$query_string;
location ~ \.php {
fastcgi_split_path_info ^(.+\.php)/(.*)$;
fastcgi_pass unix:/home/work/app-web/php/var/run/app-web.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $work_dir/src/api$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param UNIQUE_ID $request_id;
include fastcgi_params;
}
}
}
答案 0 :(得分:1)
root配置正常。 你的问题是有一个内部重定向到/dev/sync.php/sync ?,新的内部网址与regexp-location不匹配 您可以使用
激活调试error_log /var/log/nginx/debug.log debug;
日志如下所示:
*1 trying to use file: "/dev/sync.php/sync" "/home/work/app-web/src/api/dev/sync.php/sync"
*1 internal redirect: "/dev/sync.php/sync?"
*1 rewrite phase: 1
*1 http script value: "/home/work/app-web"
*1 http script set $work_dir
*1 test location: ~ "/service/([a-zA-Z]*/[a-zA-Z]*)(/[a-zA-Z]*)$"
*1 using configuration ""
新网址为/dev/sync.php/sync ?,此网址与无位置匹配,因为正则表达式/ service /([a-zA-Z] / [a-zA-Z] )(/ [a-zA-Z] *)$不适用于内部网址。
我建议你另外一个配置:
server {
set $work_dir /home/work/app-web;
listen 80;
server_name localhost;
location ~ /service/([a-zA-Z]*/[a-zA-Z]*)(/[a-zA-Z]*)$ {
root $work_dir/src/api/;
set $file $1;
set $action $2;
rewrite .* /src/api/$file.php$action last;
}
location ~ \.php {
internal;
root $work_dir;
fastcgi_split_path_info ^(.+\.php)/(.*)$;
fastcgi_pass unix:/home/work/app-web/php/var/run/app-web.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param UNIQUE_ID $request_id;
include fastcgi_params;
}
error_log /var/log/nginx/test-error.log debug;
}
注意internal;
指令