Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Nginx重写url路径以添加路径前缀_Nginx - Fatal编程技术网

Nginx重写url路径以添加路径前缀

Nginx重写url路径以添加路径前缀,nginx,Nginx,我正在使用我的nginx服务器,我不知道如何将/api前缀添加到现有的url 我有两个端点/api/deepzoom和/api/deepzoom被烧瓶暴露。 我还不想更改前端调用/deepzoom和/detection的代码 使用/deepzoom从前端调用时,如何将url路径重写/重定向到/api/deepzoom 我在nginx.conf中的当前代码段: upstream platform { server platform:5001; } upstream models {

我正在使用我的nginx服务器,我不知道如何将/api前缀添加到现有的url

我有两个端点/api/deepzoom和/api/deepzoom被烧瓶暴露。 我还不想更改前端调用/deepzoom和/detection的代码

使用/deepzoom从前端调用时,如何将url路径重写/重定向到/api/deepzoom

我在nginx.conf中的当前代码段:

upstream platform {
    server platform:5001;
}
upstream models {
    server models:4999;
}

upstream deepzoom {
    server deepzoom:5999;
}

server {
    listen 80 ;
    server_name  myhost.mydomain.com;

    client_max_body_size    0;
    client_body_buffer_size 1m;
    proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header   Host $host:$server_port;
    proxy_set_header   X-Real-IP  $remote_addr;
    send_timeout       10m;
    proxy_buffer_size 512k;
    proxy_buffers 4 1024k;
    proxy_busy_buffers_size 1024k;
    proxy_redirect off;

location ^~ /api/detection/ {
    include uwsgi_params;
    proxy_pass    http://models;
}

location ^~ /api/deepzoom/ {
    include uwsgi_params;
    proxy_pass    http://deepzoom;
}
location  / {
    include uwsgi_params;
    proxy_pass         http://platform;

    }
 }
我已尝试在服务器块中添加行:

rewrite ^ /detection/ http://$server_name/api/detection/$1 permanent;
rewrite ^ /deepzoom/ http://$server_name/api/deepzoom/$1 permanent;
但它不适用于404未找到的错误


任何人都可以帮助我找出它,以及如何满足要求。谢谢

您问题中的
重写…永久性
语句格式错误,无法执行您所需的功能

  • permanent
    使用301响应导致重定向
  • ^
    /
    字符之间有空格
  • 没有括号来表示
    $1

要在向上游传递URI之前对其进行内部重写,可以使用
rewrite…last
。有关详细信息,请参阅

例如:

rewrite ^(/(detection|deepzoom)(/.*)?)$ /api$1 last;
location ^~ /api/detection { ... }
location ^~ /api/deepzoom { ... }
location ^~ /deepzoom {
    include     uwsgi_params;
    proxy_pass  http://deepzoom/api/deepzoom;
}
注意,如果您的端点是
/api/deepzoom
,则您不希望在
位置
值上有尾随的
/


您可以使用
proxy\u pass
指令实现类似的行为。有关详细信息,请参阅

例如:

rewrite ^(/(detection|deepzoom)(/.*)?)$ /api$1 last;
location ^~ /api/detection { ... }
location ^~ /api/deepzoom { ... }
location ^~ /deepzoom {
    include     uwsgi_params;
    proxy_pass  http://deepzoom/api/deepzoom;
}

注意
位置
代理传递
值都有尾随
/
,或者都没有尾随
/

是否要用3xx响应重定向URI,或者在将URI传递到上游之前对其进行内部重写?@RichardSmith,实际上,我想在传递到上游之前在内部重写URI。