Wednesday, May 24, 2017

best way to save nginx request as a file?

Leave a Comment

i am looking for a solution to save data sent via http (e.g. as a POST) as quickly as possible (with lowest overhead) via nginx (v1.2.9). i tried the following nginx configuration, but am not seeing any files written in the directory:

server {   listen 9199;   location /saveme {     client_body_in_file_only on;     client_body_temp_path /tmp/bodies;   } } 

what am i doing wrong? and/or is there a better way to accomplish this? (the data that is written should ideally be one file per request, and it does not matter if it is fairly "raw" in nature. post-processing of the files will be done via a separate process via a queue.)

2 Answers

Answers 1

This question has already been answered here:

Basically, you need to combine log_format and fastcgi_pass. You can then use the access_log directive for example, to specify where the saved variable should be dumped to.

location = /saveme {   log_format postdata $request_body;   access_log  /var/log/nginx/postdata.log  postdata;   fastcgi_pass php_cgi; } 

It could also work with your method but I think you're missing client_body_buffer_size and `client_max_body_size

Answers 2

Do you mean save cache for HTTP post while someone access and request file and store on hdd rather than memory? I may suggest use proxy_cache_path and proxy_cache. The proxy_cache_path directive sets the path and configuration of the cache, and the proxy_cache directive activates it.

proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m max_size=10g                  inactive=60m use_temp_path=off; server { ...     location / {         proxy_cache my_cache;         proxy_pass http://my_upstream;     } } 
  • The local disk directory for the cache is called /path/to/cache
  • levels sets up a two‑level directory hierarchy under /path/to/cache/
  • keys_zone sets up a shared memory zone for storing the cache keys and metadata such as usage timers
  • max_size sets the upper limit of the size of the cache
  • inactive specifies how long an item can remain in the cache without being accessed

    the proxy_cache directive activates caching of all content that matches the URL of the parent location block (in the example, /). You can also include the proxy_cache directive in a server block; it applies to all location blocks for the server that don’t have their own proxy_cache directive.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment