Nginx static file server configuration

In Java development and production environments, the most commonly used web application server comes as Tomcat, although the cat can handle some static requests, such as images, html, style files, but the efficiency is not so do everyone. In a production environment, we generally use Nginx proxy server to handle static files, to improve site performance.

basic configuration

server {
    listen 80;
    server_name file.52itstyle.com;
    charset utf-8;
 #root instruction group for a specified file path on the server
    root /data/statics;
 #location command is used to map the request to the local file system
    location / {
      autoindex on; # Index
      autoindex_exact_size on; # display the file size
      autoindex_localtime on; # display the file time
    }
  }

Restart Nginx services:

nginx -s reload

Access file services, http: //file.52itstyle.com/

 

set password

htpasswd command is the Apache Web server built-in tools for creating and updating the stored user name, domain, and basic user authentication password file.

htpasswd (option) (parameters)

  1. -c: Create an encrypted file;
  2. -n: Do not update encrypted files, only the user name encrypted password displayed on the screen;
  3. -m: default MD5 algorithm to encrypt the password;
  4. -d: CRYPT algorithm using the password encryption;
  5. -p: do not encrypt passwords that clear text passwords;
  6. -s: SHA algorithm to encrypt the password;
  7. -b: the command line along with a user name and password rather than prompted for a password;
  8. -D: delete the specified user.

Examples

htpasswd -bc passwd.db itstyle 123456

Passwd.db generate a file in the directory, the user name itstyle, Password: 123456, defaults MD5 encryption.

Increase the next user in 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; # Index
      autoindex_exact_size on; # display the file size
      autoindex_localtime on; # display the file time
      auth_basic " Please enter your user name and password " ;
      auth_basic_user_file /usr/local/openresty/nginx/passwd.db;
    }
  }

Restart Nginx visit:

Picture anti-theft chain

If the server is hotlinking images to other sites, will affect bandwidth and access speed of the server, then we need to set the security chain function image files or video files.

Anti-hotlinking function, simply, is that you can directly access the resource, but not my resource link on your own server for others to access, especially pictures or videos of this relatively large files, very easily lead to server response slow.

server {
    listen 80;
    server_name file.52itstyle.com;
    charset utf-8;
 #root instruction group for a specified file path on the server
    root /data/statics;
 #location command is used to map the request to the local file system
    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 Nginx service, test image link: http: //file.52itstyle.com/NFS_C.png

 

Guess you like

Origin www.cnblogs.com/aaron911/p/11006334.html