Reputation: 23
when making a request to an url from within my nginx the response of this request is always not present. It seems that the request is not sended.
This is my nginx.conf
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$http_clientid"';
access_log logs/access.log main;
server {
listen 8080;
location /token-test {
access_log logs/token.log main;
content_by_lua_block {
local cjson = require "cjson"
local http = require "resty.http"
local httpc = http.new()
local ngx = ngx
local res, err = httpc:request_uri("http://www.google.de", {method="GET"})
if not res then
ngx.say(cjson.encode({message = "Error getting response",status = ngx.HTTP_INTERNAL_SERVER_ERROR }))
return ngx.HTTP_INTERNAL_SERVER_ERROR
end
return ngx.HTTP_OK
}
}
}
}
Im getting a 200 response status with this response body:
{
"status": 500,
"message": "Error getting response"
}
There are not error in the logs.
Why do I get a 200 response instead of 500 and and why does the response body contain the message from not present condition block?
UPDATE
I logged the err:
no resolver defined to resolve \"www.google.de\"
Thanks Danny
Upvotes: 1
Views: 2248
Reputation: 1535
no resolver defined to resolve "www.google.de"
You need to configure the resolver
:
https://github.com/openresty/lua-nginx-module#tcpsockconnect
In case of domain names, this method will use Nginx core's dynamic resolver to parse the domain name without blocking and it is required to configure the resolver directive in the nginx.conf file like this:
resolver 8.8.8.8; # use Google's public DNS nameserver
Why do I get a 200 response instead of 500
You should call ngx.exit
to interrupt execution of the request and return the status code.
Replace
return ngx.HTTP_INTERNAL_SERVER_ERROR
with
ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
Upvotes: 1