nginx proxy_pass only works with complete filenames

Posted on

nginx proxy_pass only works with complete filenames – A server stack is the collection of software that forms the operational infrastructure on a given machine. In a computing context, a stack is an ordered pile. A server stack is one type of solution stack — an ordered selection of software that makes it possible to complete a particular task. Like in this post about nginx proxy_pass only works with complete filenames was one problem in server stack that need for a solution. Below are some tips in manage your linux server when you find problem about linux, nginx, proxypass, , .

I have an nginx which should proxy the path domain.de/pihole/* to a docker container running pihole. If I use the IP of the docker dontainer (172.20.0.2) the index.php is loaded, 172.20.0.2/admin and 172.20.0.2/admin/index.php work as well.
If I use domain.de/pihole or domain.de/pihole/admin, I get a 404. If I use domain.de/pihole/admin/index.php, all is working.
This is my /etc/nginx/sites-available/default:

server {
        listen 80 default_server;
        listen [::]:80 default_server;
 root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
                #proxy_pass http://172.20.0.2:25565;
        }
        location /pihole/ {
                proxy_pass http://172.20.0.2:80/;
                proxy_http_version 1.1;
                proxy_set_header Host $host:$server_port;
                proxy_set_header Referer $http_referer;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto https;
                proxy_set_header X-Forwarded-Ssl on;
                proxy_set_header X-Nginx-Proxy true;
                proxy_set_header X-Client-Verify $ssl_client_verify;
                proxy_set_header X-Client-DN $ssl_client_s_dn;
                proxy_set_header X-SSL-Issuer $ssl_client_i_dn;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_redirect off;

        }
}

Any ideas what I have to change (I copied most of the “proxy_set_header” stuff from serverfault)?

Please remove the trailing / in the proxy_pass line. The reason why is described in the doc for proxy_pass

Quote:

If proxy_pass is specified without a URI, the request URI is passed to
the server in the same form as sent by a client when the original
request is processed, or the full normalized request URI is passed
when processing the changed URI:

location /some/path/ {
    proxy_pass http://127.0.0.1;
}

solved with this location block

location /pihole/ {
    proxy_http_version 1.1;
    proxy_set_header Referer $http_referer;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto https;
    proxy_set_header X-Forwarded-Ssl on;
    proxy_set_header X-Nginx-Proxy true;
    proxy_set_header X-Client-Verify $ssl_client_verify;
    proxy_set_header X-Client-DN $ssl_client_s_dn;
    proxy_set_header X-SSL-Issuer $ssl_client_i_dn;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_pass http://172.20.0.2/admin/;
}

I used
a bit of try and error with slashes
and the fact that pihole needs /admin in the url (what I did not know before)

Leave a Reply

Your email address will not be published.