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 / 问题 / 586586
Accepted
robjohncox
robjohncox
Asked: 2014-04-04 16:49:39 +0800 CST2014-04-04 16:49:39 +0800 CST 2014-04-04 16:49:39 +0800 CST

Nginx 通过代理重定向、重写和保留 URL

  • 772

在 Nginx 中,我们一直在尝试重定向 URL,如下所示:

http://example.com/some/path -> http://192.168.1.24

用户仍然可以在浏览器中看到原始 URL。一旦用户被重定向,假设他们点击链接到/section/index.html,我们希望它发出一个导致重定向的请求

http://example.com/some/path/section/index.html -> http://192.168.1.24/section/index.html

并再次保留原始 URL。

我们的尝试涉及使用代理和重写规则的各种解决方案,下面显示了使我们最接近解决方案的配置(请注意,这是 Web 服务器的 Web 服务器配置example.com)。但是,这样做仍然存在两个问题:

  • 它无法正确执行重写,因为 Web 服务器接收到的请求 URLhttp://192.168.1.24包含/some/path并因此无法提供所需的页面。
  • 提供页面后,当您将鼠标悬停在链接上时,/some/pathURL 中缺少

    server {
        listen          80;
        server_name     www.example.com;
    
        location /some/path/ {
            proxy_pass http://192.168.1.24;
            proxy_redirect http://www.example.com/some/path http://192.168.1.24;
            proxy_set_header Host $host;
        }
    
        location / {
            index index.html;
            root  /var/www/example.com/htdocs;
        }
    }
    

我们正在寻找一种仅涉及更改example.com. 我们可以更改192.168.1.24(也包括 Nginx)上的配置,但是我们想尝试避免这种情况,因为我们需要为数百个通过代理访问的不同服务器重复此设置example.com。

nginx
  • 4 4 个回答
  • 298603 Views

4 个回答

  • Voted
  1. Alexey Ten
    2014-04-04T22:17:15+08:002014-04-04T22:17:15+08:00

    proxy_pass您应该在指令中使用 URI 部分。此外,您混淆了proxy_redirect指令的顺序参数,可能根本不需要它。Nginx 对该指令有合理的默认值。

    在这种情况下,您的location块可能非常简单:

    location /some/path/ {
        proxy_pass http://192.168.1.24/;
        # note this slash  -----------^
        proxy_set_header Host $host;
    }
    
    • 79
  2. Best Answer
    Tero Kilkanen
    2014-04-04T21:24:31+08:002014-04-04T21:24:31+08:00

    首先,您不应该root在 location 块中使用指令,这是一种不好的做法。在这种情况下,这并不重要。

    尝试添加第二个位置块:

    location ~ /some/path/(?<section>.+)/index.html {
        proxy_pass http://192.168.1.24/$section/index.html;
        proxy_set_header Host $host;
    }
    

    这会将 /some/path/ 之后和 index.html 之前的部分捕获到 $section 变量,然后用于设置 proxy_pass 目标。如果需要,您可以使正则表达式更具体。

    • 67
  3. cnst
    2017-08-27T19:42:44+08:002017-08-27T19:42:44+08:00

    /some/path/您可以使用以下配置在前端和后端之间进行 100% 无缝映射/。

    请注意,这是迄今为止唯一可以无缝处理生成404 Not Found错误的绝对路径的答案,前提Referer是浏览器发送了正确的 HTTP 标头,因此,所有这些 gif 应该继续加载,而无需修改底层 HTML (这不仅昂贵,而且如果没有默认未编译的附加模块也不支持)。

    location /some/path/ {
        proxy_pass http://192.168.1.24/; # note the trailing slash!
    }
    location / {
        error_page 404 = @404;
        return 404; # this would normally be `try_files` first
    }
    location @404 {
        add_header Vary Referer; # sadly, no effect on 404
        if ($http_referer ~ ://[^/]*(/some/path|/the/other)/) {
            return 302 $1$uri;
        }
        return 404 "Not Found\n";
    }
    

    您可以在https://github.com/cnst/StackOverflow.cnst.nginx.conf存储库中找到完整的概念验证和最小可行产品。

    这是一个测试运行,以确认所有边缘情况似乎都有效:

    curl -v -H 'Referer: http://example.su/some/path/page.html' localhost:6586/and/more.gif | & fgrep -e HTTP/ -e Referer -e Location
    > GET /and/more.gif HTTP/1.1
    > Referer: http://example.su/some/path/page.html
    < HTTP/1.1 302 Moved Temporarily
    < Location: http://localhost:6586/some/path/and/more.gif
    < Vary: Referer
    
    curl -v localhost:6586/and/more.gif | & fgrep -e HTTP/ -e Referer -e Location
    > GET /and/more.gif HTTP/1.1
    < HTTP/1.1 404 Not Found
    
    curl -v localhost:6586/some/path/and/more.gif | & fgrep -e HTTP/ -e Referer -e Location -e uri
    > GET /some/path/and/more.gif HTTP/1.1
    < HTTP/1.1 200 OK
    request_uri:    /and/more.gif
    

    PS 如果您有很多不同的映射路径,那么您可能希望使用基于全局的指令,而不是对$http_refererwithin 进行if正则表达式比较。location @404map

    另请注意,根据相关答案proxy_pass, 以及location它所包含的尾部斜杠都非常重要。

    参考:

    • http://nginx.org/r/location
    • http://nginx.org/r/error_page
    • http://nginx.org/r/if
    • http://nginx.org/r/return
    • http://nginx.org/r/add_header
    • 6
  4. tomdunn
    2017-05-05T07:44:12+08:002017-05-05T07:44:12+08:00

    当该斜杠添加到 nginx 代理 jenkins 时,您会看到“您的反向代理设置似乎已损坏”错误。

    proxy_pass          http://localhost:8080/;
    
    Remove this -----------------------------^
    

    它应该读

    proxy_pass          http://localhost:8080;
    
    • 2

相关问题

  • 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

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve