# needed if your proxy destination specified with domain name instead of IP address
resolver 8.8.8.8;
location /home/ {
proxy_set_header Host HOST1;
# setup other proxied headers if needed
if ($http_referer ~ ^https?://dev.xyz.com/home) {
rewrite ^/home(/.*)$ HOME_PAGE$1 break;
proxy_pass https://HOST1:8080; # this can be specified by IP address
}
}
对于来自dev.xyz.com/home/...(但不是来自dev.xyz.com/any/other/path!)的your_domain.com/home/path/file 的此类配置请求将被代理到https://HOST1:8080/HOME_PAGE/path/file。如果您使用域名而不是 IP 地址指定代理目标,则需要在服务器配置中指定附加参数 resolver。如果您有本地名称服务器,您可以使用本地名称服务器,也可以使用外部名称服务器,例如 Google 公共 DNS (8.8.8.8) 或 ISP 为您提供的 DNS。无论如何,这样的配置会导致额外的 DNS 查找,因此如果可以,请使用 IP 地址指定您的代理目标。
更新
valid_referers 指令还有另一种方法:
# needed if your proxy destination specified with domain name instead of IP address
resolver 8.8.8.8;
location /home/ {
proxy_set_header Host HOST1;
# setup other proxied headers if needed
valid_referers example.com/home;
if ($invalid_referer = "") {
rewrite ^/home(/.*)$ HOME_PAGE$1 break;
proxy_pass https://HOST1:8080; # this can be specified by IP address
}
}
更新@2020.11.11
除了这个答案以某种方式获得了 5 分之外,给定的解决方案的设计非常糟糕(在 location 和嵌套的 if 块中拥有不同的内容处理程序并不是一个好方法;此外,具有如果可能,应避免使用除来自 nginx rewrite module 之外的任何指令的 if 块)并且在早期的 nginx 版本上根本不起作用(当我看到我的一些早期答案时,我想哭)。一个原始的 OP 问题是
逻辑应该如下,但有一些语法错误。
if ($http_origin ~ '^http?://(dev.xyz.com/home)') {
set $flag 'true';
}
if ($flag = 'true') {
location /home/ {
proxy_pass "https://HOST1:8080/HOME PAGE/";
}
}else{
Do Not proxy pass
}
不清楚不代理通过是什么意思。如果它意味着返回一些 HTTP 错误(例如,HTTP 403 Forbidden),可以通过以下配置完成:
location /home/ {
if ($http_referer !~ ^https?://dev.xyz.com/home) {
return 403;
}
rewrite ^/home(/.*)$ HOME_PAGE$1 break;
proxy_set_header Host HOST1;
# setup other proxied headers if needed
proxy_pass https://HOST1:8080; # this can be specified by IP address
}
如果不代理传递的意思是在本地服务请求,解决方法就比较复杂了:
map $http_referer $loc {
~^https?://dev.xyz.com/home loc_proxy;
default loc_local;
}
server {
...
location /home/ {
try_files /dev/null @$loc;
}
location @loc_proxy {
rewrite ^/home(/.*)$ HOME_PAGE$1 break;
proxy_set_header Host HOST1;
# setup other proxied headers if needed
proxy_pass https://HOST1:8080;
}
location @loc_local {
rewrite ^/home(/.*)$ HOME_PAGE$1 break;
root /path/to/required/page;
...
}
try_files /dev/null @the_named_location; 技巧取自 this 优秀答案。
但是,现在编辑后的 OP 的问题说明了不同的要求,这也可以通过 map 指令帮助来实现:
map $http_referer $environment {
~^https?://dev.xyz.com/home Environment1;
default Environment2;
}
server {
...
location /home/ {
rewrite ^/home(/.*)$ HOME_PAGE$1 break;
proxy_set_header Host $environment;
# setup other proxied headers if needed
proxy_pass https://$environment;
}