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 / 问题 / 1112795
Accepted
Reza Kazemi
Reza Kazemi
Asked: 2022-10-12 02:46:08 +0800 CST2022-10-12 02:46:08 +0800 CST 2022-10-12 02:46:08 +0800 CST

Nginx 根据位置块重定向所有传入的请求

  • 772

我使用 Nginx 作为反向代理。我假设我有一个像 test.com 这样的域和一个像 /test 这样的路径的位置块。该位置将 proxy_pass 到另一个网站,例如http://test2.com。当我在浏览器上键入 URL 时,它会将第一个请求重定向到应用程序(http://test.com/test/ --> http://test2.com/)。但是,所有传入的请求都将在没有我在 Nginx 上定义的位置的情况下发送。如何使所有传入请求都遵循该路径?我想要这样的东西:

  • 第一个请求:http ://test.com/test/ --> http://test.com

  • 传入请求:http ://test.com/test/statifile.js

  • http://test.com/test/api/something等等...

    server {
     listen 80;
     charset utf-8;
     server_name test.com;
     location = /auth/test{
         internal;
         proxy_pass http://test.com/test/decisions/;
         proxy_pass_request_body off;
         proxy_set_header Content-Length '';
         proxy_set_header X-Original-URI $request_uri;
     }
     location = /test/decisions/ {
         proxy_set_header Host $host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_set_header X-Forwarded-Proto $scheme;
         proxy_set_header Upgrade $http_upgrade;
         proxy_set_header Connection $http_connection;
         proxy_set_header Authorization $http_authorization;
         proxy_pass http://valid.domain/test/api/authorization;
     }
     location /test/ {
         auth_request /auth/test;
         auth_request_set $auth_status $upstream_status;
         proxy_set_header Host $host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_set_header X-Forwarded-Proto $scheme;
         proxy_set_header Upgrade $http_upgrade;
         proxy_set_header Connection $http_connection;
         proxy_set_header Authorization $http_authorization;
         proxy_pass https://test2.com/;
         proxy_pass_header Server;
         proxy_http_version 1.1;
         proxy_redirect default;
         access_log /var/log/nginx/access.log;
         client_max_body_size 10240M;
     }
    }
    
nginx nginx-reverse-proxy
  • 1 1 个回答
  • 21 Views

1 个回答

  • Voted
  1. Best Answer
    Tero Kilkanen
    2022-10-12T14:54:45+08:002022-10-12T14:54:45+08:00

    术语“传入请求”在这里令人困惑。我假设你的意思是:

    1. 浏览器向 发出请求http://example.com/test/,由 nginx 代理到http://2.example.com/。
    2. 浏览器直接发出后续请求http://2.example.com/statfile.js,当您希望发出请求时http://example.com/test/statfile.js

    有了这个问题陈述,最佳解决方案是修改2.example.com服务器配置,以便它创建指向example.com/test的 URL,而不是指向的 URL 2.example.com。大多数情况下,Web 应用程序中有一个可以修改的根 URL 设置。

    不太可靠和性能更差的选项是使用sub_filter模块替换块中所有出现的2.example.comwith 。example.com/test/proxy_pass

    但是,这可能会产生意想不到的副作用。

    • 0

相关问题

  • 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