4

Why i can't use variable $user in proxy_pass - like in example below?

server {
    listen 80;
    server_name ~^(?P<user>[a-z|A-Z|0-9|_|-]+)\.example\.net$;
    root /home/$user/webapps/;

    location /app/ {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://unix:/home/$user/webapps/app/run/gunicorn.sock:/;
    }
}

Is this possible to achieve or should i give up?

And this one would be perfect, but it is also not working.

server {
    listen 80;
    server_name ~^(?P<user>[a-z|A-Z|0-9|_|-]+)\.example\.net$;
    root /home/$user/webapps/;

    location ~ ^\/(?P<app>[\w-_]+)(\/.*)?$ {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://unix:/home/$user/webapps/$app/run/gunicorn.sock:/;
    }
}

I read this and that and here but none of them cover proxy_pass through unix socket.

Abc Xyz
  • 608
  • 1
  • 8
  • 16
  • 2
    I'm not 100% sure, but I think the right syntax is `proxy_pass unix:/home...`, that is, your attempt has `http://` extra. – Tero Kilkanen Apr 15 '15 at 23:57
  • @TeroKilkanen The offical documentation disagrees. http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass – 84104 Apr 16 '15 at 00:15
  • OK, my bad for assuming it to be in the same format as in other places.. – Tero Kilkanen Apr 16 '15 at 00:27
  • 1
    why not just use rewrite ahead of proxypass - that should work around the problem, no? – Droopy4096 Apr 16 '15 at 03:33
  • thought rewrite, would only rewrite uri that is at the end `:/;` not the `http://unix:/...:` - i will try to do some rewrites in about 4h from now. Thank you for suggestion. – Abc Xyz Apr 16 '15 at 08:19
  • im thinking n thinking ... and don't think that rewrite is the correct 'tool' for this case, Droopy4096 could you give an example? Right now I'm fall-backing to one conf file for one app for user - but i would like to do it in one conf file, like in example. // weird thing is that, that site is working but it's not useable. – Abc Xyz Apr 16 '15 at 13:45

2 Answers2

1

The proxy pass directives don't see $user and $app as parameter in your case you have to tell him via the $is_args and $args variables like so:

proxy_pass http://unix:/home/$user$is_args$args/webapps/$app$is_args$args/run/gunicorn.sock:/;
skip87
  • 195
  • 2
  • 9
  • 1
    I just turned on [debugging](http://nginx.org/en/docs/debugging_log.html) and i can see that variables are passed to the log `http://unix:/home/user/webapps/app/run/gunicorn.sock` so looks like problem is somewhere else ... i will put my hand on it tomorrow. – Abc Xyz Apr 16 '15 at 16:55
1

Found that 'uri' was the problem not variables, so the correct config should be

proxy_pass http://unix:/home/$user/webapps/$app/run/gunicorn.sock:$request_uri;

i should turn on debugging faster, thank you for support.

I can't check this solution anymore, this is my guess right now - because i went with one conf per app, i will try to unify my config later.

Abc Xyz
  • 608
  • 1
  • 8
  • 16