This article mainly introduces some of the most basic functions of nginx and simple configuration, but does not include nginx installation deployment and implementation principles.
1. Static HTTP Server
First, Nginx is an HTTP server that can present static files (such as HTML, pictures) on the server to the client via the HTTP protocol. Configuration:
server {
listen 80; # port number
location/{
root/usr/share/nginx/html; # static file Path
}
}
2. Reverse Proxy Server
What is a reverse proxy?
The client can access a Web application server directly through the HTTP protocol, the webmaster can add a nginx in the middle, the client requests the Nginx,nginx request the application server, then returns the result to the client, at this time Nginx is the reverse proxy server.
Configuration:
server {
listen;
Location/{
proxy_pass http://192.168.20.1:8080; # Application Server HTTP address
}
}
Since the server can directly HTTP access, why to add a reverse proxy in the middle, is not superfluous? What is the effect of reverse proxy?
Continue to look down, the following load balancing, virtual host, etc., are based on reverse proxy implementation, of course, the function of reverse proxy is not only these.
3. Load balance
When the site is very large traffic, webmaster happy to make money at the same time, also stand on the matter. Because the website is getting slower, a server is not enough.
The same application is deployed on multiple servers, and a large number of user requests are allocated to multiple machines for processing. At the same time, the benefit is that one of the servers in case of hanging, as long as there are other servers running normally, it will not affect the user use. Nginx can achieve load balancing by reverse proxy.
Configuration:
Upstream MyApp {
server 192.168.20.1:8080; # application server 1 server
192.168.20.2:8080; # Application server 2
}
server {
Listen;
Location/{
proxy_pass http://myapp;
}
}
The above configuration will request polling be allocated to the application server, that is, multiple requests from a client, which may be handled by several different servers. You can assign a request to a fixed server by Ip-hash the hash value of the client IP address.
Configuration:
Upstream MyApp {
ip_hash; the request is assigned to a fixed one server to handle server 192.168.20.1:8080 according to the client IP address hash value
;
Server 192.168.20.2:8080;
}
server {
listen;
Location/{
proxy_pass http://myapp;
}
}
In addition, the server's hardware configuration may be bad, want to allocate most of the requests to the good server, the small number of requests allocated to the poor server, can be controlled by weight.
Configuration:
Upstream MyApp {
server 192.168.20.1:8080 weight=3; # This server handles 3/4 request
server 192.168.20.2:8080; # weight defaults to 1, The server handles 1/4 requests
}
server {
listen;
Location/{
proxy_pass http://myapp;
}
}
4. Virtual Host
Some websites have large traffic and need load balancing. However, not all sites are so good, some sites, due to the amount of traffic is too small, need to save costs, to deploy multiple sites on the same server.
For example, the www.aaa.com and www.bbb.com two Web sites deployed on the same server, two domain names resolved to the same IP address, but the user through two domain name can open two completely different sites, do not affect each other, like access to two servers, so called two virtual host.
Configuration:
server {
listen default_server;
server_name _;
return 444; # Filter other domain requests, return 444 status code
}
server {
listen;
server_name www.aaa.com; # www.aaa.com Domain
location/{
proxy_pass http://localhost:8080; # corresponding port number 8080
}
server {
Listen;
server_name www.bbb.com; # www.bbb.com Domain
location/{
proxy_pass http://localhost:8081; # corresponding port number 8081
}
}
In the server 8080 and 8081 opened an application, the client through a different domain name access, according to server_name can reverse proxy to the corresponding application server.
The principle of the virtual host is through the HTTP request header to match server_name to achieve, interested students can study the HTTP protocol.
In addition, the server_name configuration can also filter some of the domain names malicious to point to your host server.
5, FastCGI
Nginx itself does not support languages such as PHP, but it can be fastcgi to throw requests to certain language or framework processing (such as PHP, Python, Perl).
server {
listen;
Location ~ \.php$ {
include fastcgi_params;
Fastcgi_param script_filename/php file path $fastcgi_script_name; # PHP file path
fastcgi_pass 127.0.0.1:9000; # PHP-FPM address and port number
# Another way: Fastcgi_pass unix:/var/run/php5-fpm.sock;
}
}
In the configuration, the request for end of. PHP is passed through fashcgi to PHP-FPM processing, and PHP-FPM is a fastcgi manager in PHP. Other information about fashcgi can be consulted, which is not covered in this article.
What's the difference between Fastcgi_pass and Proxy_pass? Here is a picture that you can see:
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.