Home  >  Article  >  Operation and Maintenance  >  Example analysis of nginx, apache's alias and authentication functions

Example analysis of nginx, apache's alias and authentication functions

WBOY
WBOYforward
2023-05-24 23:10:22779browse

First take a look at how to configure the apache alias:

Copy the code The code is as follows:


documentroot /www/jb51. net/www This is the root directory of the virtual host, but phpmyadmin is not in this directory and wants to access it.
servername www.jb51.net
serveralias jb51.net
alias /sdb "/www/public/phpmyadmin/" You need the alias function: //www.jb51.net/sdb This is much safer .

options indexes followsymlinks
allowoverride none
order allow,deny
allow from all

1.apache authentication

Type of authentication: basic
digest summary
Authentication method: a. Container authentication: ……
b. Create .htaccess file for hidden file authentication
Method 1. Container authentication
a. Enter the configuration file vi /etc/httpd/conf/httpd.conf
b. Configuration: approx. The configuration near line 531 is as follows:

allowoverride none ##Do not allow hidden authentication, that is, container authentication
authtype basic ##The authentication type is basic
authname "ajian" ##The authentication name is ajian
authuserfile /var/www/passwd/pass ##pass is the authentication password file and specifies the location where the password file is stored.
require valid-user ## Valid user (note the case, some cases change due to word)
c. Create the directory mkdir -p /var/www/passwd
Enter the directory cd /var /www/passwd
d, create apache user htpasswd -c pass ajian ##pass is the password file ajian is the user
Change the use rights of the pass file to apache: chown apache.apache pass
Attachment: Then Add a user to the pass file: htpasswd pass tt ##Add a tt user to the pass file
e, restart the service and test
Method 2, pass hidden authentication
Similar to the above but the configuration is different
httpd main configuration file

allowoverride authconfig
Create a hidden file and place it in the directory to be authenticated
eg: vi /var/www/html/mrtg
authtype basic
authname “ajian”
authuserfile /var/www/passwd/pass
require valid-user

Here are examples

Example analysis of nginx, apaches alias and authentication functions

Example analysis of nginx, apaches alias and authentication functions

Example analysis of nginx, apaches alias and authentication functions

2. nginx login authentication

nginx’s http auth basic password is encrypted with crypt(3) of. Use apache's htpasswd to generate a password file.
Install it yourself without apache. 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 means to generate the file, and -d is to encrypt with crypt.
vi nginx.conf cd /usr/local/nginx/conf /usr/local/apache2/bin/htpasswd -c -d pass_file user_name #Enter the password, -c means to generate the file, -d is to encrypt with crypt . vi nginx.conf Add authorization statements to the nginx.conf file. It should be noted here that starting from 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 code The 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 directories Authentication, in a separate location, and a location that interprets php is nested in the location, otherwise the php file will not be executed and will be downloaded. auth_basic comes after the nested location.

Copy code The 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;
}



##三.nginx alias function configuration automatically lists the directory

Copy code The code is as follows:


server {

listen www.jb51.net:88;

server_name www.jb51.net;

autoindex on; //Open the directory Function.

# charset gbk;
location /club { Name of visit //www.jb51.net:88/club
alias /www/clublog/club.xywy.com/; This is the server The place where logs are stored
} This means that when you visit www.jb51.net:88/club, you will see the contents of the club directory.
location /{
root /www/access;
This location does not need to be www.jb51.net:88. What comes out is the default nxing page
# index index.html index.htm index. php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}



The above nginx configuration means: When accessing http://hou.xywy.com/:88 authentication, the directory in /www/access/ on the server is accessed by default. After authentication, url=http: //hou.xywy.com:88/club will display the contents of the directory in /www/clublog/club.xywy.com/. , it may be confusing, just analyze it carefully.

The difference between root and alias.
The most basic difference: the directory specified by alias is accurate, root is the superior directory of the specified directory, and the superior directory must contain the same name directory with the name specified by location. In addition, as mentioned above, rewrite's break cannot be used in a directory block using the alias tag.

This will be very clear after reading this paragraph,

Copy the code The code is as follows:


location /abc/ {
alias /home /html/abc/;
}


Under this configuration, http://test/abc/a.html specifies /home/html/abc/ a.html. This configuration can also be changed to

Copy code The code is as follows:


location /abc/ {
root /home/html/;
}


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

However, if I change the configuration of alias to:

Copy code The code is as follows:


location /abc/ {
alias / home/html/def/;
}


Then nginx will fetch data from /home/html/def/. This configuration cannot directly use root configuration. If not To configure, you only need to create a soft link (shortcut) of def->abc under /home/html/.

Generally, it is a good habit to configure root in location / and alias in location /other.

The above is the detailed content of Example analysis of nginx, apache's alias and authentication functions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete