nginx serve index file from subfolders - http

I would like to serve an index file from a root folder and one from a subfolder from root.
My nginx server conf looks as follows:
server {
listen 80;
server_name localhost;
root /data/;
index index.html index.htm index.txt;
location / {
}
location /a {
alias /data/a/;
}
}
and my directory structure looks as follows:
/data/:
a index.txt
/data/a:
index.txt
If I then do curl localhost, I get the contents of the file /data/index.txt.
But curl /localhost/a gives me a 301.
curl localhost/a/index.txt works.
Why can't I access my index.txt with curl /localhost/a ?
I tried using a root instead of alias in the location /a block and also tried to specify the index.txt for location /a, but no success.
I see similar posts, e.g.
Nginx location configuration (subfolders)
but couldn't yet find the answer.

The index directive works with URIs that end with a /:
So the URI / gives you the contents of /index.txt and /a/ gives you the contents of `/a/index.txt.
If you provide Nginx with a URI of a directory (but without a trailing /), the default behaviour is to redirect to the same URI, but with a trailing /.
This is just how the index directive works. See this document for details.
If you want something other than default behaviour you will have to do it manually using try_files. See this document for details.
For example, to return the contents of an index.txt file by providing the URI of the directory without a trailing /, use:
root /data;
location / {
try_files $uri $uri/index.txt =404;
}
Note that the location /a { ... } block is not necessary in either this example, or the example in your question.

Related

Nginx: Serve static files for a subpath

[UPDATE/Note: "subdomain" in the following description actually means a subpath in the URL, sorry for the confusion. I changed the title but left the description as it was.]
I would like to serve static content from a subdomain:
location /subdomain/ {
root /www/mydirectory;
index index.html;
client_max_body_size 10g;
}
The problem is that the index.html for https://mydomain/mysubdomain/ then has to be available in directory /www/mydirectory/mysuddomain, but I would like to place it into /www/mydirectory. In other words: When using the URL https://mydomain/mysuddomain/, then /www/mydirectory/mysuddomain/index.html is served, but it should be /www/mydirectory/index.html
Thanks to the above answer by Ivan Shatsky: I changed to the following and it worked:
location /subdomain {
alias /www/mydirectory;
index index.html;
client_max_body_size 10g;
}
(I also noted that other than I thought before, using /subdomain i.e. without a trailing slash does not mean that /subdomain2 also matches.)

nginx force serve local file, instead of web URL

I'm trying to authenticate user, then serve the requested file.
URL: http://example.com/stream/asd.m3u8
Static file: /stream/asd.m3u8 (/ = Linux filesystem's root)
location ~ /stream/(.*)\.m3u8 {
# authentication happens here, but commented out
for debugging, and had the results below;
alias /stream/;
# 403 Forbidden (the directory and contents has chmod 777)
rewrite /stream/(.*)\.m3u8 /stream/$1;
# Infinite loop (curl: (47) Maximum (50) redirects followed)
rewrite /stream/(.*)\.m3u8 /stream/$1 break;
# 404 Not Found (in web root, that file doesn't exist)
try_files /stream/$1.m3u8 =404;
# 404 Not Found
try_files /stream/$1.m3u8 =505;
# 505
}
I think nginx treats /stream/asd.m3u8 as a web URL.
How can I force it to treat /stream/asd.m3u8 as a local file?
Also, what could cause the 403 on alias directive?
Thanks!
The URI is the same as the pathname, which means that the document root is /. The document root is set using the root directive. See this document for details.
To set the correct document root for any URI that begins with /stream/ use:
location /stream/ {
root /;
}
If you have other files in this location, and only want to serve those ending with .m3u8, you could use your existing regular expression location block:
location ~ ^/stream/.*\.m3u8 {
root /;
}
See this document for more.
The alias directive is not necessary here, in which case the root directive is preferred. To use alias inside a regular expression, you must capture the entire URI. See this document for details.

nginx - 403 returned when I specified the location to my folder in Mac

I have nginx configuration like this:
location /myweb/ {
#alias html/myweb/app/;
alias /Users/admin/Documents/Projects/myweb/app/;
index index.html;
}
When I place my project under my local folder(/Users/admin/Documents/Projects/myweb), 403 is returned when I visit with http://localhost/myweb, but if I place it into the html folder under nginx home folder, it works.
I even tried chmod -R 777 xxx, and start nginx with sudo. But it still returns 403.
Did I miss any settings for nginx?
The location /myweb/ does not match the URI /myweb. It is the fact that there is a directory called myweb under html that makes it work at the alternative root. But the simple solution is to fix the location and alias to match that URI, rather than just its descendants.
location /myweb {
alias /Users/admin/Documents/Projects/myweb/app;
index index.html;
}
Remove the trailing / from both the location and alias directives.

Wrong with my nginx rewrite rule

My first rewrite rule is :
location / {
root E://w/q/__t/q/;
index index.html index.htm;
}
then I request 127.0.0.1/test.js
I can fetch the test.js file in the fold E://w/q/__t/q/
then I update the rewrite rule, I add a /js/ path both in my location and request path:
location /js/ {
root E://w/q/__t/q/;
index index.html index.htm;
}
then I request 127.0.0.1/js/test.js
but the nginx return 404
So what's wrong with my code? How can make it correct?
My nginx version is 1.5.8 and my OS is Windows 7
well, you don't actually use the rewrite command!
With this config, Nginx will look at E://w/q/__t/q/js/test.js when you request 127.0.0.1/js/test.js
So copy your js there, or use a rewrite command to remove the js part in your url.
You didn't update the root, you can either do that or use alias instead of root
location /js/ {
root E://w/q/__t/q/js;
index index.html index.htm;
}
or
location /js/ {
alias E://w/q/__t/q/;
index index.html index.htm;
}
Ps: try to avoid placing index and root inside locations, it's a bad practice, also if you're going to use alias make sure not to use try_files with it

Removing index extension in nginx

With Apache the directive DirectoryIndex index along with DefaultType application/x-httpd-php within a particular vhost worked quite well to exclude a file extension from index files without rewriting. How can I duplicate this in Nginx? So far all I've been able to find is regex rewriting solutions.
The .conf file would look something like this:
server {
server_name example.com;
# Set the docroot directly in the server
root /var/www;
# Allow index.php or index.html as directory index files
index index;
# See if a file or directory was requested first. If not, try the request as a php file.
location / {
try_files $uri $uri/;
}
}
the line try_files $uri should try the files without extensions on the backend

Resources