Home > Operation and Maintenance > Nginx > How to configure Nginx static file server

How to configure Nginx static file server

WBOY
Release: 2023-05-15 18:04:14
forward
1860 people have browsed it

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; # 显示文件时间
    }
  }
Copy after login

Restart the nginx service:

nginx -s reload
Copy after login

Access the file service, http://file.52itstyle.com/

How to configure Nginx static file server

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)

  1. -c: Create an encrypted file;

  2. -n: Do not update Encrypt the file and only display the encrypted username and password on the screen;

  3. -m: By default, the md5 algorithm is used to encrypt the password;

  4. -d: Use the crypt algorithm to encrypt the password;

  5. -p: The password is not encrypted, that is, the plain text password;

  6. -s: Use the sha algorithm to encrypt the password;

  7. -b: Enter the user name and password together in the command line instead of entering the password according to the prompt;

  8. -d: Delete the specified user.

Example

htpasswd -bc passwd.db itstyle 123456
Copy after login

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
Copy after login

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;
    }
  }
Copy after login

Restart nginx access:

How to configure Nginx static file server

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;
       }
    }
  }
Copy after login

Restart the nginx service and test the image link: http://file.52itstyle.com/nfs_c.png

How to configure Nginx static file server

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template