在Nginx的nginx.conf里面有如下的字段
- server
- {
- listen 80 default_server;
- #listen [::]:80 default_server ipv6only=on;
- server_name www.nenew.net;
- index index.html index.htm index.php;
- root /home/wwwroot/default;
- #error_page 404 /404.html;
- include enable-php.conf;
- location /nginx_status
- {
- stub_status on;
- access_log off;
- }
- location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
- {
- expires 30d;
- }
- location ~ .*\.(js|css)?$
- {
- expires 12h;
- }
- location ~ /\.
- {
- deny all;
- }
- access_log /home/wwwlogs/access.log access;
- }
- include vhost/*.conf;
- }
如果我们想在默认的目录添加密码保护,只保护对目录的访问,也就是登陆这个目录就会输入密码,则我们这样设置
- server
- {
- listen 80 default_server;
- #listen [::]:80 default_server ipv6only=on;
- server_name www.nenew.net;
- index index.html index.htm index.php;
- root /home/wwwroot/default;
- #error_page 404 /404.html;
- include enable-php.conf;
- location /nginx_status
- {
- stub_status on;
- access_log off;
- }
- location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
- {
- expires 30d;
- }
- location ~ .*\.(js|css)?$
- {
- expires 12h;
- }
- location ~ /\.
- {
- deny all;
- }
- location /
- {
- auth_basic “Restricted”;
- auth_basic_user_file pass_file;
- }
- access_log /home/wwwlogs/access.log access;
- }
- include vhost/*.conf;
- }
其中pass_file是密码文件的绝对路径,密码文件是由用户名和函数 crypt加密的密码组成,可以使用 htpasswd -c -d pass_file username 来生成。