首先,要增加nginx的新的模块,那么就不要使用apt-get install nginx安装,而是使用源码编译安装,之后再添加新的模块,主要原因是使用apt-get安装会找不到./configure 文件,至于怎么使用源码安装和添加新的模块的命令,网上已经有很多的教程,在此博主也不再赘述,下面附上我的nginx.conf文件,给大家做一个参考。
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
rtmp {
server {
listen 1935;
application live {
live on;
allow publish 127.0.0.1;
allow play all;
record all;
record_path /home/yjwang/record;
record_max_size 100M;
record_unique off;
}
}
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log;
error_log logs/error.log;
#access_log /var/log/nginx/access.log;
#error_log /var/log/nginx/error.log;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
#keepalive_timeout 0;
keepalive_timeout 65;
types_hash_max_size 2048;
gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#location / {
# root html;
# index index.html index.htm;
# }
location / {
root /home/yjwang/ssd_space/ficus2;
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
增加用户权限管理,即查看某些文件目录时,需要输入用户名和密码:
修改如下:
location / {
66 root /home/yjwang/ssd_space/ficus2;
67 auth_basic "Restricted";
68 auth_basic_user_file /usr/local/nginx/conf/pass_file;
69 autoindex on;
70 autoindex_exact_size on;
71 autoindex_localtime on;
72 }
然后reload让新的配置文件生效
之后就是生成用户和密码,添加用户,nginx自带了一个功能,如下
htpasswd -c -d /usr/local/nginx/conf/pass_file name
这样就在/usr/local/nginx/conf/pass_file
中添加了了一个用户
这样我们一个简单的文件服务器就搭建完成了
注意:
搭建nginx后,为了让文件可见,可以通过下面的命令更改共享目录的权限
chmod 755 -R 目录,可以将指定目录及其下面的子目录、子文件调整成可读。
接下来还有一个问题,那就是加上用户权限后如何访问呢?
经过我的探索,终于总结了几种方法:
1. 浏览器中使用 直接在浏览器中输入地址, 会弹出用户密码输入框, 输入即可访问
2. 使用 wget wget --http-user=XXX --http-passwd=XXX url(此处的url指向一个文件)
3. 使用 curl curl -u username:password -O url(此处的url指向一个文件)
4.最后重点来了,使用ajax实现自动下载文件:
headers.set('Authorization', 'Basic ' + base64.encode(username + ":" + password)) ;
注意Basic后面有一个空格哦
好了,这几种方法基本满足了我们大部分的访问请求。