我知道这可以用Apache来完成,但是我已经开始使用nginx了,想知道这是否可行,如果可以的话,我该如何实现它。
在我的networking服务器上,我有目录/ mcscreens其中包含一些图像。 该目录用h5ai索引。 我想密码保护目录,所以我可以访问/ mcscreens,login,并能够使用h5ai浏览所有的图像。 我也想直接把人们链接到特定的图像,而不需要进行身份validation。
基本上,我想密码保护目录,但不是单个文件。
我怎样才能做到这一点?
编辑:我的完整configuration,包括rmalayter的例子:
server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /var/www; index index.php index.html index.htm /mcscreens/_h5ai/server/php/index.php; # Make site accessible from http://localhost/ server_name redacted; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ /index.html; # Uncomment to enable naxsi on this location # include /etc/nginx/naxsi.rules } location /doc/ { alias /usr/share/doc/; autoindex on; allow 127.0.0.1; allow ::1; deny all; } #location for the root folder listing with or without trailing slash location ~ ^/(mcscreens|mcscreens/)$ { auth_basic "Restricted"; auth_basic_user_file /var/www-assets/passwd; } #allow retrieval of any individual image via URL without auth location ~ ^/mcscreens/* { autoindex off; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
rmalayter的例子工作,但PHP文件下载为.bin文件。
我相信这可以通过两个位置块来完成,一个用于文件夹本身,它具有自动索引,然后是一个用于文件内的文件。 这可能必须是正则expression式的位置。
未经testing的例子:
#location for the root folder listing with or without trailing slash location ~ ^/(mcscreens|mcscreens/)$ { auth_basic "Restricted"; auth_basic_user_file htpasswd; autoindex on; } #allow retrieval of any individual image via URL without auth location ~ ^/mcscreens/* { autoindex off; }