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

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

May 24, 2023 pm 11:10 PM
apache nginx alias

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 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to configure cloud server domain name in nginx How to configure cloud server domain name in nginx Apr 14, 2025 pm 12:18 PM

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

How to check nginx version How to check nginx version Apr 14, 2025 am 11:57 AM

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

How to check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to start nginx server How to start nginx server Apr 14, 2025 pm 12:27 PM

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

How to configure nginx in Windows How to configure nginx in Windows Apr 14, 2025 pm 12:57 PM

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to solve CentOS system failure How to solve CentOS system failure Apr 14, 2025 pm 01:57 PM

There are many ways to solve CentOS system failures. Here are some common steps and techniques: 1. Check the log file /var/log/messages: system log, which contains various system events. /var/log/secure: Security-related logs, such as SSH login attempts. /var/log/httpd/error_log: If you use the Apache server, there will be an error message here. 2. Use the diagnostic tool dmesg: display the contents of the kernel ring buffer, which helps understand hardware and driver questions

How to build a Zookeeper cluster in CentOS How to build a Zookeeper cluster in CentOS Apr 14, 2025 pm 02:09 PM

Deploying a ZooKeeper cluster on a CentOS system requires the following steps: The environment is ready to install the Java runtime environment: Use the following command to install the Java 8 development kit: sudoyumininstalljava-1.8.0-openjdk-devel Download ZooKeeper: Download the version for CentOS (such as ZooKeeper3.8.x) from the official ApacheZooKeeper website. Use the wget command to download and replace zookeeper-3.8.x with the actual version number: wgethttps://downloads.apache.or

See all articles