I have tried with host.docker.internal
and it works fine inside default.conf. However if I use it inside njs script file (using js-content) it is not working.
Here is my default.config file and njs script file:
server {
listen 80;
server_name localhost; # Replace with your domain name or IP address
location /api1 {
js_content scripts.apiRouter;
}
location /api2 {
proxy_pass http://host.docker.internal:5055/WeatherForecast;
}
}
function apiRouter(r) {
ngx.fetch('http://host.docker.internal:5055/WeatherForecast') // Pass the URL as a string parameter
.then(function(response) {
if (response.ok) {
return response.text();
} else {
throw new Error('API request failed with status: ' + response.status);
}
})
.then(function(body) {
ngx.log(ngx.ERR, body); // Log the response data to the error log
})
.catch(function(error) {
ngx.log(ngx.ERR, 'Error: ' + error.message); // Log the error message to the error log
});
}
export default { apiRouter };
/api2 is working fine but accessing api api1 gives following error:
unable to find host host.docker.internal.
Things I have tried already, I have make my API accessible(172.17.0.1) with http://172.17.0.1:5055/WeatherForecast.However it gives following error,
2023-07-11 00:25:00 2023/07/10 18:55:00 [error] 141#141: *11 recv() failed (111: Connection refused), client: 172.17.0.1, server: localhost, request: "GET /api HTTP/1.1", host: "localhost:8008" 2023-07-11 00:25:00 2023/07/10 18:55:00 [error] 141#141: *11 js: Error: connect failed.
Please note my container also in same subnet (172.17.0);
"Networks": {
"bridge": {
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"DriverOpts": null
Please help me resolving this issue.