I want to have different sub-websites inside my website, in such a way that each of them matches to a different location.
For example, <website>/game/
should serve stuff from /var/www/<gamePath>
, and <website>/<blog>
should serve stuff from /var/www/<blogPath>
. As a default, <website>/<anything>
should serve stuff form /var/www/html/
.
My current config looks like this:
server {
server_name <website>;
root /var/www/html;
location / {
try_files $uri /index.html =404;
}
location /game/ {
root /var/www/<gamePath>/;
try_files $uri $uri/ /index.html =404;
}
location /blog/ {
root /var/www/<blogPath>/;
try_files $uri $uri/ /index.html =404;
}
<ssl stuff>
}
While the default location works, and <website>
correctly serves the file /var/www/html/index.html
, when I try to do <website>/blog/favicon.ico
, it does not serve the favicon, but instead defaults to /var/www/<blogPath>/index.html
. It seems that this is because it is appending blog
(the location) to the uri, and does it won't match anything.
I have seen many questions around this issue, but no answer has worked for me so far. Some replace the root
inside each specific location by alias
, but if I do that I only get 404's. Other solutions try rewriting the request instead of using $uri, in order to avoid the location being appended. This seems like the way to go, but I haven't found a regex that actually works (maybe they require trivial modifications that I haven't been able to do myself, as all I've tried is to copy and paste them).