How can I read a file within location directive? After reading the file, based on some decision I will proceed further or throw error.
Related
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.
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/
}
I think I have a problem understanding how I can check if a file exists in a specific folder with nginx.
for example, I use this url:
www.domain.tld/folder/filename.pdf
now, I think I have to check it like that:
location /folder/.(pdf)$ {
}
is that correct?
and then, if it is the right way, how can I redirect if the file doesn't exists?
I'm new and from apache os it is a bit hard for men to switch in my mind
The location directive matches the requested URI, but does not decide if the file exists. The contents of the location block determines the action if the file exists or not, and the simplest way to accomplish that is using try_files.
The try_files directive will test if the file exists and internally redirect to another URI if it does not.
For example:
location ~ ^/folder/.*\.pdf$ {
try_files $uri /another/uri;
}
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.
I am trying to execute cgi script on Nginx. In nginx.conf file, I added a location directive such as below:
location /cgi-bin{
root cgi-bin;
index index.cgi;
}
When I try to connect to http://example.com/cgi-bin/index.cgi, it says file not found. In error.log, I see the request as "http://example.com/html/cgi-bin/index.cgi.
cgi-bin is not in html folder. The correct path is "http://example.com/cgi-bin/index.cgi
I tried many possibilities for location directive. Either it looks in html/cgi-bin or it does /cgi-bin/cgi-bin/index.cgi. I am not sure why it uses 'cgi-bin' twice
Any suggestions.. I have been trying for hours now!!!