Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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: 127.0.0.1, 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: 127.0.0.1, 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:
share|improve this question

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_pass http://10.0.0.2; proxy_set_header Host www.example.com; # ..various other required directives omitted }

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

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

Hope this helps.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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