Nginx can't find file - nginx

I have simple nginx server configuration which works and looks like this:
server {
listen 80;
server_name _;
location /cert {
alias /aaa.txt;
}
location / {
proxy_pass http://127.0.0.1:3000;
}
}
Proxy pass works like it should do but what doesn't work is getting this /aaa.txt file.
I tried changing alias... into:
return 200 'Hi' - This indeed return and download file named Hi
root / > As I read, root just adds to link before so it is not suitable for this situation but tried it
try_files /aaa.txt =404 - Not working
I tried changing permission of /aaa.txt with command chown www-data:www-data aaa.txt and still not working.
I have tried creating folder /wwwroot/ and put it inside that folder and do everything the same but still not working.
I do not know what else to try.
Pointing to answers provided in suggested question Nginx return file for path I edited my setup file and (this time I put it in folder wwwroot) and it is still not working
GNU nano 6.2 /etc/nginx/sites-available/reverse-proxy
server {
listen 80;
server_name _;
location /cert {
index aaa.txt;
alias /wwwroot;
}
location / {
proxy_pass http://127.0.0.1:3000;
}
}

Solution was quite simple. I was all time putting path relative to ~ which is not where nginx starts. In terminal you see it as ~ but its full path is /home/[user]/ so when i created folder wwwroot it was not located at /wwwroot but /home/ubuntu/wwwroot and that is the reason why it coulnd't find it.

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 location not resolved

I am trying to have my nginx instance host from two separate static files.
The config for root path works but using any other prefix pattern gives 404 not found.
The config is as follows:
server {
listen 80;
server_name *.saurabhharwande.com;
root /var/www/certbot;
location /abc/ {
index vindex.html;
}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
www.saurabhharwande.com Works
www.saurabhharwande.com/abc Gives 404
Am I missing something?
Edit: My basic understanding of how nginx configuration works was flawed. I thought that index file is searched in the root itself /<domain>/<root>/index.html and location is just used to point to different roots. It is rather searched at <domain>/<root>/<location>/index.html

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

How to specify a directory in nginx.conf to serve index.html from

I am trying to rewrite a very simple nginx.conf file. The only purpose of this project is to have nginx serve a static index.html on localhost.
Since all of the documentation and tutorials online have minimum 50 line configurations. I'm wondering if my 7 line configuration will work and accomplish what I need.
}
server {
root /test/index
listen 8888;
}
}
Usually I just use the default nginx.conf and make modifications for whatever project I'm working on, so I'm not sure if I can strip out as much as I did here and have it still function?
You'll do best to include the server_name as well, and to end your statements with semicolons:
server {
server_name some.server.name;
listen 8888;
# or just _ (underscore) to listen to any name
root /test/index;
index index.html;
}

Nginx config file only working for ''/"

I am setting up nginx as a sort of static file server. For some reason it is only working when I go to 123.123.123.123/ or 123.123.123.123. However, when I go to 123.123.123.123/static/content/ or 123.123.123.123/static/content/another.mp3 it returns a 404 not found. Here is the config file that is located in /etc/nginx/sites-available and linked to /etc/nginx/sites-enabled. I am really stumped as to why it is not working.
Any pointers or tips would be appreciated.
server {
listen 123.123.123.123:80;
server_name "";
location / {
root /srv/homepage;
index index.html;
}
location /static/content/ {
root /srv/static/content;
index song.mp3;
}
}
Look into logs.
Seems like nginx tries to open paths like /srv/static/content/static/content/file.mp3
You need to rewrite url here, try this:
rewrite /static/content/(.*) /$1 break;

Resources