I´m trying to serve a pdf file by making a configuration in nginx but I get the following error in the page: 404 Not Found
The configuration is like this:
server {
listen 3002;
index index.html;
server_name _;
location / {
root /var/www/html/pdf_carpet;
try_files $uri /index.html = 404;
}
}
pdf_carpet is where the pdf file is.
What could I do or change to be able to serve a pdf file in nginx?
P.S. It works with html files.
Here is the full location block that should show the PDF file in the browser window under the http://<your_domain_or_IP>:3002/pdf_carpet URL:
location = /pdf_carpet {
alias /var/www/html/pdf_carpet/file.pdf;
default_type application/pdf;
add_header Content-Disposition 'inline';
}
Update
If an URI for accessing the PDF file ends with the slash (or it is a root URI as a special case), the above config would not work since an index file name will be appended to such an URI by the nginx (making location = /path/ { ... } not match the $uri internal nginx variable). For such a case another technique can be used:
location = / {
root /var/www/html/pdf_carpet;
rewrite ^ /file.pdf break;
add_header Content-Disposition 'inline';
}
Related
// conf
server {
listen 80;
location /x {
root /templates;
index x.html;
}
location / {
root /templates;
index index.html;
}
}
//
// Folder
tempalets
| - index.html
| - x.html
I go to url domain.com, it's will show index.html
But, I go to url domain.com/x, it's will show 404 Not Found.
And, I try domain.com/x.html, it's will show x.html.
Why url domain.com/x doen't show x.html?
How could I go to url domain.com/x and show x.html?
I don't want that .html in the url.
Alternative 1:
Use try_files. Below it concatenates the extension (.html), so when requesting /x it first checks if the file /templates/x.html exists, then /templates/x, otherwise it 404s.
server {
listen 80;
location / {
root /templates;
try_files $uri.html $uri =404;
index index.html;
}
}
Alternative 2:
Upload the HTML files without the extension and set the default_type (MIME) to text/html.
default_type 'text/html';
https://blog.uidrafter.com/pretty-routes-for-static-html
I use docker with nginx and this is my app config file:
server {
listen 80;
server_name app.me.site;
return 308 https://$host$uri;
location .well-known {
root /var/www/.well-known;
try_files /$uri /$uri/ /index.html;
}
# other configs
}
The path is /var/www/app.
I also created /var/www/.well-known for Let's Encrypt and it is accessible but it's only accessible for https.
I need to have an if cluse: if URL is app.me.site/.well-known, do not use https.
I tried to reach this but did not find any clue.
Your config is not workable because the return directive is executed at the NGX_HTTP_SERVER_REWRITE_PHASE while proper location selection will be done later at the NGX_HTTP_FIND_CONFIG_PHASE (request processing phases are described in the development guide). To fix it you should move that return directive to the location block (I also suggest to use the $request_uri variable instead the normalized $uri one):
location / {
# everything not started with the '/.well-known/' prefix will be processed here
return 308 https://$host$request_uri;
}
location /.well-known/ {
# do not append '/.well-known' suffix here!
# (see the difference between 'root' and 'alias' directives)
root /var/www;
try_files $uri =404;
}
I have my domain config file setup for Nginx. I have set domain like www.example.com and I have all sub directory also inside that like basefolder/index.html, basefolder/abc1/signup/index.html, basefolder/abc2/signup/index.html, basefolder/signup/index.html etc as n number can be exists.
I can directly call them through domain/dir/ which will call the index.html file available in that directory. I have some specific query string set for the "signup" folders only like "www.example.com/signup/?query=a12seddf" or "www.example.com/abc1/signup/?query=a12seddf". I want to covert them to the path like "www.example.com/signup/a12seddf" or "www.example.com/abc1/signup/a12seddf". I tried with the rewrite rule but it works only if I give the whole location till the signup but I want to write the single rule which will work on all signup folder instead of writing the rewrite rule for all.
I tried below code but it won't work for me and giving 404 on accessing the beautify URL.
location ~* signup/ {
rewrite ^/signup/([a-z0-9A-Z]+)/?$ /signup/?query=$1 break;
}
Please find the whole sample working config file given below with names example.com.conf in nginx/sites-enabled folder:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/html/sites/example.com;
error_page 404 /error_404.html;
location = /error_404.html {
root /var/www/html;
internal;
}
location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
expires 30d;
add_header Pragma public;
add_header Cache-Control "public";
}
rewrite ^/abc1/signup/(.*)$ /abc1/signup/index.html?_l=$1 last;
rewrite ^/signup/(.*)$ /signup/index.html?_l=$1 last;
}
Thanks.
I want to configure nginx to server HTML files for viewing instead of downloading.
server {
listen 5000;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#location / {
# root html;
# index index.html index.htm;
#}
location = / {
root /home/vagrant/own/base/assets;
index index.html;
}
location = /login {
# root /home/vagrant/own/base/assets;
alias /home/vagrant/own/base/assets/login.html;
}
location /index.html {
root /home/vagrant/own/base/assets;
}
}
This is my conf file, when I browse to /login, my browser tries to offer the file for download instead of viewing it. Thanks for your help.
location = /login {
default_type "text/html";
alias /home/vagrant/own/base/assets/login.html;
}
I think the approach above is effective, there maybe other answers.
just set new "types" for location:
location /saveonly/ {
types { application/octet-stream html; }
...
}
Just in case someone can benefit from my mistake - I had deleted the default server from the nginx sites-enabled area and no matter what I did, the browser would download the index.html file instead of opening it. It made no difference what I did with mime types in the server block files. After finding a replacement copy online, and installing it in the sites-enabled directory, and restarting nginx, the problem went away.
I currently have 2 image locations and they may the formats (jpg,jpeg,png,gif)
i.domain.com/simage.jpg thumbnail
i.domain.com/image.jpg high quality
i.domain.com/o/image.jpg full resolution
Does anyone know how I can force image files in /o/ to download rather than render in the web browser?
Here's my conf file: http://pastebin.com/dP8kZMzx
#setup subdomain i.domain.com
server {
server_name i.domain.com;
access_log /var/log/nginx/i..com.access.log;
error_log /var/log/nginx/i.domain.com.error.log;
root /var/www/domain.com/test1/images;
index index.php index.html index.htm;
#error_page 403 = /notfound.jpg;
#error_page 500 = /notfound.jpg;
location / {
#change this to a 404 img file .jpg
try_files $uri $uri/ /notfound.jpg;
rewrite "/s([A-Za-z0-9.]+)?" /small/$1 break;
rewrite "/o/([A-Za-z0-9.]+)?" /orig/$1 break;
rewrite "/([A-Za-z0-9.]+)?" /medium/$1 break;
}
}
You just need to return HTTP header Content-disposition:
location ~* /orig/(.+\.jpg)$ {
add_header Content-disposition "attachment; filename=$1";
}
I have been trying to get this functionality into my own nginx server.
I have a folder on server that I would like to have open for anyone who knows the path.
To do that I added a /file/ location into my nginx config file.
server {
listen 80;
server_name example.com;
location /file/ {
alias /var/openStuff/file/;
add_header Content-disposition "attachment";
}
}
Any request to example.com/file/x will go to /var/openStuff/file and look for x and force the browser to download the file
This will also work if the path is /file/stuff/things.txt and will serve it from /var/openStuff/file/stuff/things.txt
Below config works for me.
server {
...
# Django media
location /media {
alias /var/www/media;
types { application/octet-stream .pdf; }
default_type application/octet-stream;
}
...
}
But the pdf file needs to ends with lowercase pdf for download to work. I still don't know the syntax to add both .pdf and .PDF to above config file. Any suggestion?
The answer is based on info from http://210mike.com/force-file-download-nginx-apache/
A little mixing did the job for me. Forced to download a CRX file instead of installing or displaying.
## Download crx
location ~* \.crx$ {
add_header Content-disposition "attachment; filename=$1";
}