yeluolei
yeluolei

Reputation: 83

nginx sub location with php-fpm config failed

I have an nginx config for my server as follow, I want

  1. when I request service/dev/sync/sync
  2. it will route to /home/work/app-web/src/api/dev/sync.php/sync
  3. match the php rule with $fastcgi_script_name is /dev/sync.php and $fastcgi_path_info is sync
  4. this rule only need for ~ /service/([a-zA-Z]*/[a-zA-Z]*)(/[a-zA-Z]*)$.

But when I request /service/dev/sync/sync, it will return 404 with error

*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 is the default root for my nginx server. the root config is not work at all.

what's the problem with this config?

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;
    }
}
}

Upvotes: 1

Views: 908

Answers (1)

Arno
Arno

Reputation: 1359

Root config works fine. Your problem is that there is an internal redirection to /dev/sync.php/sync?, and the new inner-url does not match the regexp-location You can activate the debug with

error_log /var/log/nginx/debug.log debug;

The logs look like this :

*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 ""

The new URL is /dev/sync.php/sync?, and this url matches with no location, because the regexp /service/([a-zA-Z]/[a-zA-Z])(/[a-zA-Z]*)$ is not intended for the inner-URL.

I suggest you another configuration :

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;
}

Note the internal; directive

Upvotes: 1

Related Questions