NGINX - Password Protect Folder - Error 502 - Bad gateway - SOLVED!

Discussion in 'Installation/Configuration' started by HappierTimesAhead, Feb 1, 2026.

  1. HappierTimesAhead

    HappierTimesAhead Member HowtoForge Supporter

    Hi Team ISPConfig
    I am trying to password protect a folder on NGINX and getting Error 502 - Bad gateway
    I am using Debian 12, PHP 8.2 on ISPConfig 3.3.0p3 and the Document Root is /var/www/clients/client0/web28
    This is my NGINX Directive entered via ISPConfig nginx Directives UI: -
    Code:
    #RESTRICT USER ACCESS
    ###WARNING THIS MUST ALWAYS BE THE FIRST BLOCK OR IT WILL NOT WORK###
    location ^~ /media_asset_main/ {
     location ~ \.php$ {
                try_files $uri =404;
                include /etc/nginx/fastcgi_params;
                fastcgi_pass unix:/var/lib/php8.2-fpm/web28.sock; ##web?? needs to match Linux User for our website
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_intercept_errors on;
      }
       auth_basic "Hello, please login";
       auth_basic_user_file /var/www/foo.foo.com/private/.htpasswd;
    }
    #RESTRICT USER ACCESS
    ###WARNING THIS MUST ALWAYS BE THE FIRST BLOCK OR IT WILL NOT WORK###
    When I try and access "https://foo.foo.com/media_asset_main/mediaupdate.php
    I get: -
    1/ Auth popup which works all OK
    2/ Error 502 - Bad gateway
    This is the error that gets logged: -
    Code:
    2026/02/01 13:36:15 [crit] 2098141#2098141: *8 connect() to unix:/var/lib/php8.2-fpm/web28.sock failed (2: No such file or directory) while connecting to upstream, client: 123.123.123.123, server: foo.foo.com, request: "GET /media_asset_main/mediaupdate.php HTTP/2.0", upstream: "fastcgi://unix:/var/lib/php8.2-fpm/web28.sock:", host: "foo.foo.com"
    If I remove the above block of directive everything works as expected!
    I just cannot see what I am doing wrong???
    I need to be able to process .php files which are password protected.
    Any pointers / help would be very welcome.
    Many thanks in advance for any kind help
    Regards
    HTA
     
    Last edited: Feb 1, 2026
  2. ahrasis

    ahrasis Well-Known Member HowtoForge Supporter

    HappierTimesAhead likes this.
  3. HappierTimesAhead

    HappierTimesAhead Member HowtoForge Supporter

    Hi ahrasis - Thank You for your time.
    Sadly that did not resolve the issue!
    Thanks also for the nginx config checkup link - very useful
    Kind regards
    HTA
     
  4. HappierTimesAhead

    HappierTimesAhead Member HowtoForge Supporter

    Hi all,
    I managed to solve the problem!
    If anyone has this issue, please see the solution below: -
    Code:
    ## RESTRICT USER ACCESS - START
    location ^~ /media_asset_main/ {
     location ~ \.php$ {
                include /etc/nginx/fastcgi_params;
                fastcgi_pass unix:/run/php/web23.sock; ##web?? needs to match Linux User for our website
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_intercept_errors on;
      }
       auth_basic "Hello, please login";
       auth_basic_user_file /var/www/foo.foo.com/private/.htpasswd;
    }
    ## RESTRICT USER ACCESS - END
    Hope this helps someone?
    Kind regards
    HTA
     
  5. ahrasis

    ahrasis Well-Known Member HowtoForge Supporter

    Will it work if you remove this part in your added directive as I think by default, ISPConfig already set it in each website's vhost.
    Code:
     location ~ \.php$ {
               include /etc/nginx/fastcgi_params;
               fastcgi_pass unix:/run/php/web23.sock; ##web?? needs to match Linux User for our website
               fastcgi_index index.php;
               fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
               fastcgi_intercept_errors on;
      }
    
     
  6. till

    till Super Moderator Staff Member ISPConfig Developer

    And he probably could just have used the folder protection feature in ISPConfig instead of inserting custom directives.
     
    ahrasis likes this.
  7. ahrasis

    ahrasis Well-Known Member HowtoForge Supporter

    :oops:I also forgot about that folder protection feature.
     
  8. HappierTimesAhead

    HappierTimesAhead Member HowtoForge Supporter

    Hi ahrasis,
    The short answer is no it will not work by removing the code you mention!
    If I did that, the PHP file would just get downloaded.
    Regards
    HTA
     
  9. HappierTimesAhead

    HappierTimesAhead Member HowtoForge Supporter

    Hi Till,
    I need four different people (with unique passwords) to be able to run various PHP scripts in that folder!
    I believe that the folder protection feature in ISPConfig is limited to one user?
    HTA
     
    ahrasis likes this.
  10. till

    till Super Moderator Staff Member ISPConfig Developer

    No, its not.
     
  11. HappierTimesAhead

    HappierTimesAhead Member HowtoForge Supporter

    Wow, I stand corrected!
    I wish I had known this three days ago!
     

Share This Page