As a Nginx noob, I have a problem which I can't solve. In Nginx website configuration I have:
server {
listen 443;
listen [::]:443;
ssl on;
ssl_certificate /etc/letsencrypt/live/website.eu/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/website.eu/privkey.pem;
server_name website.eu www.website.eu;
index index.html index.htm index.php;
root /var/www/website;
location / {
try_files $uri $uri/ =404;
}
location /pihole {
alias /var/www/html;
try_files $uri $uri/ /admin/index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}
}
Website works, but the Pihole was installed in /var/www/html
folder.
If I try to access it, I get:
user@website:~$ curl -i https://www.website.eu/pihole
HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.0 (Ubuntu)
Date: Fri, 01 Nov 2019 11:29:13 GMT
Content-Type: text/html
Content-Length: 194
Location: https://www.website.eu/pihole/
Connection: keep-alive
Any idea what is wrong?
Let's check:
your current configuration
try_files
directive:index
directive:your requested URL path is
/pihole
, which matcheslocation /pihole
, which in turn is aliased:Okay, so when you request for
/pihole
($uri
),nginx
will sequentially do:check for
$uri
first, but it can't match that as/pihole
is alised to a location so no file can't be matchedmove onto
$uri/
next (this causes the 301 redirection); so after resolving thealias
the final filesystem path becomes/var/www/website/
and then tries to see if there is any file referred byindex
in this directory, so triesindex.html
,index.htm
,index.php
sequentially here -- the first one found wins and a response is sent.If the second step fails, it moves onto the last one i.e.
/admin/index.php?$query_string
-- this is an absolute path so it is matched from the serverroot
. Hence the final path becomes:/var/www/website/admin/index.php?$query_string
as theroot
is set as/var/www/website
. If a file/var/www/website/admin/index.php
is found, it is passed thequery_string
and the resulting response is sent.If nothing matches, a 404 is sent eventually.
Like I mentioned earlier, in your case, the second (
$uri/
) is causing the 301 redirection that you're seeing. While we're at this, in case of redirection, always check out theLocation
directive to find out the redirected URL:Now, a good practice while using
location
is to use trailing/
unless you're do a generic/unbound match. So for example,location /pihole
matches/pihole
,/piholefoo
,/piholebar
and so on. But you don't probably want that. So you should be precise in this case: