I'm trying to setup nginx for first test uses, without a domain yet.
My current goal is to show some page at http://<server IP>
and serve some static content at http://<server IP>/projectname
. The "some page" is currently just the default /var/www/html/index.nginx-debian.html
.
In /etc/nginx/sites-available/
I've created a projectname
config and I've put a link to sites-enabled:
sudo ln -s /etc/nginx/sites-available/tiddlywiki /etc/nginx/sites-enabled/
The first version of config was
server {
listen 80;
listen [::]:80;
server_name <server IP>;
root /some/path/to/project/static-files;
index index.html;
location /projectname {
try_files $uri $uri/ =404;
}
}
What I got, is http://<server IP>
started serving static files, but http://<server IP>/projectname
showed 404. How do I fix that? Because next step, I've followed this answer and tried to set 2 locations:
location /projectname {
try_files $uri $uri/ =404;
}
location / {
root /var/www/html;
index index.nginx-debian.html;
}
but only got the default page at http://<server IP>
back again, and 404 at http://<server IP>/projectname
.
CodePudding user response:
Ok, so the problem was, with root
directive, path is concatenated to the root, so with this config
server {
listen 80;
listen [::]:80;
server_name <server IP>;
root /some/path/to/project/static-files;
index index.html;
location /projectname {
try_files $uri $uri/ =404;
}
location / {
root /var/www/html;
index index.nginx-debian.html;
}
}
nginx tried to serve /projectname
→ /some/path/to/project/static-files/projectname
which is an unexisting folder (existing one is /some/path/to/project/static-files
). What I needed is the alias
directive:
server {
listen 80;
listen [::]:80;
server_name <server IP>;
index index.html;
location /projectname {
alias /some/path/to/project/static-files;
index index.html;
}
location / {
root /var/www/html;
index index.nginx-debian.html;
}
}
I'm not sure how exactly try_files
works so I've removed it for now and also added the index
directive.