SailsJS as API 和 Nginx:限制外部访问

SailsJS as API and Nginx: Restrict external access

我运行在 digitalocean droplet 上使用 SailsJS(MEAN Stack with nginx)。我的所有请求都映射到我的 Angular 前端,除了 /api 上的请求映射到 proxy_pass 端口 1337 上(Sails 运行s)。这个程序工作正常。

现在我想限制对我的 API 的访问,只允许来自我的前端的请求。我已经尝试从我的 nginx 配置中 deny / allow 但这会阻止用户请求本身。我也尝试了几个答案,例如 this,但没有成功。

将对我的 Sails API 的访问限制为本地主机的推荐方法是什么?我想在我的 Droplet 上 运行 多个应用程序,并使用 Sails 作为我的 API,它应该只能由我的 Droplet 中的应用程序访问。

我的 nginx 配置:

upstream sails_server {
    server 127.0.0.1:1337;
    keepalive 64;
}

server {
    server_name domain.com;
    index index.html;

    location / {
        root /opt/domain/build;
        try_files $uri $uri/ /index.html;
    }

    location /api {
        proxy_http_version               1.1;
        proxy_set_header Connection      "";
        proxy_set_header Host            $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP       $remote_addr;
        client_max_body_size             500M;
    }
}

– 提前致谢!

我认为你不能这样做,因为 angular 在你的客户端中运行,所以你需要从所有用户那里获取 IP。您可以使用与 trustes 代理一起使用的简单方法

var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress

或者使用一些更复杂和可信的像 link