Nginx:如何拒絕對 proxy_pass 中端點的公共存取?

Nginx:如何拒絕對 proxy_pass 中端點的公共存取?

我有一個子網域app.example.com設定使用 proxy_pass 使用連接埠 4000 上的套接字連接到 phoenix/elixir 後端專案。

在我的專案中,我有一個名為 的端點/admin/auth,我的 PHP 腳本(在example.com, 不是app.example.com) 將發送 HTTPS cURL 貼文到。但是,該/auth端點高度敏感,我只希望本地主機 127.0.0.1 向該端點發送帖子,而不是其他人。

我有以下 Nginx 配置,但它似乎不起作用:

upstream app {
  server 127.0.0.1:4000 max_fails=5 fail_timeout=60s;
}
server {
  server_name app.example.com;

  listen [::]:443 ssl;
  listen 443 ssl;

  location = /admin/auth {
    allow 127.0.0.1;
    deny all;
  }

  location / {
    proxy_http_version 1.1;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-Cluster-Client-Ip $remote_addr;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_pass http://app;
  }
}

我該怎麼做?另外,我怎麼能拒絕公眾存取整個/admin/資料夾,其中包含許多更敏感的端點?

我在 example.com 上的 cURL 腳本在 localhost 127.0.0.1 上運行,看起來像這樣。目前,該腳本不適用於上面的 Nginx 配置,但如果我刪除location = /admin/authNginx 配置中的區塊,它確實可以工作:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://app.example.com/admin/auth");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);

相關內容