How to use location with subfolder in Nginx? - 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.

Related

Nginx location to index certain html file

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

Nginx whenever loading static 404 is returened

I have an image om the disk saved on the path: /usr/volumes/rgd/app/images/image-name.
when ever someone calls to domain.com/img/some-image-name I want to show the image.
when ever someone calls to domain.com/anythingelse I want to show the proxy_pass.
server {
listen 80;
server_name _;
location ~/img/ {
try_files $uri /usr/volumes/rgd/app/images/$uri =404;
}
location / {
proxy_pass http://app:3000;
...
}
}
So domain.com/img/testimg should return the image: /usr/volumes/rgd/app/images/testimg the image is placed at the correct location in the nginx server.
I am getting error 404. the proxy_pass works as expected.
UPDATE
I have fixed it, to:
location ~^/img/(.*)$ {
try_files $uri /usr/volumes/rgd/app/images/$1 =404;
}
$ ls -la /usr/volumes/rgd/app/images/
-rw-r--r-- 1 root root 14717 Dec 26 14:31 /usr/volumes/rgd/app/images/d5085ad47099ed8c08bd
when i access domain.com/img/d5085ad47099ed8c08bd I get 404.
Thanks
You are telling NGINX to check this directory
/usr/volumes/rgd/app/images/img/
instead of this
/usr/volumes/rgd/app/images/
You must extract only part of url after "/img/" using regular expression with capture:
location ~^/img/(.*)$ {
root /usr/volumes/rgd/app;
try_files $uri /images/$1 =404;
}
This tell the NGINX to process all URLs that begins with "/img/" and contains anything after it. Grab the part after "/img/" and chcek this location "usr/volumes/rgd/app/images/my_grabbed_part_from_url".

Nginx - server static files

I'm trying to configure nginx to serve static files (images).
I would like to get images by this URL:
DOMAIN/images/RANDOM_FOLDER_NAME_123/RANDOM_FILE_NAME_456.png
On server I store images as:
/usr/share/nginx/html/RANDOM_FOLDER_NAME_123/IMAGES
/usr/share/nginx/html/RANDOM_FOLDER_NAME_234/IMAGES
/usr/share/nginx/html/RANDOM_FOLDER_NAME_345/IMAGES
/usr/share/nginx/html/RANDOM_FOLDER_NAME_456/IMAGES
I would like to match fe.
DOMAIN/images/RANDOM_FOLDER_NAME_123/RANDOM_FILE_NAME_456.png -> /usr/share/nginx/html/RANDOM_FOLDER_NAME_123/RANDOM_FILE_NAME_456.png
How have I set nginx path to achieve this? I tried to do this by:
location /images/ {
try_files $uri =404;
autoindex off;
}
But I think that problem is images in URL.
You can try
location /images {
alias /usr/share/nginx/html;
}
https://www.techcoil.com/blog/understanding-the-difference-between-the-root-and-alias-directives-in-nginx/

How to serve relative source urls outside root from NGINX

This seems very basic, but I am not able to solve this.
I want NGINX to serve source urls outside the current root, basically starting with ../
This is my directory
common
root (NGINX root)
->index.html (NGINX default index)
common (another common folder)
I want to be able to serve
src="../common/whatever" /*outer common folder*/
src="./common" /*inner common folder; Cannot change unfortunately*/
As you can see, simple location /common will not work.
This is my current NGINX conf,
server
{
listen 83 default_server;
listen [::]:83 default_server ipv6only=on;
root C:/www/root;
index index.html index.htm;
#This does not work for inner /common folder
location /common {
root C:/www;
try_files $uri /index.html;
}
location / {
try_files $uri /index.html;
}
}
Maybe you can do the reverse:
root/
common/*
site/
site/common/*
and do something like
location /common {
try_files site/$uri $uri ...;
}
etc... which gives you $root/site/common/FILE, $root/common/FILE....

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