M
M
Maxim2022-02-14 05:49:33
Nginx
Maxim, 2022-02-14 05:49:33

How to setup root in nginx in my case?

Half the night I unsuccessfully tried to google the reason why this configuration does not work for me (the server returns 404 when requesting /):

...
root /home/max/www/project;

location / {
    root /pages;
    index index.html;
}
...


The config itself is correct. Logic error.
If I specify root, which is by default, all the way to the folder where I have index.html ( $root/pages ) then everything works.
But I don't need that. For html files, I have a separate /pages directory, because I don't want my [css,img,js,] folders to be on the same level as html files.
And that's why I want to leave /project as the root directory of the project in order to have access to all static resources from the root.
In general, the /project folder structure looks like this:
/css
/js
/img
/pages

Please help!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Karabanov, 2022-02-14
@max_shane

By specifying root in a location, you override it within the location, at the server level, and in other locations (if there are no overrides in them), root will remain the same. What do you not like?

root /home/max/www/project;

location / {
    root /home/max/www/project/pages;
    # [...]
}

# можешь хоть для каждого location переопределить root,
# но нет нужды в этом в твоём случае - достаточно переопределить root
# только для location / { ... }

PS
About root and alias
Pitfalls and Common Mistakes
PPS
root /pages;- means to look for files in the pages directory, which is located in the root of the disk.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question