Nginx: Serve static files for a subpath - nginx

[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.)

Related

How to route different webservers to different URL using nginx

creating a website for my self and need to host projects.
Basically, i hhave different projects with different framework. ie, Flask, Django, Node.JS and some html file projects. I would like to host them at projects.domain.com/<project name>
I tried to set server_name projects.domain.com/asdf but in error.log it says, server name "projects.domain.com/asdf" has suspicious symbols
Next up, i tried to nest location blocks (which i presume isn't how its supposed to be)
location /asdf {
location /static/ {
root blah blah;
}
location / {
..
proxy_pass http://localhost:3000 ;
}
}
But, this errors out saying location static is outside asdf
Some suggested to alias instead of root in the location /static/ block, but that doesnt work too.
Any help is appreciated :)
First of all a server_name can not contain URI segments. So a hostname or IP should be used as a value.
If you want to mix different local directories and proxy-locations a configuration could look like this.
Notice: Your location URI (/one, /two) will be appended to the root path.
The root directive can be used in every location block to set the document root.
http://nginx.org/en/docs/http/ngx_http_core_module.html#root
This is the reason why alias exists. With alias the location will not be part of the directory path. Check this out:
http://nginx.org/en/docs/http/ngx_http_core_module.html#alias
server {
server_name project.domain.com;
listen 80;
location / {
proxy_pass http://localhost:3000;
}
location /one/ {
alias/var/www/html/project1/;
index index.html;
}
location /two/ {
alias/var/www/html/project2/;
index index.html;
}
}

nginx serve index file from subfolders

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.

nginx how to serve pictures or media files?

I'm having issues serving pictures with nginx. I originally started with a Django project and I wanted to serve the user uploaded media files via nginx but I wasn't able to get it working no matter what I tried.
So, I've make a second temporary droplet/server and am trying a bare bones setup with no Django project, just Nginx, to see if I can get it to simply serve an index and a couple pictures in a folder called 'media'. Here is the server block:
server {
listen 80;
listen [::]:80;
root /var/www/example.com/html;
index index.html;
server_name 159.89.141.121;
location / {
try_files $uri $uri/ =404;
}
location /media/ {
root /var/www/example.com/media;
}
}
Now, the index.html is served fine but if I try to go to 159.89.141.121/media/testpic.jpg it still doesn't work and returns a 404 error. I'm at a complete loss here, I've tried using alias instead of root, I've tried changing the folder structure and setting all permissions to 777 and changing folder and file ownership, permissions shouldn't be a problem because the index.html works fine with the same permissions; I just cant seem to figure out what I'm doing wrong. The picture is in the folder but nothing I try allows me to access it via the uri. Are there any obvious problems with my server block?
Well I decided to read the documentation and realized that the location block adds to the root directory specified..
So the pathing of
`location /media/ {
root /var/www/example.com/media;
}`
would end up routing example.com/media/testpic.jpg to /var/www/example.com/media/media/testpic.jpg
I've changed the location block to look like this
location /images/ {
root /var/www/example.com/media;
}
and it will now route example.com/images/testpic.jpg to /var/www/example.com/media/images/testpic.jpg
I'm not sure why it didn't work when I tried the alias directive, though...

Nginx - sending a static file for a specific location

I'm using nginx as a proxy (with location /) and trying to serve a static image for hotlink protection redirects under another location block. The following is what I am using to try to serve the image. I've moved the root directive outside of the location block which was necessary for nginx to build a proper path for some reason.
location = /hotlink.png {
autoindex off;
try_files hotlink.png hotlink.png
}
However, when I look at the log, it is still looking for an index.html by appending the URI to the root path: {root}/hotlink.png/index.html.
I simply want it to only send the file {root}/hotlink.png when /hotlink.png is matched and that's it.
Why is it still looking for an index with autoindex off? How can I fix this or is there a better way to handle this case in general?
After changing gears and coming back to this, I got it working by simply moving the autoindex off directive outside of the location block as well. It works with or without the try_files directive. I left it out for terseness and to avoid redundancy
root /path/to/static/files;
autoindex off;
location / {
# proxy
}
location = /hotlink.png {}

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