0

I'm having trouble making nginx proxy an url with variable to a service within kubernetes. Url looks like this: http://localhost/user?username=Dave I expect this url to take me to a subpage /user, which will read ?username=Dave and then fetch data from a database. However this takes me to the home page of the application(/ instead of /user) and does not read the variable even though url includes /user?username=Dave. My current nginx config file looks like this:

server {
listen 0.0.0.0:80;
server_name localhost;

location / {
  proxy_pass http://${FLASK_APP}:8080/;
}
location /user {
      proxy_pass http://${GO_APP}:8000/;
  }
}

I have read that location /user will match the url I'm passing. What is wrong with it? Or do I need to add something to proxy_pass http://${GO_APP}:8000/; or location /user?

davidb
  • 1,503
  • 4
  • 30
  • 49
  • Maybe this will help. https://stackoverflow.com/questions/43326913/nginx-proxy-pass-directive-string-interpolation/43341304#43341304 – EternalHour Oct 22 '19 at 20:50
  • I'm not sure how that thread is related to my question? – davidb Oct 22 '19 at 21:04
  • Well you state it "doesn't work." Without an indication, such as an error, not much else to go on. That question looks similar to what you are trying to do. – EternalHour Oct 22 '19 at 21:08
  • Updated my question. Hope it makes more sense now. – davidb Oct 22 '19 at 21:23
  • What does this mean? "this directs me to the home page of the GO_APP and does not even though url includes /user?username=Dave" I feel your question would be better received if you put it in an actual vs. expected format. – EternalHour Oct 22 '19 at 21:42
  • Thanks, I tried to add more information again. Hope it's clearer this time. – davidb Oct 22 '19 at 21:49
  • I see now, have you tried `proxy_pass http://${GO_APP}:8000/user/;`? You may also need to do `proxy_pass http://${GO_APP}:8000/user$is_args;` to get the query string. – EternalHour Oct 22 '19 at 22:05
  • I did and still the same. I changed it to `http://${GO_APP}:8000/user?$args;` and while it gets me to the correct page it still doesn't read the variable from the url. – davidb Oct 22 '19 at 22:12
  • Ok I see the problem, this is the [question](https://stackoverflow.com/questions/8130692/how-can-query-string-parameters-be-forwarded-through-a-proxy-pass-with-nginx) that relates to it. This comes from your using `${GO_APP}` in the `proxy_pass` URI. – EternalHour Oct 22 '19 at 22:17
  • I'm not sure I get it. I'm trying this `location ~* /user?(.*)$ { proxy_pass http://${GO_APP}:8000/user?$1$args; }` and now I'm getting bad gateway. – davidb Oct 22 '19 at 22:25

1 Answers1

0

As noted in the comments, the issue arises because you are using a variable in the proxy_pass target. As also noted in the comments, this question is related. As the answer referencing the docs states:

A special case is using variables in the proxy_pass statement: The requested URL is not used and you are fully responsible to construct the target URL yourself.

This means that you either need to use a static proxy_pass target, such as

// note that I added the forward slash
location /user/ {
    proxy_pass http://destination:8000/;
}

Or as an alternative, I believe you can do it this way also

location /user/ {
    proxy_pass http://${GO_APP}:8000/user$is_args$args;
}
EternalHour
  • 8,308
  • 6
  • 38
  • 57