Set Nginx browsing directory permissions for LNMP

Source: Internet
Author: User

When learning Linux, the Web environment is LNMP. When browsing the directory during use, the system prompts that you do not have permission. To enable this function, perform the following operations:

Open the nginx. conf configuration file

Add autoindex on to location server or http code snippet

You can use the above two settings, but the following two parameters can be selected based on your needs.

Parameter: autoindex_exact_size off;

Note: The default value is on. The exact size of the file is displayed, in bytes. After changing to off, the approximate size of the file is displayed. The unit is kB, MB, or GB.

Parameter: autoindex_localtime on;

Note: The default value is off. The displayed file Time Is GMT. After changing to on, the displayed File time is the file server time


For example, make the/var/www/soft directory list in the browser.

1. Set directory browsing

1. Open/usr/local/nginx/conf/nginx. conf, find the WebServer configuration, and add the following content:

Location/soft /{
Root/var/www/; the above directory of soft
Autoindex on;
Autoindex_exact_size off;
Autoindex_localtime on;
}

2. Save and exit. Restart the nginx service.


[Root @ localhost Soft] # ps aux | grep-v grep | grep nginx | awk '{print $2}' | xargs kill-9 # terminate the process

[Root @ localhost Soft] # nginx # start the process

Visit your site: http: // loalhost/soft to list the directories.

In this case, the contents of your directory can be easily seen by others. Next we will add an access verification for the directory like Apache.

2. Set access verification

1. Create a class htpasswd file

[Root @ localhost Soft] # wget-c http://jafee.net/Soft/InstallPack/htpasswd.sh

[Root @ localhost Soft] # bash htpasswd. sh

Prompt to enter the user name, password, and authentication file name. The script will automatically generate the authentication file. The default path is saved under/usr/local/nginx/conf, if your nginx directory is not here, you can modify htpasswd. sh to replace your nginx directory.

Here I am:/usr/local/nginx/conf/test. conf # write down this path

2. Add auth authentication configuration for Nginx

Location ^ ~ /Soft/
{
Auth_basic "MyPath Authorized ";
Auth_basic_user_file/usr/local/nginx/conf/test. conf; # write the file path returned by the preceding script here;
}

# "MyPath Authorized" is the prompt information, which can be modified by yourself.

3. After the configuration is modified, restart nginx and access http: // localhost/soft/. A prompt is displayed, indicating that the user name and password are entered. After successful authentication, the directory can be listed.

4. Note that the php file under the Directory will not be parsed after authentication, and you will be downloaded. If you want to enable php parsing, you can change the above configuration:

Location ^ ~ /Soft /{
Location ~ . * \. (Php | php5 )? $ {
Fastcgi_pass unix:/tmp/php-cgi.sock;
Fastcgi_index index. php;
Fcinclude GI. conf;
}
Auth_basic "Authorized users only ";
Auth_basic_user_file/usr/local/nginx/conf/test. conf;
}

Finally, restart Nginx.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.