Basic configuration
server { listen 80; server_name file.52itstyle.com; charset utf-8; #root 指令用来指定文件在服务器上的基路径 root /data/statics; #location指令用来映射请求到本地文件系统 location / { autoindex on; # 索引 autoindex_exact_size on; # 显示文件大小 autoindex_localtime on; # 显示文件时间 } }
Restart the nginx service:
nginx -s reload
Access the file service, http://file.52itstyle.com/
Set Password
The htpasswd command is apache's built-in tool for the web server, used to create and update stored usernames, domains, and users Password file for basic authentication.
htpasswd(option)(parameter)
-c: Create an encrypted file;
-n: Do not update Encrypt the file and only display the encrypted username and password on the screen;
-m: By default, the md5 algorithm is used to encrypt the password;
-d: Use the crypt algorithm to encrypt the password;
-p: The password is not encrypted, that is, the plain text password;
-s: Use the sha algorithm to encrypt the password;
-b: Enter the user name and password together in the command line instead of entering the password according to the prompt;
-d: Delete the specified user.
Example
htpasswd -bc passwd.db itstyle 123456
Generate a passwd.db file in the directory, username itstyle, password: 123456, md5 encryption is used by default.
Add the next user to the original password file
htpasswd -b passwd.db admin 123456
nginx configuration
server { listen 80; server_name file.52itstyle.com; charset utf-8; root /data/share; location / { autoindex on; # 索引 autoindex_exact_size on; # 显示文件大小 autoindex_localtime on; # 显示文件时间 auth_basic "请输入用户名密码"; auth_basic_user_file /usr/local/openresty/nginx/passwd.db; } }
Restart nginx access:
Picture anti-hotlinking
If the server's pictures are hotlinked by other websites, it will affect the server's bandwidth and access speed. At this time, we need to set the anti-hotlinking function for image files or video files. .
Anti-hotlinking function, simply put, you can access the resource directly, but you cannot put my resource link on your own server for others to access, especially for larger files such as pictures or videos. , which can easily cause the server to respond slowly.
server { listen 80; server_name file.52itstyle.com; charset utf-8; #root 指令用来指定文件在服务器上的基路径 root /data/statics; #location指令用来映射请求到本地文件系统 location ~*^.+\.(gif|jpg|png|jpeg)$ { expires 30d; valid_referers none blocked file.52itstyle.com; if ($invalid_referer) { rewrite ^/ http://www.52itstyle.com/404.jpg; } } }
Restart the nginx service and test the image link: http://file.52itstyle.com/nfs_c.png
The above is the detailed content of How to configure Nginx static file server. For more information, please follow other related articles on the PHP Chinese website!