Basic concepts
Nginx the most common use is to provide reverse proxy service, then what reverse proxy? The agency believes that many compatriots in the mainland have been used in this magical land, the principle of the following figure:
The proxy server accepts the request as the intermediary of the client side, hides the real customer, and obtains resources from the server. If the proxy server outside the Great Wall can also help us to achieve the goal of climbing over the Great Wall. And the reverse proxy as the name implies is in turn proxy server as the intermediary of the server, hide the real services to provide the server, the principle is roughly the following figure:
This is certainly not to achieve over the Great Wall, but to achieve security and load balancing a series of functions. The so-called security refers to the client's request will not directly fall into the intranet server, but through the agent to do a layer of forwarding, in this layer can be implemented security filtering, flow control, anti-DDOS, such as a series of strategies. Load balancing refers to the number of servers that we can extend the backend to actually provide the service, and the agent forwards requests to each server in a regular order, which makes the load of each server close to equilibrium.
And Nginx is the current popular such a reverse proxy service.
Under Ubuntu, you can set up the process of compiling the installation directly Apt-get
Copy Code code as follows:
sudo apt-get install Nginx
After installation, you can go directly through:
Copy Code code as follows:
To start the Nginx service, nginx the default set of 80-port forwarding, we can browser access to Http://locallhost to check.
Initial configuration
The default profile for Nginx is located in the
Copy Code code as follows:
The best way to learn to configure is to start with the example, we will not look at the other configuration, directly look at and nginx the default page-related configuration. There is one row in the configuration file:
Copy Code code as follows:
include/etc/nginx/sites-enabled/*;
This line loads an external configuration file, the Sites-enabled folder has only one default file, and this external configuration file is the default agent for our nginx. When you shrink the configuration, you get the following lines:
Copy Code code as follows:
server {
server_name localhost;
Listen default_server;
Listen [::]:80 default_server Ipv6only=on;
root/usr/share/nginx/html;
Index index.html index.htm;
Location/{
Try_files $uri $uri/= 404;
}
}
A large web site usually has a lot of subordinate sites, have their own servers to provide the appropriate services, in the nginx we can use a concept called virtual host to separate these different service configurations, which is the meaning of the server in the above configuration. For example, Google has two products for translation and academic, so we can configure two server,servername in the Nginx configuration file, respectively, translate.google.com and Scholar.google.com, so that different URL requests will correspond to the nginx corresponding settings, forwarded to different back-end servers. The servername here is matched to the host line in the client HTTP request.
In this case server_name is localhost, which is why we can access the configuration of the page through localhost in the browser. The following two listen correspond to the listening port under IPv4 and IPv6 respectively, if set to 8080, then we can only visit the default page through localhost:8080.
Default_server means that if there are other HTTP requests that host does not exist in the Nginx, then use this server's configuration to process. For example, we go to visit 127.0.0.1 so it will fall into this server to deal with.
Each URL request corresponds to a service that is nginx for processing forwarding or a local file path, or a service path for other servers. And the matching of this path is done through location. We can use the server as a configuration for a domain name, and the location is to configure the finer paths under a domain name.
Here location match/Start all requests, that is, localhost under the/xxx or/yyy have to go to the following configuration, in addition to this simple rough match, Nginx also supports regular and full equality and other fine matching methods. and tryfiles means that Nginx will access the file in the following order, returning the first match. For example, you go to request localhost/test, he will go to find/test files, find/test/and then find the return of a 404. In addition, we can implement reverse proxy and load balancing with Proxypass in location configuration, but this simplest configuration does not involve
Where Root refers to a local folder as the root path for all URL requests. For example, a user requests a localhost/test, then Nginx will go to find the/usr/share/nginx/html folder under the test file return.
And index is the default access page, and when we visit localhost, he automatically finds the root file path index.html and index.htm to return the first result.
Location Advanced Configuration
the above configuration is to map the user's URL to the local file, and did not realize the legendary reverse proxy and load balancing (of course nginx do static file distribution is also thought of the bad), we will further configure the location to see how to achieve.
It's simple to configure, for example, I'm going to transfer all the requests to port 8080 of a machine that really provides the service, as long as:
Copy Code code as follows:
Location/{
Proxy_pass 123.34.56.67:8080;
}
So all the requests were reversed to 123.34.56.67. So our reverse proxy function is implemented, but can be agents to a server where there is what load balance it? This is going to use the Nginx upstream module.
Copy Code code as follows:
Upstream Backend {
Ip_hash;
Server backend1.example.com;
Server backend2.example.com;
Server backend3.example.com;
Server backend4.example.com;
}
Location/{
Proxy_pass Http://backend;
}
We specified a set of machines in upstream and named the group backend, so that in proxypass we realized the reverse proxy load balancing of four machines as long as the request was transferred to backend upstream. The iphash indicates that our balanced approach is to distribute according to the user's IP address.
For the configuration to take effect, we do not have to reboot Nginx only need reload configuration.
Copy Code code as follows:
sudo service Nginx Reload
Summarize
The above is the simplest through nginx to achieve static file forwarding, reverse proxy and load balancing configuration. All of the features in Nginx are implemented through modules, such as when we configure upstream for upstream modules, while server and location are in the HTTP core module, and the other has a stream-controlled LIMT module, mail module , HTTPS's SSL module. Their configuration is similar to the Nginx module documentation found in the detailed configuration instructions.