简体   繁体   English

如何将 nginx 中的部分 uri 重定向到另一个应用程序?

[英]How redirect partial uri in nginx to another app?

In my server with nginx with certbot i have two api running, in port 5000 and 3000.在我的带有 certbot 的 nginx 的服务器中,我有两个 api 在端口 5000 和 3000 上运行。

All requests are for the app on port 5000, but when i request it in the 'pdf-generator' uri i want to send to the app on port 3000所有请求都是针对端口 5000 上的应用程序的,但是当我在“pdf-generator”uri 中请求它时,我想发送到端口 3000 上的应用程序

Examples:例子:

  • my-domain/abc -> localhost:5000我的域/abc -> localhost:5000
  • my-domain/pdf-generator -> localhost:3000我的域/pdf 生成器 -> localhost:3000
  • my-domain/pdf-generator/about -> localhost:3000/about我的域/pdf 生成器/关于 -> localhost:3000/about
  • my-domain/pdf-generator/abc-> localhost:3000/abc我的域/pdf 生成器/abc-> localhost:3000/abc

I searched in several places, I saw some similar situations but I didn't get success我在几个地方搜索,我看到了一些类似的情况,但我没有成功

tried:试过:

    location = /pdf-generator/ {
        proxy_pass    http://localhost:3001;
    }
    location /pdf-generator {
        proxy_pass    http://localhost:3001$request_uri;
    }
    location = /pdf-generator/ {
        proxy_pass    http://localhost:3001/$request_uri;
    }
    location /pdf-generator {
        rewrite /pdf-generator/(.*) /$1  break;
        proxy_pass    http://localhost:3001;
    }
    location /pdf-generator {
        rewrite /pdf-generator/(.*) /$1  break;
        proxy_pass    http://localhost:3001/;
    }

Accessing the app's port 3001 directly through the server's IP in the uri "about" appears correctly what I need, but I would like to do it through my domain in the uri "pdf-generator"直接通过 uri“about”中服务器的 IP 访问应用程序的端口 3001 正确显示了我需要的内容,但我想通过 uri“pdf-generator”中的我的域来访问应用程序的端口 3001

  • myip:3001/about -> "ok" myip:3001/about -> “确定”
  • my-domain/pdf-generator/about -> needs to show "ok" my-domain/pdf-generator/about -> 需要显示“ok”

At first i got confuse because I do not understand your pdf-generator should be sent to 3001 or 3000. by the way I use 3000 in the answer.起初我很困惑,因为我不明白你的 pdf 生成器应该发送到 3001 还是 3000。顺便说一句,我在答案中使用了 3000。

You can do it like this:你可以这样做:

    location /pdf-generator {
        rewrite ^/(.*?)pdf-generator[^/] /$1? break;
        rewrite ^/(.*?)pdf-generator(?:/(.*))?$ /$1$2? break;
        proxy_pass    http://localhost:3000;
    }
    location / {
        proxy_pass    http://localhost:5000;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM