3

I have a RESTful web service which exposes an interface such as :

  • GET /api/v1/films/:id/thumb
  • PUT /api/v1/films/:id/thumb
  • ...

The Web Server is composed of a nodejs cluster behind a nginx reverse proxy.

I am now trying to configure nginx proxy and client buffers. To this end I set the directives

    location ~ /api/v1/films/(.*)/thumb {
        proxy_buffers 6 500k;
        proxy_busy_buffers_size 1m;
        client_max_body_size 3m;
        client_body_buffer_size 3m;
        proxy_pass http://backend;
    }

This configuration does the job but is unsatisfying since it configures proxy_buffers 3m for the PUT request which is unnecessary and a waste of resources and a client_max_body_size 3m for a GET.

And so I am looking for a way of configuring my routes based on http methods in addition to URIs.

Thanks to everyone willing to share a bit of experience.

M. Timtow
  • 343
  • 1
  • 3
  • 11
  • You can use `map` command to check http method. Check out this (page)[http://nginx.org/en/docs/http/ngx_http_map_module.html] – abeyaz Sep 14 '17 at 11:53

1 Answers1

3

You can map the http method to max body size you want. This should work for example:

map $request_method $max_size {
    default       3m;
    PUT           1m;
    GET           1m;
}

location ~ /api/v1/films/(.*)/thumb {
    proxy_buffers 6 500k;
    proxy_busy_buffers_size 1m;
    client_max_body_size $max_size;
    client_body_buffer_size $max_size;
    proxy_pass http://backend;
}
abeyaz
  • 3,034
  • 1
  • 16
  • 20
  • Thanks, I just tried your suggestion but nginx is calling names `"client_max_body_size" directive invalid value`. I don't understand why, any idea? – M. Timtow Sep 14 '17 at 12:13
  • Can you try to quote values like '1m', '3m' ? – abeyaz Sep 14 '17 at 12:27
  • I still get same error. Do you know how I can control the variable content ? – M. Timtow Sep 14 '17 at 12:31
  • According to the [docs](http://nginx.org/en/docs/http/ngx_http_map_module.html), it has to work. Can you also try quoting variable like '$max_size' ? – abeyaz Sep 14 '17 at 12:32
  • Also can you replace 1m and 3m with 1024 and 3072 as numbers only ? – abeyaz Sep 14 '17 at 12:33
  • Still the same error, I am going to try to write a minimal config file with this example to see if it doesn't come from elsewhere in my config files – M. Timtow Sep 14 '17 at 12:39
  • I tried this : `map $request_method $ms { default 1024; } server { listen 49999; location / { client_max_body_size $ms; return 200; }}` which still doesn't work. My guess now would be that `client_max_body_size` doesn't *wait* for `map` to evaluate the variable and only use the text. It may be bs tho. – M. Timtow Sep 14 '17 at 12:48
  • See my edit please. I also found this https://groups.google.com/forum/#!topic/openresty-en/qgSjFSSDVrg and https://trac.nginx.org/nginx/ticket/586 , which are still open it seems. Briefly it says that `client_max_body_size` doesn't accept variables (maybe OpenResty allows this tho, it isn't concluded). – M. Timtow Sep 14 '17 at 13:02
  • Then your only option is using `if`. – abeyaz Sep 14 '17 at 13:07
  • `"client_max_body_size" directive is not allowed here`. I don't get it nginx+node is a pretty popular setup I think, am I doing something wrong to get these issues? – M. Timtow Sep 14 '17 at 13:10
  • Do you have another idea ? Else, I think i will just go for temp files, `PUT` requests will be very occasional when compared to `GET` after all. – M. Timtow Sep 14 '17 at 13:16
  • I tihnk you just dont need to change client body size. It would be really not neccesary for most cases – abeyaz Sep 14 '17 at 13:34