This is the third day of my participation in the August Text Challenge.More challenges in August
This article mainly introduces some basic functions and simple configuration of Nginx, but does not include the installation, deployment and implementation principle of Nginx. Without further ado, let’s get started.
Static HTTP server
First of all, Nginx is an HTTP server, which can present static files (such as HTML and images) on the server to the client through HTTP protocol. Configuration:
server { listen 80; Location / {root /usr/share/nginx/ HTML; # static file path}}Copy the code
2. Reverse proxy server
What is a reverse proxy?
The client can directly access a web application server through HTTP. If the web administrator adds an Nginx in the middle, the client requests Nginx, and Nginx requests the application server, and then sends the result back to the client, Nginx is the reverse proxy server.
Configuration:
server { listen 80; Location / {proxy_pass http://192.168.20.1:8080; # application server HTTP address}}Copy the code
If the server can be accessed directly through HTTP, why add a reverse proxy in the middle? What does a reverse proxy do? Moving on, the following load balancing, virtual hosting, are based on reverse proxy implementation, of course, reverse proxy is not only these functions.
3. Load balancing
When website visit volume is very big, website webmaster is happy while making money, also spread on thing. Because the site is getting slower and slower, one server is no longer enough. The same application is deployed on multiple servers and requests from a large number of users are distributed across multiple machines. The advantage is that if one of the servers is down, as long as other servers are running properly, users will not be affected.
Nginx can implement load balancing through reverse proxies.Configuration:
Upstream myapp {server 192.168.20.1:8080; # application server 1 192.168.20.2:8080 2} server {listen 80; location / { proxy_pass http://myapp; }}Copy the code
4. Virtual host
A large number of websites are accessed and load balancing is required. However, not all sites are so good, some sites, because the traffic is too small, the need to save costs, multiple sites deployed on the same server.
For example, the two websites www.aaa.com and www.bbb.com are deployed on the same server, and the two domain names are resolved to the same IP address, but users can open two completely different websites through the two domain names, and do not affect each other, just like accessing two servers, so it is called two virtual hosts.
server { listen 80 default_server; server_name _; return 444; } server {listen 80; server_name www.aaa.com; # www.aaa.com domain location / {proxy_pass http://localhost:8080; Server {listen 80; server_name www.bbb.com; # www.bbb.com domain location / {proxy_pass http://localhost:8081; # port 8081}}Copy the code
An application is enabled on servers 8080 and 8081 respectively. The client accesses the application server using different domain names. The application server can be reverse-proxy to the corresponding application server based on server_name.
The principle of virtual Host is through the HTTP request header Host match server_name to achieve, interested students can study HTTP protocol.
In addition, the server_name configuration can also filter out malicious attempts to point certain domain names to your host server.
5, FastCGI
Nginx itself does not support languages such as PHP, but it can use FastCGI to throw requests to languages or frameworks (such as PHP, Python, Perl).
server { listen 80; location ~ \.php$ { include fastcgi_params; $fastcgi_script_name; $fastcgi_script_name; # PHP file path fastcgi_pass 127.0.0.1:9000; Unix :/var/run/php5-fpm. Sock; }}Copy the code
Requests ending in.php are configured to be handled by php-fpm via FashCGI, a FastCGI manager for PHP. Other information about FashCGI is available and not covered in this article.
What is the difference between fastcgi_pass and proxy_pass? Here’s a picture to take you through: