Home > Operation and Maintenance > Nginx > Nginx basic knowledge introduction example analysis

Nginx basic knowledge introduction example analysis

PHPz
Release: 2023-05-17 21:48:23
forward
1491 people have browsed it

nginx is a high-performance http and reverse proxy server known for its high stability, rich feature set, sample configuration files, and low system resource consumption.

nginx Features Process static files, index files and automatic indexing; open file descriptor buffering. Cacheless reverse proxy acceleration, simple load balancing and fault tolerance. fastcgi, simple load balancing and fault tolerance. Modular structure. Including gzipping, byte ranges, chunked responses, and ssi-filter and other filters. If multiple ssi's present in a single page are processed by fastcgi or another proxy server, this processing can run in parallel without waiting for each other. Support ssl and tlssni. Main application scenarios 1. Static http server

First of all, nginx is an http server that can display static files (such as html and pictures) on the server to the client through the http protocol.

Configuration:

server {
	listen 80; # 端口号
	location / {
		root /usr/share/nginx/html; # 静态文件路径
	}
}
Copy after login

2. Reverse proxy server

What is a reverse proxy?

The client can directly access a website application server through the http protocol. If the website administrator adds an nginx in the middle, the client requests nginx, nginx requests the application server, and then returns the result to the client. This nginx is a reverse proxy server.

Nginx basic knowledge introduction example analysis

Configuration:

server {
	listen 80;
	location / {
		proxy_pass http://192.168.20.1:8080; # 应用服务器http地址
	}
}
Copy after login

Since the server can be accessed directly through http, why should we add a reverse proxy in the middle? Isn’t it unnecessary? What does a reverse proxy do? Continuing to look down, the following load balancing and virtual hosts are all implemented based on reverse proxy. Of course, the functions of reverse proxy are not limited to these.

3. Load Balancing

When the website traffic is very large, the webmaster is happy to make money, but at the same time he is also in trouble. Because the website is getting slower and slower, one server is no longer enough. So the same application is deployed on multiple servers, and requests from a large number of users are distributed to multiple machines for processing. At the same time, the benefit is that if one of the servers crashes, as long as other servers are running normally, it will not affect the user's use.

When our website undergoes a major upgrade, it is impossible for us to directly shut down all servers and then upgrade. Usually we shut down some servers in batches to upgrade the website, and when there are user requests, allocate them to other running machines for processing. After the previously turned off machines are updated, turn them on again, and then turn off some machines in batches, repeating the above cycle until all machines are finally updated. This will not affect user use.

nginx can achieve load balancing through reverse proxy.

Nginx basic knowledge introduction example analysis

Configuration:

upstream myapp {
	server 192.168.20.1:8080; # 应用服务器1
	server 192.168.20.2:8080; # 应用服务器2
}
server {
	listen 80;
	location / {
		proxy_pass http://myapp;
	}
}
Copy after login

4. Virtual host

The website has a large number of visits and requires load balancing. However, not all websites are so excellent. Some websites need to save costs by deploying multiple websites on the same server because the number of visits is too small.

For example, if two websites www.aaa.com and www.bbb.com are deployed on the same server, the two domain names resolve to the same IP address, but the user can open two through the two domain names. Completely different websites do not affect each other, just like accessing two servers, so they are called two virtual hosts.

Configuration:

server {
	listen 80 default_server;
	server_name _;
	return 444; # 过滤其他域名的请求,返回444状态码
}
server {
	listen 80;
	server_name www.aaa.com; # www.aaa.com域名
	location / {
		proxy_pass http://localhost:8080; # 对应端口号8080
	}
}
server {
	listen 80;
	server_name www.bbb.com; # www.bbb.com域名
	location / {
		proxy_pass http://localhost:8081; # 对应端口号8081
	}
}
Copy after login

Open an application on the server ports 8080 and 8081 respectively. The client accesses through different domain names and can reverse proxy to the corresponding application server according to the server_name.

The principle of virtual host is realized by whether the host in the http request header matches the server_name. Interested students can study the http protocol.

In addition, the server_name configuration can also filter out someone who maliciously points certain domain names to your host server.

5, fastcgi

nginx itself does not support languages ​​such as php, but it can throw requests to certain languages ​​or frameworks for processing (such as php, python, perl) through fastcgi.

server {
	listen 80;
	location ~ \.php$ {
		include fastcgi_params;
		fastcgi_param script_filename /php文件路径$fastcgi_script_name; # php文件路径
		fastcgi_pass 127.0.0.1:9000; # php-fpm地址和端口号
		# 另一种方式:fastcgi_pass unix:/var/run/php5-fpm.sock;
	}
}
Copy after login

In the configuration, requests ending in .php are handed over to php-fpm for processing through fashcgi. php-fpm is a fastcgi manager of php. You can check other information about fashcgi, which will not be introduced in this article.

What is the difference between fastcgi_pass and proxy_pass? The following picture will help you understand:

Nginx basic knowledge introduction example analysis

The above is the detailed content of Nginx basic knowledge introduction example analysis. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template