I need to do proxy_pass to host name passed in url and rewrite url as
well.
Since the host name is difference with each request, I cannot provide an
upstream for it. Below is the nginx configuration I am using but it
doesnt
do proxy pass and returns 404 error. The hostname resembles ec2…com.
On Thu, Oct 31, 2013 at 07:55:15PM -0400, nehay2j wrote:
Hi there,
I need to do proxy_pass to host name passed in url and rewrite url as well.
Since the host name is difference with each request, I cannot provide an
upstream for it. Below is the nginx configuration I am using but it doesnt
do proxy pass and returns 404 error. The hostname resembles ec2…com.
proxy_pass to a dynamic hostname taken from the request url works for
me.
What is an example request that you make that does not do what you want?
What is the proxy_pass line that that request sees, when you replace
the $variables with their values?
If I uncomment ‘proxy_set_header Host’ and ‘proxy_set_header
X-Forwarded-For’ it ends into 404 error. Otherwise it rewrites and
proxies
fine but doesnt pass the POST parameter.
If I uncomment ‘proxy_set_header Host’ and ‘proxy_set_header
X-Forwarded-For’ it ends into 404 error.
I presume that the 404 comes from your upstream server. Use
proxy_set_header to set whatever header values that upstream needs,
to handle the request.
I suspect that “proxy_set_header Host $1;” is what you want – or to
have no proxy_set_header directives at all so that the default applies.
Otherwise it rewrites and proxies
fine but doesnt pass the POST parameter.
It all works fine for me.
Can you provide evidence of where it fails to do what you expect?
Perhaps tcpdump the traffic, and see what does nginx send to upstream?
If
it is the expected correct client request, then you should check what
the upstream does with a similar request that you make using, say, curl.
Provide a specific example of what exactly you do, ideally using “curl”,
and what response you get, and what response you expect. That’s the best
way of making it easier for someone else to help.
Thanks Francis. I could finally see the post parameters at server end.
Setting proxy_set_header Host $1; changes the browser url which we donot
want. Currently, the code looks like-
If i set ‘proxy_set_header Host $http_host;’ to have $proxy_host
instead of
$http_host, all other things work fine other than cookies. Cookies does
not
get transferred. Having $http_host throws 404 error with the following
entry
in log-
2013/11/01 21:05:37 [error] 17515#0: *1 open() “/opt/nginx/html/test”
failed
(2: No such file or directory), client:
10.10.4.167, server: example.com, request: “GET /testHTTP/1.1”, host:
"example.com
", referrer: “http://example.com/test/test.html”
I confess that I don’t see how the configuration you show below can lead
to that response. But I don’t know what upstream is doing, so maybe it
all just works.
Do you get the same response from upstream if you avoid nginx?
Also, are you aware that a HTTP 302 will cause the browser url to
change? Once the browser gets the above response, the next request goes
to marketplace.example.com and not to example.com.
But when I submit post request through browser, I gives me
a 404 with no error in logs.
If there’s nothing in the logs, that suggests that you weren’t talking
to nginx. Or that your nginx logging level is too low for what you are
trying to see.
What happens when you submit a post using curl, so you can see exactly
what happens without the browser getting in the way?
I added rewrite command so that the url doesn’t show IP passed to the
nginx.
Curl gives a 302 because it doesnt have the sessionid with it. If there
is a
session id that is passed to the application running on http://23.23.234.234:8080/test, it will take us to app. I can see in
application logs that jsessionid does not get there and hence it
redirects
to the login page.
If I remove the rewrite command and provides the html page in proxy_pass
i.e. proxy_pass http://:8080$ec2instance/test/test.html, it gets to the
test.html page of the application but does not load any .css .js files
and
says open failed "/opt/nginx/html/test/test.html.
On Sat, Nov 02, 2013 at 10:21:14AM -0400, nehay2j wrote:
Hi there,
I suspect you’ll get better help from someone else.
I’m unable to work out what it is that you want nginx to do.
I added rewrite command so that the url doesn’t show IP passed to the nginx.
What does that mean, in the problem-report format of “I use this
configuration; I make request A; I get response B; but I expect response
C”?
Curl gives a 302 because it doesnt have the sessionid with it. If there is a
session id that is passed to the application running on http://23.23.234.234:8080/test, it will take us to app.
What does “take us to app” mean? The problem report should be in the
same format as above.
What happens when you add the sessionid to the curl request?
(The whole point of using curl is to make it easier to see what is
happening. If it doesn’t make it easier, don’t use curl. Instead, find
some other way of clearly describing the unwanted behaviour that you
see.)
If I remove the rewrite command and provides the html page in proxy_pass
i.e. proxy_pass http://:8080$ec2instance/test/test.html, it gets to the
test.html page of the application but does not load any .css .js files and
says open failed "/opt/nginx/html/test/test.html.
The same problem report format will probably help here too.
Each css or js request is a separate http request, so for each one you
can say “I make request A; I get response B; but I expect response C”.
Which is correct. But when I submit post request through browser, I
gives me
a 404 with no error in logs. Should i be using rewrite command here or
not?
If I remove rewrite command, it give file not found error as it picks
default root path always.
2013/11/01 20:35:08 [error] 17339#0: *11 open()
“/opt/nginx/html/test/main.html” failed (2: No such file or directory
), client: 10.10.4.167, server:example.com, request: “GET
/test/main.html
HTTP/1.1”, host: “example.com”, referrer: http://example.com/test_console/test.html