我基本上有一个内部url,它映射到/etc/hosts文件中的一个ip地址。当我在url上执行ping操作时,会返回正确的内部ip地址。当我依赖request节点模块时,就会出现问题:
/etc/hosts:
123.123.123.123 fakeurl.comapp.js:
403错误:
var request = require('request');
request('http://fakeurl.com/', function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the page.
});works 200代码:
var request = require('request');
request('http://123.123.123.123/', function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the page.
});有没有办法在节点应用程序中强制dns映射?
发布于 2017-04-19 02:49:25
节点(dns.lookup())使用的默认DNS解析方法使用系统解析器,它几乎总是将/etc/hosts考虑在内。
这里的区别与DNS解析本身无关,但最有可能的是与用于HTTP Host字段的值有关。在第一个请求中,Host: fakeurl.com将被发送到位于123.123.123.123的HTTP服务器,而在第二个请求中,Host: 123.123.123.123将被发送到位于123.123.123.123的HTTP服务器。服务器可能会根据它们的配置对这两个请求进行不同的解释。
因此,如果您希望使用IP地址作为HTTP Host报头字段值,则需要手动解析地址。例如:
require('dns').lookup('fakeurl.com', (err, ip) => {
if (err) throw err;
request(`http://${ip}/`, (error, response, body) => {
// ...
});
});https://stackoverflow.com/questions/43479020
复制相似问题