【问题标题】:How to use nginx proxy with access token in url?如何在 url 中使用带有访问令牌的 nginx 代理?
【发布时间】:2020-08-12 22:00:35
【问题描述】:

我正在运行 nginx 作为后端服务的代理。我想使用 url 作为http://IP:PORT/<token>/endpoint 并且应该代理到 url 作为http://backend/endpoint

nginx.conf 文件:-

server {
    listen 80;
    location / {
        proxy_pass         http://backend;
        proxy_set_header Authorization "Basic safdadfWU6cdfdvcmQ=";
    }
}

在这里,我想将<token> 从 url 中取出,并将剩余的端点传递给我的 proxy_pass 服务器。请帮助我。

【问题讨论】:

  • 尝试:rewrite ^/[^/]+(/.*)$ $1 break; 从 URI 中删除第一个路径元素。
  • 感谢@RichardSmith 的回复,您能告诉我应该将rewrite ^/[^/]+(/.*)$ $1 break; 行放在我的nginx.conf 文件中的哪个位置吗?对不起,我对 nginx 代理和配置很天真。
  • 在与 proxy_pass 语句相同的位置块内。

标签: nginx reverse-proxy


【解决方案1】:

谢谢@Richard,您的建议有助于解决我的问题。供其他人参考,更新nginx.conf文件:-

server {
    listen 80;
    location / {
        rewrite ^/[^/]+(/.*)$ $1 break;
        proxy_pass         http://backend;
        proxy_set_header Authorization "Basic safdadfWU6cdfdvcmQ=";
    }
}

现在我可以将 url 用作 http://IP:PORT/<token>/endpoint 并且应该将其代理为 url 为 http://backend/endpoint

【讨论】:

    猜你喜欢
    • 2021-01-27
    • 2021-01-11
    • 2017-02-23
    • 2019-10-05
    • 2013-01-18
    • 2023-04-01
    • 1970-01-01
    • 2021-09-01
    • 1970-01-01
    相关资源
    最近更新 更多