- 创建类htpasswd文件(如果没有htpasswd命令,可通过
yum install -y *htpasswd*或yum install -y httpd) htpasswd -c /usr/local/nginx/conf/auth_password wangshibo//会被要求输入两次密码cat /usr/local/nginx/conf/auth_password- Nginx配置中添加auth认证配置
location ^~ /soft/ {root /var/www/html; #此处为soft的上一级目录。注意root和alias虚拟目录设置区别autoindex on;autoindex_exact_size off;autoindex_localtime on;auth_basic "MyPath Authorized"; #为提示信息,可以自行修改;会出现在第一次访问Nginx站点的弹出框内auth_basic_user_file /usr/local/nginx/conf/auth_password; #最好跟htpasswd文件的全路径}
重启nginx服务
/usr/local/nginx/sbin/nginx -s reload需要特别注意的是:
加上认证之后该目录下的php文件将不会被解析,会运行下载。如果要使其能够解析php可以,可将上面的配置改为:
location ^~ /soft/ {location ~ \.php$ { #或者是location ~ .*\.(php|php5)?$ {root /var/www/html;fastcgi_pass 127.0.0.1:9000;fastcgi_read_timeout 300;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;;include fastcgi.conf;}auth_basic "Authorized users only";auth_basic_user_file /usr/local/nginx/conf/auth_password;}
nginx运行目录浏览后,就可以利用wget进行文件远程传输了(只针对文件,因为在http下只能文件访问,直接跟url访问目录是404)。
