Alias and authentication functions of nginx and apache

Source: Internet
Author: User
Tags crypt

I haven't written anything since I switched my computer to linux a year ago. Recently I am a little lazy. Here I will talk about the nginx alias function, not the server alias.
First, let's take a look at how apache alias is configured:

Copy codeThe Code is as follows: <VirtualHost *: 80>
DocumentRoot/www/jb51.net/www this is the root directory of the VM, but phpMYadmin is not in this directory and you want to access it.
ServerName www.jb51.net
ServerAlias jb51.net
Alias/sdb "/www/public/phpMyAdmin/" requires Alias function,: http://www.jb51.net/sdb so much more secure.
<Directory "/www/public/phpMyAdmin/">
Options Indexes FollowSymLinks
AllowOverride None
Order allow, deny
Allow from all
</Directory>
</VirtualHost>

1. Apache Authentication

Authentication Type: Basic
Digest Abstract
Authentication Method: A. Container authentication :......
B. Create a. htaccess file by hiding the file Authentication
Method 1: Container Authentication
A. Enter the configuration file vi/etc/httpd/conf/httpd. conf.
B. Configuration: About 531 rows are configured as follows:

AllowOverride None # container authentication is not allowed.
AuthType Basic # the authentication type is Basic
AuthName "ajian" # Authentication Name Is Ajian
AuthUserFile/var/www/passwd/pass # pass is the authentication password file, specifying the location where the password file is stored.
Require valid-user # valid users (note that the case sensitivity changes due to Word)
C. Create the directory mkdir-p/var/www/passwd.
Go to the directory cd/var/www/passwd
D. Create an Apache user htpasswd-c pass ajian # pass as the password file Ajian as the user
Change the permission to use the Pass file to Apache: chown apache. apache pass
Appendix: Add a user in the Pass file: htpasswd pass tt # Add a TT user to the Pass file.
E. Restart the service and Test
Method 2: Pass the hidden Authentication
Similar to the above but not the same as the configuration above
Httpd main configuration file

AllowOverride AuthConfig
Create a hidden file and put it in the directory to pass authentication
Eg: vi/var/www/html/mrtg
AuthType Basic
AuthName "Ajian"
AuthUserFile/var/www/passwd/pass
Require valid-user

The following is an example

Ii. Nginx logon authentication

The http auth basic password of nginx is encrypted with crypt (3. Apache htpasswd can be used to generate a password file.
You have not installed apache on your own. I installed apache2,/usr/local/apach2.
Cd/usr/local/nginx/conf/usr/local/apache2/bin/htpasswd-c-d pass_file user_name # Press enter to enter the password.-c indicates generating the file, -d is encrypted with crypt.
Vi nginx. conf cd/usr/local/nginx/conf/usr/local/apache2/bin/htpasswd-c-d pass_file user_name # Press enter to enter the password.-c indicates generating the file, -d is encrypted with crypt. Vi nginx. conf: add the authorization statement to the nginx. conf file. Note that at nginx 0.6.7, the relative directory of auth_basic_user_file is nginx_home/conf, and the relative directory of previous versions is nginx_home.

Copy codeThe Code is as follows: server {
Listen 80;
Server_name tuan.xywy.com;
Root/www/tuangou;
Index index.html index.htm index. php;
Autoindex on;
Auth_basic "input you user name and password ";
Auth_basic_user_file htpasswd. file;
Location ~ . Php $ {
Fastcgi_pass 127.0.0.1: 9000;
Fastcgi_index index. php;
Fastcgi_param SCRIPT_FILENAME/www/tuangou $ fastcgi_script_name;
Include fastcgi_params;
}
Error_page 404/404 .php;
Error_page 403/404 .php;

Access_log/logs/tuan_access.log main;
}

For directory authentication, in a separate location, and nested a location that explains php, otherwise the php file will not be executed and downloaded. Auth_basic follows the nested location.Copy codeThe Code is as follows: server {
Listen 80;
Server_name tuan.xywy.com;
Root/www/tuangou;
Index index.html index.htm index. php;
Autoindex on;
Location ~ ^/Admin /.*{
Location ~ \. Php $ {
Fastcgi_pass 127.0.0.1: 9000;
Fastcgi_index index. php;
Fastcgi_param SCRIPT_FILENAME/www/tuangou $ fastcgi_script_name;
Include fastcgi_params;
}
Root/www/tuangou /;
Auth_basic "auth ";
Auth_basic_user_file htpasswd. file;
}

Location ~ . Php $ {
Fastcgi_pass 127.0.0.1: 9000;
Fastcgi_index index. php;
Include fastcgi_params;
}

Access_log/logs/tuan_access.log main;
}

Iii. nginx alias function configuration automatic column directory

Copy codeThe Code is as follows: server {

Listen www.jb51.net: 88;

Server_name www.jb51.net;

Autoindex on; // enable the column directory function.

# Charset gbk;
Location/club {name of the accessed http://www.jb51.net: 88/club
Alias/www/clublog/club.xywy.com/; this is the place where logs are stored on the server.
} Visit www.jb51.net: 88/club to see the club directory.
Location /{
Root/www/access;
The default nxing page does not appear in the location section www.jb51.net: 88.
# Index index.html index.htm index. php;
}
Error_page 500 502 503 x.html;
Location =/50x.html {
Root html;
}
}

The nginx configuration above indicates that the contents of the directory in/www/clublog/club.xywy.com/are displayed when the ingress is accessed ., It may be difficult. Just analyze it carefully.

Root and alias.
The most basic difference is that the directory specified by alias is accurate. root is the parent directory of the specified directory, and the parent directory must contain a directory with the same name as that specified by location. In addition, rewrite break cannot be used in directory blocks using the alias label.

In this way, we can see this section clearly,Copy codeThe Code is as follows: location/abc /{
Alias/home/html/abc /;
}

In this section, http: // test/abc/a.html specifies/home/html/abc/a.html. This configuration can also be changedCopy codeThe Code is as follows: location/abc /{
Root/home/html /;
}

In this way, nginx will find the abc directory under the/home/html/directory, and the results will be the same.

However, if I change the alias configuration:Copy codeThe Code is as follows: location/abc /{
Alias/home/html/def /;
}

Nginx will retrieve data from/home/html/def/. This configuration cannot be configured directly with the root user, only a soft link (shortcut) with def-> abc can be created under/home/html.

In general, configuring root in location/and configuring alias in location/other is a good habit.

As for the difference between alias and root, I guess it's not completely done yet. If you find a strange problem during configuration, try replacing the two.

At the beginning, I also started to solve the CGI problem for a long time, including authentication of a separate directory. I hope everyone will succeed. If you have any questions, contact me for help!

This article is from the blog "learning is everlasting ".

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.