


Example analysis of nginx, apache's alias and authentication functions
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
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
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!

NGINX can be used to serve files and manage traffic. 1) Configure NGINX service static files: define the listening port and file directory. 2) Implement load balancing and traffic management: Use upstream module and cache policies to optimize performance.

NGINX is suitable for handling high concurrency and static content, while Apache is suitable for dynamic content and complex URL rewrites. 1.NGINX adopts an event-driven model, suitable for high concurrency. 2. Apache uses process or thread model, which is suitable for dynamic content. 3. NGINX configuration is simple, Apache configuration is complex but more flexible.

NGINX and Apache each have their own advantages, and the choice depends on the specific needs. 1.NGINX is suitable for high concurrency, with simple deployment, and configuration examples include virtual hosts and reverse proxy. 2. Apache is suitable for complex configurations and is equally simple to deploy. Configuration examples include virtual hosts and URL rewrites.

The purpose of NGINXUnit is to simplify the deployment and management of web applications. Its advantages include: 1) Supports multiple programming languages, such as Python, PHP, Go, Java and Node.js; 2) Provides dynamic configuration and automatic reloading functions; 3) manages application lifecycle through a unified API; 4) Adopt an asynchronous I/O model to support high concurrency and load balancing.

NGINX started in 2002 and was developed by IgorSysoev to solve the C10k problem. 1.NGINX is a high-performance web server, an event-driven asynchronous architecture, suitable for high concurrency. 2. Provide advanced functions such as reverse proxy, load balancing and caching to improve system performance and reliability. 3. Optimization techniques include adjusting the number of worker processes, enabling Gzip compression, using HTTP/2 and security configuration.

The main architecture difference between NGINX and Apache is that NGINX adopts event-driven, asynchronous non-blocking model, while Apache uses process or thread model. 1) NGINX efficiently handles high-concurrent connections through event loops and I/O multiplexing mechanisms, suitable for static content and reverse proxy. 2) Apache adopts a multi-process or multi-threaded model, which is highly stable but has high resource consumption, and is suitable for scenarios where rich module expansion is required.

NGINX is suitable for handling high concurrent and static content, while Apache is suitable for complex configurations and dynamic content. 1. NGINX efficiently handles concurrent connections, suitable for high-traffic scenarios, but requires additional configuration when processing dynamic content. 2. Apache provides rich modules and flexible configurations, which are suitable for complex needs, but have poor high concurrency performance.

NGINX and Apache each have their own advantages and disadvantages, and the choice should be based on specific needs. 1.NGINX is suitable for high concurrency scenarios because of its asynchronous non-blocking architecture. 2. Apache is suitable for low-concurrency scenarios that require complex configurations, because of its modular design.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)
