How to use Nginx and PiHole

Pho3nix

The Legend
Joined
Jul 31, 2009
Messages
32,885
Reaction score
3,057
Location
On the toilet
Hi guys,

So have a couple things running on port 80 with nginx already and wanted to try out PiHole.
Went through a couple guides and still doesn't work :|

Have this at the moment :
Code:
    location /pihole {
        return 301 $scheme://$host/pihole/;
    }
    location ^~ /pihole/ {
        include /etc/nginx/snippets/proxy.conf;
        set $upstream_app pihole;
        set $upstream_port 80;
        set $upstream_proto http;
        proxy_pass $upstream_proto://$upstream_app:$upstream_port;
        auth_basic "What's the password?";
        auth_basic_user_file /etc/htpasswd.d/htpasswd.user};
        rewrite /pihole(.*) $1 break;
        proxy_hide_header X-Frame-Options;
    }
    location /pihole/admin {
        return 301 $scheme://$host/pihole/admin/;
    }
    location ^~ /pihole/admin/ {
        include /etc/nginx/snippets/proxy.conf;
        set $upstream_app pihole;
        set $upstream_port 80;
        set $upstream_proto http;
        proxy_pass $upstream_proto://$upstream_app:$upstream_port;
        auth_basic "What's the password?";
        auth_basic_user_file /etc/htpasswd.d/htpasswd.user;
        rewrite /pihole(.*) $1 break;
        proxy_hide_header X-Frame-Options;
    }
}

From my understanding this isn't working properly as I don't have the 'server {' portion mentioned here : https://docs.pi-hole.net/guides/nginx-configuration/#basic-requirements

Don't have that at the moment as I am running my stuff placed in the /etc/nginx/apps folder as individual configs.

Has anyone of you guys attempted this?
 
An example of my configs

Code:
location /sabnzbd {
  include /etc/nginx/snippets/proxy.conf;
  proxy_pass        http://127.0.0.1:8080/sabnzbd;
  auth_basic "What's the password?";
  auth_basic_user_file /etc/htpasswd.d/htpasswd.user;
}
 
bugger all that noise.

docker + traefik + pihole + whatever else.

So much easier. and no config headache with nginx

then you get xxx.somedomain for all your services

eg sabnzbd.somedomain, pihole.somedomain...

True but understanding how to get things working is the goal here.
Only recently ventured into Linux properly so want to learn first and take shortcuts later
 
is pihole happy with sharing port 80? try it on a different port?
 
Hi guys,

So have a couple things running on port 80 with nginx already and wanted to try out PiHole.
Went through a couple guides and still doesn't work :|

Have this at the moment :
Code:
    location /pihole {
        return 301 $scheme://$host/pihole/;
    }
    location ^~ /pihole/ {
        include /etc/nginx/snippets/proxy.conf;
        set $upstream_app pihole;
        set $upstream_port 80;
        set $upstream_proto http;
        proxy_pass $upstream_proto://$upstream_app:$upstream_port;
        auth_basic "What's the password?";
        auth_basic_user_file /etc/htpasswd.d/htpasswd.user};
        rewrite /pihole(.*) $1 break;
        proxy_hide_header X-Frame-Options;
    }
    location /pihole/admin {
        return 301 $scheme://$host/pihole/admin/;
    }
    location ^~ /pihole/admin/ {
        include /etc/nginx/snippets/proxy.conf;
        set $upstream_app pihole;
        set $upstream_port 80;
        set $upstream_proto http;
        proxy_pass $upstream_proto://$upstream_app:$upstream_port;
        auth_basic "What's the password?";
        auth_basic_user_file /etc/htpasswd.d/htpasswd.user;
        rewrite /pihole(.*) $1 break;
        proxy_hide_header X-Frame-Options;
    }
}

From my understanding this isn't working properly as I don't have the 'server {' portion mentioned here : https://docs.pi-hole.net/guides/nginx-configuration/#basic-requirements

Don't have that at the moment as I am running my stuff placed in the /etc/nginx/apps folder as individual configs.

Has anyone of you guys attempted this?
Better pattern is to have the main config have an include sites/services
Then those services part of Server blocks and distinguish them inside these by paths/ports(or both) using Location blocks

Not sure how your current individual configs look,but why the 301s? Are you trying to redirect http to https along with everything else?
Is Nginx even starting up? Are you getting hits in the logs?

Might want to experiment with a separate Server listener block on port 81,then start playing with adding your app location directives,once that works you can add HTTPS and once that works,add the 301 redirect directives to force http to https
 
I just run mine in docker with a seperate IP for pihole, as it's really a "catch any" on port 80 and 443 to stop adverts.
Would definitely not run the frontend through a proxy that's doing L7 inspection of headers, as you'd have to add a header redirect for every advert domain in the pihole blocklist to rewrite it to pihole instead of your other webapp.

There's probably a way to automate blocklists in pihole being used as redirect lists in nginx pointing back to pihole, but I wouldn't want to find out how.
 
Better pattern is to have the main config have an include sites/services
Then those services part of Server blocks and distinguish them inside these by paths/ports(or both) using Location blocks
Could you please expand on this :) Still learning and tbh got disheartened after not getting it working for a while.

Not sure how your current individual configs look,but why the 301s? Are you trying to redirect http to https along with everything else?
Is Nginx even starting up? Are you getting hits in the logs?

Might want to experiment with a separate Server listener block on port 81,then start playing with adding your app location directives,once that works you can add HTTPS and once that works,add the 301 redirect directives to force http to https

301's are per this guide here : https://github.com/linuxserver/reverse-proxy-confs

Going to try this on another HTPC I have to see what I can do there.
 
Could you please expand on this :) Still learning and tbh got disheartened after not getting it working for a while.

The sub-URL stuff is irritating with the Pihole,and hosting multiple services on one binding gets more complex (Pihole is pretty iffy about its /admin/ but it can be modified)
But i'll show how I set it in docker currently as that's your ideal endstate


Basic Nginx.conf

Code:
events {
    worker_connections  1024;
}

http {
  include /etc/nginx/conf.d/sites-enabled/*.conf;
}

Then in sites-enabled you can have multiple config files which it'll "Load",you can logically separate those configs how you prefer (I have Each port/webservice in a separate config)

pihole.conf

Code:
server { ##Pihole external binding on port 9180
    listen 9180
    location / {
                auth_basic “Pihole”;
                auth_basic_user_file /etc/nginx/auth/.htpasswd;
                proxy_pass http://192.168.0.7:80; #Pihole docker
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_read_timeout 90;
   }
}

This is a HTTP-only binding,if you get this working you can add extra complexity and revert the configs if you break it




Yeah those 301s are for forcing HTTP to HTTPS ( if you can't get the HTTP portion working HTTPS will be significantly harder to get working right)
 
Last edited:
Pihole? Seriously?

They chose that over so many other good names...lawd.
 
Top
Sign up to the MyBroadband newsletter
X