Writing to log file based on path in Nginx - nginx

I want to create an HTTP nginx server which will take whatever POST request comes in
/some/path
will append the post body to some_internal_path/some/path.log
Where some and path would be variables. So /another/place would append to some_internal_path/another/place.log
How can I do this with nginx?

Related

nginx overwrite custom response headers

I have an API endpoint that returns a PDF file on the header in my local using apache.
return of request:
The header response are created with the package mpdf/mpdf and apache in my local environment don't overwrite. But, when a do a deploy, the remote server is nginx, and overwrite all the headers that was setup from mpdf.
Nginx header response:
I searched and see this solution: nginx sets the wrong Content-Type header for dynamically generated files
But, at this moment, i can't perform a change on production config file of nginx.
I need to know if has another solution for this problem.

Sending actual destination path from nginx module to backend

For uploading a file to a server I am using nginx upload module. What I understood from the docs is that it saves the uploaded file to a temporary directory and only provides information about the file to backend(python bottle server in my case) via POST and information that should be passed to backend can be specified by upload_set_form_field directive.
How can I send the actual destination path to backend ,Since nginx will store in some temporary path and not to the path where the uploading was meant to?
Found out the solution. Should set upload_pass_form_field directive value in nginx config to regex pattern matching destination path field name.

Parse url to get a string in Nginx

I have a web server sitting behind Nginx. If there is an error, then I want to fetch some information from the url and pass it on to a static file as parameters. I have configured Nginx to fetch the url parameters from the url using $arg_param_name. However, I also need to fetch a String from the url path itself. So if the url is as below:
www.website.com/path1/path2?arg1=val&arg2=someval
Now, how can I parse this url to fetch the last path (path2 in this case)? My location directive is as below:
location ~*/path1/{
...
}
The url need not always have the same number of paths. It can also have 3 paths. So I can't use $1, $2 etc. I need to fetch the last path, i.e the path which is immediately followed by the url parameters (the ? symbol).

HTTP PUT for new file creation and directory creation

How would a HTTP Server differentiate whether the request in PUT is for a folder creation or a file creation in a directory.
For HTTP GET what I understood is, if the URL has a trailing /, then HTTP Server looks out for a folder with that name and, if does not exist can look out for a file.
How does this work for PUT for a new file and folder creation?
HTTP (the protocol) doesn't have any concept of files or folders. URIs are opaque, except when a relative URI reference is resolved against a base URI.
If you want your server to provide file/folder services, you may want to look into WebDAV (RFC 4918).

How to make nginx(proxy) cache the actual content instead of just the headers when a http 302/301 response is received by the proxy_pass server?

I'm trying to use nginx as a temporary http cache in order to minimize requests to content. My content is on multiple servers so I can't use a static proxy_pass parameter to the direct location but instead of that I use a rewrite to a php script:
rewrite /([^/]+\.jpg) /index.php?file=$1 break;
proxy_pass http://www.phpserver.com;
The php script(that would be http://www.phpserver.com/index.php) then returns a redirect with http code 301 to the actual file location(like http://www.contentserver1.com/filepath/file.jpg).
The problem is that nginx returns the redirect headers instead of retrieving,caching and returning the actual content.
So how do I make it to get the content from the actual server instead of just caching the headers?
Nginx can work only as proxy. It doesn't know anything about logic of you application (site), it's just proxies requests, and can put to cache responses.
For make this schema work, you must remove rewrite section from nginx, and move this logic to phpserver.com. phpserver must download this file and output it to nginx. Even if it very hard operation, nginx would cache this response and when next request will be received, nginx will give response direct from his cache.

Resources