Here is my server block from nginx.conf, I'm trying to rewrite my requests, which happens fine but my upstream hostname is not being set correctly, it always resolves to the actual IP address, but my API hosted as a virtual host in apache so end point always returns 500, any ideas how I can fix this?

listen 8090;
server_name example.dev.xyz.com;

set $api_path http://example-dev/api;

location ~ ^/api/ {
    rewrite  ^/api/(.*)  /$1  break;
        proxy_pass $api_path;
        proxy_redirect off;
        proxy_set_header Host $server_name ;
        proxy_set_header X-Real-IP $remote_addr ;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

here are the details from nginx logs

    ...top/api-aggregator-master/sandbox/lua/system/sandbox.lua: in function <...top/api-aggregator-master/sandbox/lua/system/sandbox.lua:1> while sending to client, client:, server: example.dev.xyz.com, request: "GET /aggr/conversations HTTP/1.1", host: "localhost:8090"
2013/10/02 09:21:57 [warn] 27711#0: *8 an upstream response is buffered to a temporary file /Users/santthosh.selvadurai/Desktop/api-aggregator-master/sandbox/proxy_temp/4/00/0000000004 while reading upstream, client:, server: example.dev.xyz.com, request: "GET /aggr/conversations HTTP/1.1", subrequest: "/v1/GetTopicPage", upstream: "http://XX.XX.110.48:80/api", host: "localhost:8090"
2013/10/02 09:21:57 [error] 27711#0: *8 lua entry thread aborted: runtime error: ...top/api-aggregator-master/sandbox/lua/system/sandbox.lua:86: attempt to concatenate field 'function_to_call_file' (a nil value)
stack traceback:
coroutine 0:

1 Answer 1


Because you're setting the 'Host' header via proxy_set_header, you can change your proxy_pass directive to be the actual IP address of your backend server, rather than relying on DNS. Take for example;

location / {
  proxy_set_header Host www.example.com;

  # various other required directives omitted

This will initiate a connection to on port 80, sending the Host header of www.example.com. If www.example.com resolved to, it would be the same as:

location / {
  proxy_pass http://www.example.com;

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.