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

My iOS app is currently accessing domain A via http POST but I would like to forward all requests to domain B.

If I use the usual "rewrite ^/(.*)$ http://mydomain/$1 permanent;" the POST data seems to get lost.

How can I pass HTTP POST data to a different domain using NginX?

share|improve this question

1 Answer 1

up vote 10 down vote accepted

Try using the reverse proxy support instead. An example location section would be:

location / {
  proxy_pass      http://localhost:8080;
  proxy_redirect  http://localhost:8080/ /;
  proxy_read_timeout 60s;

  # May not need or want to set Host. Should default to the above hostname.
  proxy_set_header          Host            $host;
  proxy_set_header          X-Real-IP       $remote_addr;
  proxy_set_header          X-Forwarded-For $proxy_add_x_forwarded_for;
}

This example will pass through all requests to this server block to a second server running on localhost:8080. This preserves POST's and should also preserve other request types too if it ever becomes an issue.

The issue is that external redirects will never resend POST data. This is written into the HTTP spec (check the 3xx section). Any client that does do this is violating the spec.

If the 301/302 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.

I'm fairly sure that most browsers implement this by simply forcing the redirected request to be a GET request. Theoretically, the spec does allow for a browser that would ask the user whether to redirect the POST data, but I'm unaware of any that currently do.

share|improve this answer
    
awsome! works like a charm –  Jiho Kang Sep 16 '11 at 9:22
    
It works. Thank you so much. –  hudolejev Nov 18 '11 at 19:37
    
a little bit confused from comment. Do I generally want to set the proxy_set_header values? Otherwise I guess that remote address becomes the remote address of the proxy server but what I usually want is the remote address of the client, correct? –  George Pligor Mar 2 at 12:24

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.