首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Nginx基于位置块重定向所有不可执行的请求。

Nginx基于位置块重定向所有不可执行的请求。
EN

Server Fault用户
提问于 2022-10-11 10:46:08
回答 1查看 456关注 0票数 0

我正在使用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等等..。服务器{侦听80;charset utf-8;server_name test.com;location = /auth/test{ IP;proxy_pass http://test.com/test/decisions/;proxy_pass_request_body off;proxy_set_header Content-Length '';proxy_set_header X-原始-URI $request_uri;} location = /test/decisions/ { proxy_set_header主机$host;proxy_set_header X-Real $remote_addr;proxy_set_header X转发- $proxy_add_x_forwarded_for;proxy_set_header X转发-Proto $scheme;proxy_set_header升级$http_upgrade;proxy_set_header连接$http_connection;proxy_set_header授权$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;proxy_set_header X-Real $remote_addr;proxy_set_header X转发-For $proxy_add_x_forwarded_for;proxy_set_header X转发-Proto $scheme;proxy_set_header升级$http_upgrade;proxy_set_header Connection $http_connection;proxy_set_header授权$http_authorization;proxy_pass https://test2.com/;proxy_pass_header Server;proxy_http_version 1.1;proxy_redirect默认值;access_log /var/log/nginx/access.log.log;client_max_body_size 10240M;}
EN

回答 1

Server Fault用户

回答已采纳

发布于 2022-10-11 22:54:45

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

  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,而不是指向2.example.com的URL。大多数情况下,web应用程序中有一个可以修改的根URL设置。

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

然而,这可能会有意想不到的副作用。

票数 0
EN
页面原文内容由Server Fault提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://serverfault.com/questions/1112795

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档