Nginx location to index certain html file - nginx

// 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

Related

How to cofigure nginx to serve a pdf file?

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';
}

How to use location with subfolder in Nginx?

There is the following config:
server {
listen 80;
server_name some-site.io;
root /srv/projects/folder/public;
location /view {
try_files $uri /index.html =404;
}
}
In '/srv/projects/folder/public' there is folder view with files like 'app.js', 'app.css' and so on. I want that this config catches requests like 'some-site.io/view/app.js' and returns app.js file from '/srv/projects/folder/public/view' folder; for 'some-site.io/view' requests I'd like to get index file from '/srv/projects/folder/public/view' folder. Right now I get 404 for 'some-site.io/view' request and I don't understand why.

Make only 1 root file accessible, and serve it for all requests

I actually have this working, but I'd like to know if I am doing it the most efficient way, or if there are any improvements I can make to my conf file. Here is what I am attempting to do:
If any file is requested from the root, we should always serve "index.html". No other file should be accessible, and requesting anything else should be treated as if you requested "index.html". Currently I'm using rewrite, but a redirect would be okay too, and possibly preferable.
Any file under "/css" or "/js" can be requested, and requesting files from those directories that don't exist should return a 404.
Here's my current working conf file:
server {
listen 80;
server_name www.example.com;
client_max_body_size 50M;
root /var/www/mysite;
location = /index.html {
}
# map everything in base dir to one file
location ~ ^/[^/]*$ {
rewrite ^/[^/]*$ /index.html;
}
location ~ ^/css/ {
}
location ~ ^/js/ {
}
}
UPDATE
My final conf file, which is both faster under a load test and simpler than the original, is here:
server {
listen 80;
server_name example.com;
root /var/www/register;
location = /index.html {
}
# Default location, request will fallback here if none other
# location block matches
location / {
rewrite ^.*$ /index.html redirect; # 'root' location '/'
}
location /css/ {
}
location /js/ {
}
}
I'm not sure if I got this right or not, but check this answer, you always want to server index.html so it should be the default location location /
server {
server_name example.com www.example.com;
client_max_body_size 50M;
root /var/www/mysite;
index index.html;
location / {
try_files index.html =404;
}
location /(css|js) {
try_files $uri =404;
}
}

Location and document path in nginx

This is my nginx configuration file:
server {
listen 80;
server_name localhost;
location / {
root d:/www;
index index.html index.htm;
}
location /js/api/ {
root D:/workspace/javascript/maplib/;
autoindex on;
}
}
And the directory of the document is like this:
D:/workspace/javascript/maplib
-- v1.0
--main.js
-- v1.1
Now I want to access the v1.0/main.js by http://localhost/js/api/v1.0/main.js.
And it returns a 404 error.
It seems that ngnix will tried to get the file through D:/workspace/javascript/maplib/js/api/v1.0/main.js which does not exist.
It seems that the string path in the location(in the url) must exist at the file system.
How to fix it to meet my requirement?
BTW, there is not only the js but also some other kinds of files like .gif,.png,.html inside the D:/workspace/javascript/maplib/.
Use alias. Ref: http://nginx.org/en/docs/http/ngx_http_core_module.html#alias
That is, replace
root D:/workspace/javascript/maplib/;
by
alias D:/workspace/javascript/maplib/;
Use rewrite inside location /js/api/ for this, like:
rewrite ^/js/api(.*)$ $1;
You can use root with try_files, just add the try_files line
location /js/api/ {
root D:/workspace/javascript/maplib/;
autoindex on;
try_files $uri $uri/ =404;
}

Configure nginx with multiple locations with different root folders on subdomain

I'm looking to serve the root url of a subdomain and directory of a subdomain to two different folders on my server. Here is the simple set-up that I have and is not working...
server {
index index.html index.htm;
server_name test.example.com;
location / {
root /web/test.example.com/www;
}
location /static {
root /web/test.example.com/static;
}
}
In this example going to test.example.com/ would bring the index file in /web/test.example.com/www
and going to test.example.com/static would bring the index file in /web/test.example.com/static
You need to use the alias directive for location /static:
server {
index index.html;
server_name test.example.com;
root /web/test.example.com/www;
location /static/ {
alias /web/test.example.com/static/;
}
}
The nginx wiki explains the difference between root and alias better than I can:
Note that it may look similar to the root directive at first sight, but the document root doesn't change, just the file system path used for the request. The location part of the request is dropped in the request Nginx issues.
Note that root and alias handle trailing slashes differently.
The Location directive system is
Like you want to forward all request which start /static and your data present in /var/www/static
So a simple method is separated last folder from full path , that means
Full path : /var/www/static
Last Path : /static and First path : /var/www
location <lastPath> {
root <FirstPath>;
}
So lets see what you did mistake and what is your solutions
Your Mistake :
location /static {
root /web/test.example.com/static;
}
Your Solutions :
location /static {
root /web/test.example.com;
}
server {
index index.html index.htm;
server_name test.example.com;
location / {
root /web/test.example.com/www;
}
location /static {
root /web/test.example.com;
}
}
https://nginx.org/en/docs/http/ngx_http_core_module.html#root
A little more elaborate example.
Setup: You have a website at example.com and you have a web app at example.com/webapp
...
server {
listen 443 ssl;
server_name example.com;
root /usr/share/nginx/html/website_dir;
index index.html index.htm;
try_files $uri $uri/ /index.html;
location /webapp/ {
alias /usr/share/nginx/html/webapp_dir/;
index index.html index.htm;
try_files $uri $uri/ /webapp/index.html;
}
}
...
I've named webapp_dir and website_dir on purpose. If you have matching names and folders you can use the root directive.
This setup works and is tested with Docker.
NB!!! Be careful with the slashes. Put them exactly as in the example.
If you use this, I will suggest you set up this command too.
location /static/ {
proxy_set_header Host $host/static; // if you change the directory and the browser can't find your path
alias /web/test.example.com/static/;
}
If you want to check two different directories for the same URI use this config:
server {
...
root /var/www/my-site/public/;
...
index index.php index.html index.htm;
...
location / {
root /var/www/old-site/dist/;
try_files $uri $uri/ /index.php$is_args$args;
}
...
}
If Nginx couldn't find file in /var/www/old-site/dist/ directory, then it will try file in /var/www/my-site/public/ directory, but as we said to Nginx to try files with $uri $uri/ /index.php$is_args$args patterns, so Nginx will try /index.php$is_args$args in /var/www/my-site/public/ directory. not $uri
If you want to complete your fallthrough, then replace /index.php$is_args$args with /fallthrough$uri and then add the location /fallthrough { ... } with the alias key to your target directory.
https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#root-inside-location-block

Resources