Linux----What nginx can do __linux

Source: Internet
Author: User

What Nginx can do?

1. Reverse proxy

2. Load Balancing

3.HTTP server (including static and dynamic separation)

4. Forward Agent Reverse Proxy

Reverse proxy should be nginx do the most thing, what is the reverse proxy, the following is the saying of Baidu Encyclopedia: Reverse Proxy (Reverse proxy) means to accept the connection request on the Internet by proxy server, then forward the request to the server on the internal network, and returns the results from the server to the client on the Internet requesting a connection, at which point the proxy server behaves as a reverse proxy server. In short, the real server can not be directly access to the external network, so need a proxy server, and the proxy server can be access to the external network and the real server in the same network environment, of course, may also be the same server, the port is different just. Here's a simple code that implements the reverse proxy

server {
Listen 80;
server_name localhost;
Client_max_body_size 1024M;

    Location/{
        Proxy_pass http://localhost:8080;
        Proxy_set_header Host $host: $server _port
    }
}

After saving the configuration file, start Nginx, so that when we visit localhost, it is equivalent to access localhost:8080 load Balancing

Load balancing is also a common function of nginx, load balancing means to allocate to multiple operational units for execution, such as Web servers, FTP servers, enterprise-critical application servers, and other mission-critical servers to work together to accomplish tasks. In simple terms, when there are 2 or more servers, according to the rules of random distribution of requests to the designated server processing, load balancing configuration generally need to configure the reverse proxy, through the reverse proxy jump to load balance. Nginx currently supports 3 load balancing strategies and 2 common third-party strategies.

1, RR (default)

Each request is assigned to a different back-end server in chronological order, and can be automatically removed if the backend server is down.

Simple configuration

Upstream test {
    server localhost:8080;
    Server localhost:8081;
}
server {
    listen       Bayi;                                                        
    server_name  localhost;                                              
    Client_max_body_size 1024M;

    Location/{
        Proxy_pass http://test;
        Proxy_set_header Host $host: $server _port
    }
}

The core code for load Balancing is

Upstream test {
    server localhost:8080;
    Server localhost:8081;
}

Here I have configured 2 servers, of course, is actually a, but the port is not the same, and 8081 of the server is not there, that is, access is not, but we visit the http://localhost, there will be no problems, will default jump to http://localhost : 8080 specifically because Nginx will automatically determine the state of the server, if the server is not reachable (the server is dead), it will not jump to this server, so it also avoids a server hanging the impact of use, because the Nginx default is the RR policy, so we do not need more settings.

2. Weight

Specifies the polling probability, proportional to the weight and the access ratio, for the performance of the backend server. For example

Upstream test {
    server localhost:8080 weight=9;
    Server localhost:8081 weight=1;
}

Then 10 times typically only 1 visits to 8081, and 9 visits to 8080

3, Ip_hash

There's a problem with all 2 of these ways, that's when the next request comes. Request may be distributed to another server, when our program is not stateless (using the session to save the data), this time there is a very big problem, such as the login information saved to the session, Then to jump to another server when you need to log in, so many times we need a client to access only one server, then need to use Iphash, iphash each request by the access to the hash result of IP allocation, so that each visitor fixed access to a back-end server, can solve the session problem.

Upstream test {
    ip_hash;
    Server localhost:8080;
    Server localhost:8081;
}

4, Fair (third party)

The response time of the backend server is allocated to the request, and the response time is short for priority assignment.

Upstream backend {
    fair;
    Server localhost:8080;
    Server localhost:8081;
}

5, Url_hash (third party)

The request is allocated by the hash result of the access URL, which directs each URL to the same back-end server, which is more efficient when cached. Add hash statement in upstream, server statement can not write weight and other parameters, Hash_method is the hash algorithm used

Upstream backend {
    hash $request _uri;
    Hash_method CRC32;
    Server localhost:8080;
    Server localhost:8081;
}

The above 5 kinds of load balance are applicable under different circumstances, so you can choose which policy mode to use according to the actual situation, but fair and url_hash need to install a third-party module to use, because this article mainly introduces Nginx can do things, so Nginx install the third party module will not be introduced in this article HTTP Server

Nginx itself is also a static resources of the server, when only static resources, you can use Nginx to do the server, and now also very popular static and dynamic separation, can be achieved through nginx, first look at the Nginx do static resource server

server {
    listen       ;                                                        
    server_name  localhost;                                              
    Client_max_body_size 1024M;


    Location/{
           root   e:wwwroot;
           Index  index.html;
       }
}

This way, if the access http://localhost will default to access to the index.html below the e-disk wwwroot directory, if a site is just a static page, then the deployment can be implemented in this manner. static and dynamic separation

Static separation is to allow Dynamic Web site Dynamic Web pages according to a certain rule of the constant resources and constantly changing resources to distinguish, static and dynamic resources do a split, we can according to the characteristics of static resources to do caching operations, this is the core of static web site management ideas

Upstream test{
Server localhost:8080;
Server localhost:8081;
}

server {  
    listen       ;  
    server_name  localhost;  

    Location/{  
        root   e:wwwroot;  
        Index  index.html;  
    }  

    # All static requests are processed by Nginx, and the directory is HTML  
    location ~. GIF|JPG|JPEG|PNG|BMP|SWF|CSS|JS) $ {  
        root    e:wwwroot;  
    }  

    # All dynamic requests are forwarded to Tomcat processing  
    location ~. ( Jsp|do) $ {  
        proxy_pass  http://test;  
    }  

    Error_page   502 503 504  /50x.html;  
    Location =/50x.html {  
        root   e:wwwroot;  
    }  
}  

So we can put HTML and pictures and CSS and JS into the Wwwroot directory, and Tomcat is only responsible for processing JSP and request, for example, when we suffix to gif, nginx default will be obtained from wwwroot to the current request of the dynamic map file return, Of course, the static file here is the same server as Nginx, we can also in another server, and then through the reverse proxy and load-balanced configuration of the past is good, as long as the most basic flow of the process, a lot of configuration is very simple, in addition localtion behind is actually a regular expression, So very flexible forward agent

Forward proxy, meaning a server between the client and the original server (Origin server), in order to obtain content from the original server, the client sends a request to the agent and specifies the target (the original server), and the agent then forwards the request to the original server and returns the obtained content to the client. The client can use a forward proxy. When you need to use your server as a proxy server, can use Nginx to implement forward agent, but at present Nginx have a problem, then is not support HTTPS, although I Baidu to configure HTTPS forward agent, but to the last found or agent, of course, may be my configuration of the wrong , so I would like to have a message from the comrades who know the right way.

Resolver 114.114.114.114 8.8.8.8;
server {

    resolver_timeout 5s;

    Listen Bayi;

    Access_log  E:wwwrootproxy.access.log;
    Error_log   E:wwwrootproxy.error.log;

    Location/{
        proxy_pass http://$host $request_uri;
    }
}

Resolver is a DNS server that configures forward proxies, listen is a forward proxy port, and can be configured to use server ip+ port number for proxy on IE above or other proxy plug-ins.

Finally, two words.

Nginx is to support the hot boot, that is, when we modify the configuration file, do not have to close the Nginx, you can implement the configuration to take effect, of course, I do not know how many people know this, anyway, I do not know the beginning, cause often killed the nginx thread again to start ... Nginx the command for the new read configuration is

Nginx-s Reload  

Under Windows is

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.