AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

    • 主页
    • 系统&网络
    • Ubuntu
    • Unix
    • DBA
    • Computer
    • Coding
    • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 913111
Accepted
SinaOwolabi
SinaOwolabi
Asked: 2018-05-22 07:46:38 +0800 CST2018-05-22 07:46:38 +0800 CST 2018-05-22 07:46:38 +0800 CST

nginx 修改和代理 URL

  • 772

Ngnix新手在这里。请我需要一些帮助来弄清楚如何正确地使 nginx 修改和重定向(代理?)传入请求。重定向似乎工作正常,但 URL 没有在目的地重写。

我的配置是:

        server {

       listen 91 default_server ssl;

       ssl_prefer_server_ciphers on;
       ssl_certificate /etc/nginx/ssl/domain.crt;
       ssl_certificate_key /etc/nginx/ssl/domain.key;

        location /dest {

        rewrite ^a_service_prod&id_number=((1234701|1234708|1234802|1234808|1234812|1234902)\d+)&(.*?)$ /dest?service=a_service_prod.sub_service&operation=sub_service&id_number=$1&$2 break;
            proxy_pass http://192.168.1.1:1440;
            proxy_redirect off;
            proxy_set_header Host $host;
        }
    }

我试图获取诸如/dest?service=a_service_prod&id_number=12347016734696&slime=somethig 被重写的请求并将请求发送到另一台服务器作为http://192.168.1.1:1440/dest?service=a_service_prod.sub_service&operation=sub_service&id_number=12347016734696&slime=somethig

但是在目的地,收到的是http://192.168.1.1:1440/dest?service=a_service_prod&id_number=12347016734696&slime=somethig

请问我做错了什么,我该如何解决?

nginx
  • 1 1 个回答
  • 446 Views

1 个回答

  • Voted
  1. Best Answer
    Richard Smith
    2018-05-22T10:00:19+08:002018-05-22T10:00:19+08:00

    您当前的方法不起作用,因为您试图在rewrite指令的正则表达式中捕获查询字符串。nginx使用规范化的 URI 进行评估rewrite和location指令,其中不包括?和它后面的任何内容。

    $request_uri您可以在变量、变量中找到查询字符串$args- 或在变量中拆分$arg_xxx。有关详细信息,请参阅此文档。

    您可以使用if语句或map指令将正则表达式应用于其中一个变量。

    下面的示例使用带有正则表达式和两个命名捕获的map指令(有关详细信息,请参阅此文档)来重建所需的参数列表。上游 URI 附加到proxy_pass指令中(有关详细信息,请参阅本文档)。

    map $args $newargs {
        default $args;
        ~*^(?<prefix>service=a_service_prod)&(?<suffix>id_number=(?:1234701|1234708|1234802|1234808|1234812|1234902)\d+&.*)$  $prefix.sub_service&operation=sub_service&$suffix;
    }
    server {
        ...
        location /dest {
            proxy_pass http://192.168.1.1:1440$uri?$newargs;
            ...
        }
    }
    
    • 4

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助
subwaysurfers
my femboy roommate

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve