5

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?

JanezKranjski
  • 831
  • 7
  • 18
  • 36

1 Answers1

3

Let's check:

  • your current configuration try_files directive:

    try_files $uri $uri/ /admin/index.php?$query_string;
    
  • index directive:

    index index.html index.htm index.php;
    
  • your requested URL path is /pihole, which matches location /pihole, which in turn is aliased:

    alias /var/www/html
    

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 matched

  • move onto $uri/ next (this causes the 301 redirection); so after resolving the alias the final filesystem path becomes /var/www/website/ and then tries to see if there is any file referred by index in this directory, so tries index.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 server root. Hence the final path becomes: /var/www/website/admin/index.php?$query_string as the root is set as /var/www/website. If a file /var/www/website/admin/index.php is found, it is passed the query_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 the Location directive to find out the redirected URL:

Location: https://www.website.eu/pihole/ 

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:

location /pihole/ {
    # Note trailing / here as well
    alias /var/www/html/;
    try_files $uri $uri/ /admin/index.php?$query_string;
}
heemayl
  • 91,753
  • thank you for your explanation. At the same time as you wrote this, I moved the "root /var/www/website;" inside the first Location block (for /). And it starts responding. Now I have another problem, but it is probably not in connection with Nginx settings. – JanezKranjski Nov 04 '19 at 04:16
  • @JanezKranjski ummmm...I don't think I told you to move root. I've mentioned all the basic stuffs you need to know. If you're stuck at some place, please feel free to let me know. – heemayl Nov 04 '19 at 05:23
  • 1
    you are right. Now I tried from the scratch (from suggestions from Pihole site) and it works now. I will probably have new questions when I will start changing folder and URL structure :-) but it will be new question :-) Thank you. – JanezKranjski Nov 04 '19 at 05:52
  • @JanezKranjski No problem :) – heemayl Nov 04 '19 at 05:53