Nginx 不匹配路径位置

Nginx 不匹配路径位置

我正在尝试根据位置执行 Fastcgi 服务器。

所有请求 /api/v1/ -> server v1:9000 和 /api/v2/ -> server v2:9000 我的 nginx 配置是

server {
listen 80;
index index.php index.html;
root /var/www/public;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass v1:9000;
        if ( $uri ~ "^/api/v1/") {
            fastcgi_pass v1:9000;
        }
        if ( $uri ~ "^/api/v2/") {
                fastcgi_pass v2:9000;
        }
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
    location / {
                 try_files $uri /index.php?$args;
             }
}

答案1

您需要location针对不同版本使用指令。例如:

location ^~ ^/api/v1/.+\.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass v1:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
}

location ~ ^/api/v2/.+\.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass v2:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
}

相关内容