Nginx serve a file without the filename in the adress bar - nginx

Is the following behaviour possible only with nginx directives?
I want to visit the page:
https://example.com/xyz
When hitting enter in the adressbar I want an immediate download of the file abc.tst to be presented to the user but the adressbar should not change, it should "stay" on https://example.com/xyz and not go to https://example.com/abc.tst and the name of the downloaded file should not change to xyz.
I have experimented with various rewrite rules, or locations but can't seem to figure it out...

You need to use the Content-Disposition HTTP header to speficy filename different from xyz:
location = /xyz {
alias /path/abc.tst;
add_header Content-Disposition 'attachment; filename="abc.tst"';
}
You may also need to specify MIME type for this file, use default_type directive, for generic binary data try
default_type application/octet-stream;

Related

default_type application/octet-stream in nginx.conf file

In the default nginx configuration file i see that the default_type is set to application/octet-stream. I understand the MIME types but I do not understand why we are setting a default type. What is the significance of this configuraion? Can someone help me to understand this?
include /etc/nginx/mime.types;
default_type application/octet-stream;
The default_type only applies to file extensions that have not been defined in the mime.types file.
If the file extension is missing from the mime.types file, it's fairly safe to assume application/octet-stream, which most browsers will treat as a binary file and download it rather than attempting to render it.
The mime.types file is simply a types directive with a long list of common MIME types and their associated file extension(s).
See this document for details.

Nginx fancy-index header and footer never load

I'm creating a Nginx file server, and I'm trying to enable the fancy-index module to get have custom header and footer, but I can't get it working, the header/footer never load. (The request isn't even done from the browser).
For now, I've followed this tutorial : https://neilmenon.com/blog/install-nginx-fancyindex
My current config for the site is
server {
listen 80;
server_name myname;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
location / {
root /var/www/html
fancyindex on;
fancyindex_exact_size off;
fancyindex_footer /fancy-index/footer.html;
fancyindex_header /fancy-index/header.html;
fancyindex_css_href /fancy-index/style.css;
fancyindex_time_format "%B %e, %Y";
}
}
I've also loaded the module in the nginx.conf on the first line of the file
load_module /usr/share/nginx/modules/ngx_http_fancyindex_module.so;
I also clarify that I am new to nginx, so I apologize if this is a common issue that I should be aware of.
Thanks in advance, any help would be much appreciated
I wasn't able to find a solution to use fancy index however, I've got a workaround by using the module ngx_http_addition_module on which fancy index is based.
This module is here : https://nginx.org/en/docs/http/ngx_http_addition_module.html
Basically, the configuration goes as follows :
location / {
root /var/www/html
addition_types text/html; # Replace this with watever mime type this server is responding
add_before_body /fancy-index/header.html; # Replace the fancyindex_header
add_after_body /fancy-index/footer.html; # Replace the fancyindex_footer
}
You don't have the possibility to link a stylesheet from these directives or changes the time format, but nothing prevent to load a stylesheet from the header and adding a script in it for the time.
I had the same problem. The issue is coming from the fact that you enable autoindex.
To fix the issue you need to comment the line that reference autoindex

Nginx force download mp3 files

Inside /var/www/storage/ folder located only mp3 files. What I want to do is, to make every request to those files to end up with download rather than playing in browser.
My current config looks like this, I can't figure out what is wrong.
server {
charset utf-8;
client_max_body_size 128M;
listen 80; ## listen for ipv4
server_name dl.domain.com;
root /var/www/storage/;
location / {
add_header Content-Disposition: "$request_filename";
}
}
Your initial configuration is almost correct, all you are forgetting is the "attachment":
add_header Content-Disposition "attachment; filename=$request_filename";
It's also recommended that you do the application/octet-stream as suggested by raven428. However I believe you will get the "save as" in most UAs without adjusting the Content-Type.
Per RFC 2616 sec19.5.1 :
If this header is used in a response with the application/octet- stream content-type, the implied suggestion is that the user agent should not display the response, but directly enter a `save response as...' dialog.
browser decision to play or download depends on content-type header. if you want to force browser download file instead of play, your webserver should return Content-Type: application/octet-stream for mp3 files:
location ~ /mp3folder/.+\.mp3$ {
types {
application/octet-stream;
}
}

nginx file upload with client_body_in_file_only

Good evening,
I need to upload static content to nginx server 1.9 (so upload module didn't work with this version). I've read the article "Nginx direct file upload without passing them through backend" and followed instructions step by step. Everything works for me, except file names at the nginx data directory. File names look like '0000000001', '0061565403' and so on. What should I do to save files with their correct names?
Here is my nginx location config:
location /upload {
limit_except POST { deny all; }
client_body_temp_path /data/;
client_body_in_file_only on;
client_body_buffer_size 128K;
client_max_body_size 50M;
proxy_pass_request_headers on;
proxy_set_header content-type "text/html";
proxy_set_body $request_body_file;
proxy_pass http://localhost:8080/
proxy_redirect off;}
You can use HTTP header in the client to pass the correct name (whatever that is), e.g.:
Correct-Filename: my-correct-filename
And since you're using proxy_pass_request_headers on, the header is visible in the back end where you can use it when saving the file. However when using headers the filename is limited to using ASCII characters, see this answer.
The only way I have been able to do this is to send the original filename as a parameter (I use JS to copy the filename to a hidden field), and then, on the server, if I am storing the temp file to our file system, I use that parameter to rename the file in the process of saving it to its "proper" location.
Not beautiful, but it works.

best way to save nginx request as a file?

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.)
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
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.

Resources