How do I serve static html files in NGINX? - nginx

I'm trying to serve some static html files in NGINX in the following folder and file naming convention.
/root/level-1/
/root/level-1a/
/root/level-1b/
/root/level-1.html
/root/level-1a.html
/root/level-1b.html
/root/level-1/foo.html
/root/level-1a/foo.html
/root/level-1b/foo.html
/root/level-1/level-2/
/root/level-1/level-2a/
/root/level-1/level-2b/
/root/level-1/level-2.html
/root/level-1/level-2a.html
/root/level-1/level-2b.html
/root/level-1/level-2/foo.html
/root/level-1/level-2a/foo.html
/root/level-1/level-2b/foo.html
All the paths in my html look something like this..
<a href="/root/level-1">
<a href="/root/level-1a">
<a href="/root/level-1b">
<a href="/root/level-1/foo">
<a href="/root/level-1a/foo">
<a href="/root/level-1b/foo">
<a href="/root/level-1/level-2">
<a href="/root/level-1/level-2a">
<a href="/root/level-1/level-2b">
<a href="/root/level-2/foo">
<a href="/root/level-2a/foo">
<a href="/root/level-2b/foo">
etc...
I'm trying to get NGINX to realise that when I have a URL that looks like this /root/level-1, it should try the file /root/level-1.html OR /root/level-1/level-2, it tries /root/level-1/level-2.html OR /root/level-1/level-2a/foo, it tries /root/level-1/level-2a/foo.html
This is what i have set for my location setting, I specifically trying to target the "root" location.
location ~ ^/root/.*$ {
try_files $uri.html $uri $uri/ =404;
}
Right now when i try http://localhost/root/level-1 I get a 403 Forbidden and the URL in my browser ends up looking like this "http://localhost/root/level-1/" (the url now has a forward slash at the end of it).
I've tried several combinations but I think I'm missing a core understanding of what NGINX is doing and what I need to tell it to do in my situation.
Can any one help?

This has got me 99% there
location ~ ^(?<path>/.*/)(?<file>[^/]+)/$ {
try_files $uri $path$file.html $uri/ =404;
}
This URL is working /root/level-1/level-2 but if I then try to get /root/level-1/level-2/foo it does not try and read /root/level-1/level-2/foo.html I get a 404 error.
I should add there is no foo folder in /root/level-1/level-2 just /root/level-1/level-2/foo.html
I'm happy to hear from those more skilled than me if this solution can be improved on.

Related

Nginx Configuration for Dynamic URL Segments as Arguments for Parent Segment Index

I am trying to set a location in the configuration that allows me to do something like https://example.com/car/<vin> which would not go to a <vin> application or directory but to /car/index.html. From there, I would read the URL or pass <vin> to /car/index.html.
I have tried various regex location blocks, like the one below, but they all result in a 404 when accessing /car/<vin>.
location ~ ^/car/(.*)$ {
root $document_root/car/
index index.html;
}
What would be an appropriate location block?
Do you want to use a regular expression? The prefix location would also work as it matches any URI that begins with /car/. See this document for details.
For example:
location ^~ /car/ {
try_files /car/index.html =404;
}
Using $document_root in the root statement may not work, and the index directive only works with URIs that end with a /. The try_files statement is probably the simplest solution. See this document for details.

convert url segment to get variable keeping others get variables

I can't fix that I have a url
domain.com/api/class/access/1/index.php?username=usuariodemo&name=sala
where /1/ is a ID so, I need convert /1/ to get variables like
domain.com/api/class/access/index.php?id=1&username=usuariodemo&name=sala
That is pure PHP not framework my index.php is located on folder api/class/access/index.php
I have this I try others but this explain me better waht I want do
location /api/class/access/(.*)/* {
try_files $uri $uri/ /api/class/access/index.php?id=$1&$query_string;
}
That show me:
No input file specified.
Thanks!
Regards!
The location statement in your question is invalid and probably unnecessary.
If you want to rewrite a URI ending with .php it may be easiest to place a rewrite statement inside the block that processes all .php URIs, usually: location ~ \.php$ { ... }.
For example:
location ~ \.php$ {
rewrite ^(/api/class/access/)(\d+)/(index.php)$ $1$3?id=$2 last;
...
}

Having issues with regex matching wrong URLs in NGINX rewrite

Hi I have urls that look like this
http://dansawesomesite.com/123/articlename
I have the following rewrite rule in nginx
location ~* /(\d+)/([\+\w-\ ]+)/?$ {
try_files $uri /wpcontentredir.php?slug=$1;
}
This matches the above URL however the issue comes about when I have the following URL's
http://dansawesomesite.com/posts/630325/like
(as well as a number of similar)
These also end up getting matched which is correct based on my regex, but will mess things up as I dont want these urls parsed through that try_files, I just want them to pass as is.
Just wondering if anyone can help me with only matching the top first URL?
CHeers
Dan
Try to add "^" to the beginning of regexp
location ~* ^/(\d+)/([+\w-\ ]+)/?$
So it will match only if first part of URI contains digits and not "posts" or something

Display default image when missing file in nginx

We have various pictures associated with items on our site but not for all of them. To this end we would like to display the contents of 'blank.jpg' if a given image is not found.
To this end we have the nginx config like this
location /static/images/ {
root /blah_blah_blah/pictures/;
error_page 404 blank.jpg;
break;
}
The problems with this is that this takes /static/images/fred.jpg and redirects to /static/images/blank.jpg when what we want is to simply display the contents of blank.jpg whilst keeping the /static/images/fred.jpg url
It also seems to go into a redirect loop occasionally.
Not sure if you still need it 10 month later but this looks like what you were looking for: https://serverfault.com/questions/562269/redirect-missing-images-in-a-specific-directory
I'd like to share a little bit more sophisticated example. It's a way how you can match only selected extensions or name patterns:
location ~ ^/media/(.*)/preview(.*).(png|mp4)$ {
try_files $uri #rewrite;
}
location #rewrite {
rewrite ^/media/(.*)/preview(.*).(png|mp4)$ $scheme://$host/media/no_preview/preview$2.$3 redirect;
}

Nginx same config for multiple paths

I'd like to configure several paths within a site (such as /foo/ and /bar/) in the same way. To avoid copy-pasting, I figured I should use a single location block, but the only way I found for doing that is to use a regex, such as:
location ~ ^/(foo|bar)/ {
...
}
Is this the best way or is there a better alternative?
This would work, but I believe it works slower, because it involves a regex engine, another alternative you could create the config you want in a separate file and include them in each location so that it would be written once and could be edited all together, like this
location /foo {
include foo.conf;
}
location /bar {
include foo.conf;
}
Inside foo.conf you could write any config that's in a location scope
heres a random sample snippet:
root /foo/bar;
try_files $uri /index.html;

Resources