Sails.js 0.10.x: How to listen on localhost only?

孤街浪徒 提交于 2019-12-05 07:49:18

问题


I would like to pipe all traffic through an NGINX proxy and make sure that the node server won't be accessible directly from the outside.

Node's http module has the ability to listen on a given port on localhost only, is there an option to enable sails.js to do the same?


回答1:


Simply add this line:

config/local.js

explicitHost: process.env.HOST || 'localhost'

Or you could add a policy:

config/policies.js

module.exports.policies = {
  '*': 'isLocal'
}

api/policies/isLocal.coffee

 # sessionAuth
 #
 # @module      :: Policy
 # @description :: Accept only local connections
 # @docs        :: http://sailsjs.org/#!documentation/policies

module.exports = (req, res, cb) ->

  if req.ip is '127.0.0.1' then cb()
  else res.forbidden new Error 'Accept only local connections'



回答2:


Not sure why you want to use Sails to restrict access to only localhost when you're using nginx as a proxy server (nginx is designed to do what you want). You can use an nginx configuration file to restrict local access to your Sails app.

  server {
    listen 80;
    server_name www.yourSailsApp.com;
    ...
    location / {
      allow   127.0.0.1;
      deny    all;
    }
  }

You may need to add your site to your HOSTS file /etc/hosts:

127.0.0.1 yourSailsApp.com

Alternatively, you can just find the public IP of your server and use that in the nginx configuration instead, in the allow field.



来源:https://stackoverflow.com/questions/25585513/sails-js-0-10-x-how-to-listen-on-localhost-only

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!