2

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.

  • 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. nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass – 84104 Apr 16 '15 at 0:15
  • OK, my bad for assuming it to be in the same format as in other places.. – Tero Kilkanen Apr 16 '15 at 0:27
  • 1
    why not just use rewrite ahead of proxypass - that should work around the problem, no? – Droopy4096 Apr 16 '15 at 3: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 8:19
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:/;
  • 1
    I just turned on debugging 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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