Nginx Match Image Filename Based On Part of URL? - nginx

For local development, I'm trying to serve static content out of a public directory. My images live in app/public/images, and my root is app/public. My images are looked up at /d/my_app_name/images/<image>.png. I'm matching the /d/my_app_name/images location, but trying to figure out how to then extract the file name and have nginx look up the appropriate filename from the images directory? My current settings:
root /app/public;
index index.php;
location /d/my_app_name/images/ {
root /images/; # <--- I guess this rule is making nginx look up a file that matches the entire relative path, rather than just the filename
}
UPDATE
I was able to get it to match and look up the filename only from what appears to be the right directory, but I'm still getting a not found error. I'm assuming that the relative path shown in the error is relative to the root, in which case that file/directory should exist.
root /app/public;
index index.php;
location /d/my_app_name/images/ {
alias /images/;
}
When I go to localhost:8380/d/my_app_name/images/my_image.png it is looking up the correct png file in the app/public/images directory, but it's still saying it doesn't exist.
[error] 8#0: *1 open() "/images/my_image.png" failed (2: No such file or directory), client: 192.168.176.1, server: devbox, request: "GET /d/my_app_name/images/my_image.png HTTP/1.1", host: "localhost:8380"
Images are kept in /app/public/images directory.

I think you have inverted location and root paths.
I can suggest you to change your location block as follow:
location /images/ {
root /d/my_app_name/images/; # override global root to your specific path where your images are hosted
}
More configurations examples can be found on the official documentation.

You can use a simple alias which tells Nginx to search for all the files in another path. Root would only work if your location was just images and is actually used in the documentation: https://nginx.org/en/docs/http/ngx_http_core_module.html#alias
root /app/public; #Use full path, ex: /home/username/repo/app/public/
index index.php;
location /d/my_app_name/images/ {
alias /app/public/images/; # Use full path, ex: /home/username/repo/app/public/
}

Related

nginx: Index file outside of the document root

I'm trying to serve a directory of user-provided files with nginx, but with an index file that is outside of the document root. The reason I don't want to locate the index file inside the directory is because the directory is meant for users to drop their stuff in there.
Nginx's index directive seems to only work for files inside the document root; the documentation says that the index can be an "absolute path", but my experiments tell that this is only relative to the document root.
So, I tried to serve another index location with location =/index.html { alias /path/to/index.html }, and this manages to work when /index.html is directly requested, but it doesn't work if / is requested (403 with log "directory index of "/srv/docroot/" is forbidden"). It starts to work, though, if I create an empty file to /srv/docroot/index.html; then /path/to/index.html is served at /. So it seems that Nginx
Checks if /srv/docroot/index.html file exists in the filesystem at the /'s location.
If it exists, it does an internal redirect, and serves the /path/to/index.html at /index.html location.
What is the correct way to serve an index file outside of the document root?
index and try_files directives, provided with absolute paths, seem only to be able to point to the files inside the current document root. (With relative ../ paths you can point outside of the document root, but that's not ideal if you want to point to an absolute path in the filesystem.)
It seems that only the alias directive can point outside the document root. I was able to get my setup to work with:
...
location / {
try_files $uri /index.html;
}
location =/index.html {
alias /path/to/index.html;
}
...
This doesn't strictly answer to the question in the sense, that in this case, index.html isn't shown only when / is requested, but always when a matching file isn't found. I'm happy with this solution, but it might make sense in some cases to separate the 404 error.

How to not need a .dotted file path in nginx configuration

I'm trying to serve my AppLink for google association services. The following works:
location /.well-known/assetlinks.json {
root /var/www/static/google-association-service/;
types { } default_type "content-type: application/json";
}
Provided I have the correct file placed at
/var/www/static/google-association-service/.well-known/assetlinks.json
The URI is what it it is, Google gets to decide that, but I'd rather not have it resolve to a hidden file at the bottom of a directory where the next guy wonders why the directory is even there because he forgot to ls with a '-a'. Is there a way to make it so I can map this URI to something like:
/var/www/static/google-association-service/assetlinks.json # omit the hidden sub directory
?
(I've tried understanding the difference between root and alias, but I'm not seeing how alias would help me here)
alias basically gives you the possibility to serve a file with another name (e.g. serve a file named foo.json at location /.well-known/assetlinks.json). Even if this is not required in your case I would favor this config, as it is easily understandable:
location = /.well-known/assetlinks.json {
alias /var/www/static/google-association-service/assetlinks.json;
}
Note that the = is required to not match locations like /.well-known/assetlinks.json1111.
You can use try_files to find the specific file you want.
For example:
location = /.well-known/assetlinks.json {
root /var/www/static/google-association-service;
try_files /assetlinks.json =404;
}
Or:
location = /.well-known/assetlinks.json {
root /var/www/static;
try_files /google-association-service/assetlinks.json =404;
}
The concatenation of the root value and the try_files parameter form the path to the file.

how to specify nginx nested location in another directory

I have a config that looks like:
server {
listen 80;
root /pubStuff;
...stuff...
location /something {
root /app/something
}
}
The intention is, that while I can access stuff in the door of the server using example.com/ or with whatever directory/files location in /pubStuff, IF someone were to request example.com/something, or example.com/something/something.here.zip, they would get it!
When I try to go for example.com/something, I get a 404 not found error!
In error log it says:
14#14: *15409 open() "/app/something/something" failed (2: no such file or directory)....
I dont understand why does it affix a second "something", when I asked for only example.com/something, which should show me the content of /app/something as configured above, right?
I read the documentation on http://nginx.org/en/docs/http/ngx_http_core_module.html#location but it is rather focused on the prefix matching stuff, not explaining whats wrong in my use case.
Based on stackoverflow suggestions, I tried the solution at Nginx -- static file serving confusion with root & alias
basically, it seems that only should only specify root /app/, and that it would go to /app/something/something.zip, but error log says it tries /pubStuff/something/something.zip, totally ignoring the root directive for /something.

Nginx - how to correctly define root dir for location?

all.
Plese advice, how to correctly set root dir for location.
Now I have incorrect config:
location ~* ^/upload/
{
root /home/site/upload/
}
I want to locate all files from URLs, what are started from /upload/ from directory /home/site/upload/
But Nginx try to locate files from /home/site/upload/upload/ , for example /home/site/upload/upload/1.doc
How to fix my config?
Founded solution!
http://nginx.org/ru/docs/http/ngx_http_core_module.html#alias
Result:
location /upload/
{
alias /home/site/upload/;
}

configuring nginx server to store the static images and html

I want to configure the nginx server in my windows7 PC.For storing images and html files.
I followed up the following steps:
1.I downloaded the nginx-1.2.9 and unziped into c:\ filder.
2. created one folder "data" and within 'data" folder created another two folders say "WWW" and "images".
3.Keeping all images in the "images"folder .and .html file in folder "WWW".
4.Now started the nginx server using command C:\nginx-1.2.9>start nginx
5.Made changes in nginx.conf file.`
#server {
#location / {
# proxy_pass http://127.0.0.0:8080;
#}
#location /images/ {
# root /C:/data/images;
# }
}
Not able to access images and html page.
Please help me to solving this problem. I'm sure doing mistake in config file only..
Thanks in Advance,
Satya
You have commented the configuration data.First remove all the # from your configuration file.Then use the below code inside the server {}
location / {
root data/www;
}
location /images/ {
root data;
}
Note- the location of the static file inside your nginx root folder should be the (root+location) data and the access of the file should be "location" data. e.g from the first location configuration the static file should present inside the folder "data/WWW/" and in the second location configuration the static file should present inside the folder "data/images/".
URL folder inside nginx home path
----- --------------------------
localhost/hello.html data/WWW/hello.html
localhost/images/img1.png data/images/img1.png

Resources