Some basic features of Nginx

Source: Internet
Author: User

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 through 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 directly access a website Application server through the HTTP protocol, the webmaster can add an nginx in the middle, the client requests Nginx,nginx request the application server, and then return the result to the client, when 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 be accessed directly by HTTP, why add a reverse proxy in the middle, not superfluous? What is the role of the reverse proxy? Continue to look down, the following load balancer, virtual host, etc., are based on reverse proxy implementation, of course, the function of reverse proxy is not only these.

3. Load Balancing

When the site visits are very large, webmaster happy to make money at the same time, also spread the matter. Because the website is getting slower, a server is not enough. The same application is then deployed on multiple servers, assigning a large number of user requests to multiple machines for processing. At the same time, the benefit is that one of the servers in case of a hang, as long as there are other servers operating properly, will not affect user use.

Nginx can use reverse proxy to achieve load balancing.

Configuration:

  Upstream MyApp {    server 192.168.20.1:8080; # application server 1    server 192.168.20.2:8080; # application Server 2}server {    Listen 80;< C4/>location/{        proxy_pass Http://myapp;    }}

The above configuration allocates request polling to the application server, which is a multiple request from one client, which may be handled by multiple different servers. The request can be assigned to a fixed server for processing by Ip-hash, based on the hash value of the client IP address.

Configuration:

  Upstream MyApp {    ip_hash; # assigns a request to a fixed server to process the server 192.168.20.1:8080 based on the client IP address hash value    ;    Server 192.168.20.2:8080;} server {    listen;    Location/{        proxy_pass Http://myapp;    }}

In addition, the server hardware configuration may be bad, want to allocate most of the request to a good server, a 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; # The server processes 3/4 requests    server 192.168.20.2:8080; # weight defaults to 1, The server processes 1/4 requests}server {    listen;    Location/{        proxy_pass Http://myapp;    }}

4. Virtual Host

Some sites have a large number of visits and need load balancing. However, not all sites are so good, some sites, because the traffic is too small, need to save costs, to deploy multiple sites on the same server.

For example, the www.jim.com and www.tim.com two Web sites deployed on the same server, two domain names resolved to the same IP address, but the user through two domain names 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 name request, return 444 status code}server {    listen;    server_name www.jim.com; # www.jim.com Domain Location    /{        proxy_pass http://localhost:8080; # corresponding port number 8080    }}server {    Listen 80;< C17/>server_name www.tim.com; # www.tim.com Domain Location    /{        proxy_pass http://localhost:8081; # corresponding port number 8081    }}

In server 8080 and 8081 opened an application, the client through a different domain 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 in the host match server_name to achieve, interested students can study the HTTP protocol.

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

Some basic features of Nginx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.