如何使用NGINX作为反向代理,为我的特定用例传递查询字符串参数

问题描述

我的NGINX配置如下所示。

  upstream api {
     ...
  }

server {

  listen 2023; 

  server_name www.server.com;

  location /api/v1/comment/ {

    rewrite /api/v1/comment(.*) /api/v1/comment$1 break;
    proxy_pass http://api/;

  }

以下路径组合有效并从上游API返回数据,但前两个具有不理想的多余斜杠:

  • api / v1 / comment /?foo = 1234
  • api / v1 / comment /
  • api / v1 / comment / 1

我希望使用以下路径组合:

  • api / v1 / comment?foo = 1234(而不是api / v1 / comment /?foo = 1234)
  • api / v1 / comment(而不是api / v1 / comment /)
  • api / v1 / comment / 1(现在可以使用-这是需要的)

我一直在努力使它保持原样,并且想知道是否有任何出色的stackoverflowers可以帮助一个人弄清楚我要完成的工作。我在网上尝试过的大多数答案都没有用,这是我着眼于该功能的第一件事。。。。。。

谢谢!

解决方法

我简直不敢相信我以前没看过...

不是在重写命令中将/ comment /保留为位置和/ comment的一部分:

 upstream api {
     ...
  }

server {

  listen 2023; 

  server_name www.server.com;

  location /api/v1/comment/ {

    rewrite /api/v1/comment(.*) /api/v1/comment$1 break;
    proxy_pass http://api/;

  }

我从位置中删除了/ comment /,并从重写命令中删除了/ comment:

 upstream api {
     ...
  }

server {

  listen 2023; 

  server_name www.server.com;

  location /api/v1/ {

    rewrite /api/v1(.*) /api/v1$1 break;
    proxy_pass http://api/;

  }

做到了这一点,因此在联系反向代理以使与API的通信正常工作时,我不必使用多余的/。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...