Nginx whenever loading static 404 is returened - nginx

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

Related

nginx if host true with location and root

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

Nginx sites-available configuration

So, I emigrate from Apache to Nginx, and it is a terrible experience for me.
Right now I need your help with understanding of Nginx file of configuration:
server {
....
location / {
try_files $uri $uri/ $uri.html $uri.php$is_args$args;
}
location ~ "^/([-0-9a-zA-Z_\s]+)$" {
try_files $uri $uri/ /user/user?username=$1;
}
location ~ "^/trends/([.*]+)$" {
try_files $uri $uri/ /trends/trend?name=$1;
}
}
When I type to the URL bar site https://example.com/someuser ,
it successfully redirect me (show to me the folder) to the folder example.com/user/user?username=someuser .
But when I trying to access to https://example.com/trends/sometrend
Nginx load me the page from the fist block.
Why it do this?
Thanks for your help.
Sorry, for my late reply, but I suppose that it will be helpful for others, who will face with similar problem:
server {
...
# enable utf8
charset UTF-8;
# pretty urls
# user folder
location ~ "^/([-0-9a-zA-Z_\s]+)$" {
try_files $uri $uri/ /user/user?username=$1;
}
# trends
location ~ "^/trends/([\x00-\xff]+)$" {
try_files $uri $uri/ /trnds/trend?name=$1;
}
...
}
Let me explain it in details.
\x00-\xff - it allow all symbols in Unicode format, don't forget to enable UTF-8 with charset UTF-8; in the server block.
/trnds/ - on official documentation to Nginx (cant find it now to post the link) they have a little explain that this folder shouldn't exist in the server if we have the same address in the URL bar.
So, I change try_files from /trends/ to /trnds/ and do refactor of folder on my server and everything work like a charm!

How nginx process =404 fallback in try_files

I have a example web server with only one index.html file in a www directory. I can setup a nginx with following configuration:
location /subfolder {
alias /data/www;
try_files $uri $uri/ /index.html;
}
In browser I can see correct response on my local domain test.local/subfolder, also test.local/subfolder/something returns a default nginx page (it is normal because root is not set)
if I change a configuration to
location /subfolder {
alias /data/www;
try_files $uri $uri/ /index.html =404;
}
response to test.local/subfolder is still correct, but test.local/subfolder/something and all URI with /subfolder prefix return a index.html of correct response also status is 200 not 404. If I remove /index from try_files I get the same result
I wonder how nginx process request with =404 fallback, but cant find any information, not even in a official docs.
UDAPTE:
I found out that a alias directive should end with an / but still dont get a =404 functionality and purpose because a status is still 200ok
The try_files directive only supports these syntaxes:
try_files file ... uri;
try_files file ... =code;
It doesn't support:
try_files file ... uri =code;
The difference between file and uri here, is that for file arguments, NGINX will check their existence before moving on to next argument; for uri, it won't.
If the last argument has form of a =code, then all prior arguments to it are files (checked for existence).
From this, you can get a conclusion that with request URI /foo/bar and this config:
root /var/www;
location /foo/ {
try_files $uri $uri/ =404;
}
... Will not trigger 404 error if any of the files exist:
/var/www/foo/bar
/var/www/foo/bar/ directory (if you have autoindex enabled)
/var/www/foo/bar/index.html (or index.php, etc.) (due to value of index)
Only when none of the above exist, NGINX will trigger 404 error.
You should define the root of your server, then the default indexes and then add the =404 to try_files:
server {
server_name example.com;
root /var/www/html/example.com;
index index.html index.htm index.php;
# This is optional - if you want a customized 404 error page
error_page 404 /404.html;
location /subfolder {
try_files $uri $uri/ =404;
}
}
The difference between root and alias is that root appends location to get the absolute path in the filesystem while alias excludes the location. So for example when you try to fetch http://example.com/subfolder/filename.txt
server_name example.com;
root /var/www/html/example;
location /subfolder {
try_files $uri $uri/ =404;
}
will return the contents of /var/www/html/example/subfolder/filename.txt (if it exists) while
server_name example.com;
location /subfolder {
alias /var/log;
try_files $uri $uri/ =404;
}
will return the contents of /var/log/filename.txt (if it exists)

Nginx Configuration wildcard first folder

I am struggling to get nginx conf to work the way we need it.
Basically on the same domain we have many apps, each one in root folder. As the user installs apps it is not possible to know the name of the folders.
location / {
try_files $uri $uri/ /index.php?$args /index.php?q=$uri&$args;
}
location /myfiles {
try_files $uri $uri/ /myfiles/index.php?$args /myfiles index.php?q=$uri&$args;
}
If I specify the second folder, it makes app in myfiles work, URLs are resolving properly. If I do not then the main app tries to resolve the URL and it fails.
So I would like to have something like:
location /* {
try_files $uri $uri/ /$folderrequested/index.php?$args /$folderrequested/index.php?q=$uri&$args;
}
where * would be any root folder, for example myfiles, mycrm, myaccount, which would route the trafic to that folder.
Any suggestions and ideas welcome!
Put all your app root directories in a parent directory.
server {
listen .....;
server_name ....;
root /path/to/apps;
index index.php index.html;
location / {
}
location ~ \.php {
fastcgi_pass localhost:8000;
}
}
Bingo.

Hiding .html file extension from site url

I've tried this:http://www.tweaktalk.net/60/nginx-remove-php-file-extension-from-url but it didn't quite work for me. I'm getting either Error 404 or Error 505
I'm using Digital Ocean hosting with nginx server.
If someone wants to know how I solved this:
I've went to the directory /etc/nginx/conf.d , created there a folder called 'domain.trade.conf', for example if my domain is example.com, the folder name would be example.com.conf.
In this file I've added this code:
server {
listen 80;
server_name example.com; #Domain
root /var/www/html; #The place of your site files
access_log /var/www/html/logs/access.log; #Where to log the accesses
error_log /var/www/html/logs/error.log; #Where to log the errors
location / {
try_files $uri $uri/ #htmlext;
}
location ~ \.html$ {
try_files $uri =404;
}
location #htmlext {
rewrite ^(.*)$ $1.html last;
}
}
Hope I helped you.

Resources