[英]NGINX as proxy of Node.js
I'm using NODE.js behind NGINX server, this is my Nginx configuration: 我在NGINX服务器后面使用NODE.js,这是我的Nginx配置:
upstream example.it {
server 127.0.0.1:8000;
}
server {
server_name www.example.it;
location / {
proxy_pass http://example.it;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
All works good, the requests are correctly sent from nginx to node BUT I saw a problem in the log file generated from Express.js. 一切正常,请求从nginx正确发送到节点但是我发现从Express.js生成的日志文件中存在问题。
The problem is that ALL THE REQUESTS are saved as done from 127.0.0.1, why? 问题是所有请求都保存为127.0.0.1,为什么?
I don't seen any remove hosts (the real ip of who made the request). 我没有看到任何删除主机(谁提出请求的真实IP)。
Thanks 谢谢
Assuming you're using Express 3.0, a better way to do this is through the trust proxy
setting. 假设您使用的是Express 3.0,更好的方法是通过
trust proxy
设置。
From the Express documentation : 从Express文档 :
trust proxy
Enables reverse proxy support, disabled by defaulttrust proxy
启用反向代理支持,默认情况下禁用
In order to use it: 为了使用它:
app.set('trust proxy', true);
app.use(express.logger('default'));
This has the added advantage of working correctly when you're using a proxy as when you're not (for example in a development environment). 当您使用代理时(例如在开发环境中),当您使用代理时,这具有正常工作的附加优势。
That's correct, as nginx will be the remote host. 这是正确的,因为nginx将是远程主机。 You need to specify a custom log format to log the
X-Forwarded-For
header, see the connect logger documentation . 您需要指定自定义日志格式以记录
X-Forwarded-For
标头,请参阅connect logger文档 。
app.use(express.logger(':req[X-Forwarded-For] - - [:date] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.