Help with nginx protected directory

dbembibre

Active member
Hi to all im totally stucked with a stupid thing, i move in my plesk nginx as reverse proxy for apache to nginx server directly php files via php-fpm, all work ok, xenforo url friendly, etc. But i can't make a password protected directory, i see that auth basic module is part of the core of nginx.
My nginx.com

Code:
server {
    listen x.x.x.x:80;

    server_name site.com;
    server_name www.site.com;
    server_name ipv4.site.com;

    client_max_body_size 128m;

    root "/var/www/vhosts/site.com/httpdocs";

    location / {
        proxy_pass http://x.x.x.x:7080;
        proxy_set_header Host            $host;
        proxy_set_header X-Real-IP        $remote_addr;
        proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
        access_log off;
        try_files $uri $uri/ /index.php?$uri&$args;
    }

    location ~ /(internal_data|library) {
        internal;
    }

    location @[USER=7374]FallBack[/USER] {
        proxy_pass http://x.x.x.x:7080;
        proxy_set_header Host            $host;
        proxy_set_header X-Real-IP        $remote_addr;
        proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
        access_log off;
    }

    location ~ ^/(.*\.(ac3|avi|bmp|bz2|css|cue|dat|doc|docx|dts|exe|flv|gif|gz|htm|html|ico|img|iso|jpeg|jpg|js|mkv|mp3|mp4|mpeg|mpg|ogg|pdf|png|ppt|pptx|qt|rar|rm|swf|tar|tgz|txt|wav|xls|xlsx|zip))$ {
        try_files $uri @[USER=7374]FallBack[/USER];
    }

    location ~ ^/~(.+?)(/.*)?$ {
        proxy_pass http://x.x.x.x:7080;
        proxy_set_header Host            $host;
        proxy_set_header X-Real-IP        $remote_addr;
        proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
        access_log off;
    }

    location /protected_directory {
        auth_basic  "protected directory";
        auth_basic_user_file  /var/www/vhosts/system/site.com/pd/filewithpassword;
}

  location ~ ^/admin/.*\.php$ {
        auth_basic  "";
      auth_basic_user_file  /var/www/vhosts/system/site.com/pd/filewithpassword;
    proxy_pass http://x.x.x.x:7080;
        proxy_set_header Host            $host;
        proxy_set_header X-Real-IP        $remote_addr;
        proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
}


    location ~ \.php(/.*)?$ {
        fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_pass "unix:/var/www/vhosts/system/site.com/php-fpm.sock";
        include /etc/nginx/fastcgi.conf;    }

    location ~ /$ {
        try_files $uri $uri/ /index.php?$uri&$args;
        index index.php index.html index.htm index.cgi index.xhtml index.pl index.shtml;
    }


}
 
Last edited:
I move in my plesk nginx as reverse proxy for apache to nginx server directly php files via php-fpm.
Hello,

I'm not sure I understand what you are trying to achieve, but from what you say, you want to use Nginx as main server, combined with php-fpm? If that is the case, your entire configuration is wrong. Start with this basic format and read below how to password protect a directory.

A quick tip: location ~ /(internal_data|library)
will simply not work. This is not how you are supposed to use alternate locations in Nginx. Try to access any of the above directories, you will see that a 404 is not returned.
 
Hey @Floren nice to see you, yes i config a nginx with php-fpm, all is working fine except that i receive a 502 bad gateway configuration when i try to access to my protected directory.
Is a problem with plesk because the php-fpm error log show that i don't know nginx try to redirect to apache.
 
That's the problem, i install nginx with php-fpm many times in many servers without problems, this new server come with plesk, and i dont understand well that is doing inside, im thinking in simply delete it :D
Is too strange, i have apache stopped, all xenforo web is working fine, permalinks, url friendly, served by nginx with php-fpm, but if i want to access to any of the sub directories that i have in domain, for example my folder xcache, club, test, i receive a 502 bad gteway because plesk redirect this subdirectories to apache via 7080 port.
I review all files hierarchy of plesk but i cant found in what point nginx is defined like a reverse proxy instead of to serve files directly.
In other words, uninstall plesk, install webmin, and solve the problem.
 
Top Bottom