将具有特定路径的请求重定向到本地主机或其他远程地址

Redirect requests with specific path to localhost or other remote address

我想在加载站点时在本地加载文件 a.js(例如 example.com)。通常我可以将 /etc/hosts 更改为将 example.com 指向 127.0.1.1 但我 不想 只加载文件 [=11] =].

更好地解释了

我要:

example.com/a.js ---> localhost/a.js
example.com/b.js ---> example.com/b.js

这样做的一种方法(不是最快的)是在您的服务器配置中引入代理传递(下图显示了 nginx,但可以使用 apache 或其他):

  1. 将您的/etc/hosts更改为将目标域名(问题中的example.com)重定向到127.0.0.1
  2. 在您的 nginx 配置中引入两个代理通道:

    我。特定文件 (a.js) 到本地文件的代理传递。

    二。所有其他路径的代理传递回到目标域的远程 IP (example.com)。此代理通行证需要是 IP 地址(可以使用 nslookup example.com 获得),因为在我们在步骤 1 中设置主机时,使用域 example.com 将被阻止。

        server {
                listen 80;
                server_name example.com;
    
                location /a.js {
                        # your local server
                        proxy_pass http://localhost:80/; 
                        proxy_set_header Host      $host;
                        proxy_set_header X-Real-IP $remote_addr;
                }
    
                location / {
                        # everything else back to the IP of example.com
                        proxy_pass http://<REMOTE_IP>/; 
                        proxy_set_header Host      $host;
                        proxy_set_header X-Real-IP $remote_addr;
                }
            }