Nginx four action

In the case of paper only for Nginx does not load third-party modules which can handle things, because the third-party modules also introduced so much endless.

What do Nginx

- Reverse Proxy

- Load Balancing

--HTTP server (static and dynamic separation)

- Forward Proxy

That is all I have learned in Nginx does not rely on third-party modules can handle things, the following detailed description of each function how to do it.

Reverse Proxy

Nginx reverse proxy should be made up of one thing, what is it the reverse proxy, the following statement is Baidu Encyclopedia: Reverse Proxy (Reverse Proxy) mode refers to the proxy server to accept connection requests on the internet, then forwards the request to the server on the internal network, and the results obtained from the server back to the client requesting the connection to the internet, in which case the external proxy server performance of a reverse proxy server. It simply is not true server directly access the external network, so you need a proxy server, and proxy server can simultaneously access the external network of any connection with the same real server in a network environment, of course, it may be the same server, port different.

The following paste a simple piece of code to achieve reverse proxy

 

After saving the configuration file to start Nginx, so that when we visit the localhost, equivalent to access localhost: 8080 a

Load Balancing

Nginx load balancing is a common feature, which means that load balancing across multiple operating units allocated to execution, such as Web servers, FTP servers, business-critical application servers and other mission-critical servers, so as to work together to complete the task. In simple terms it is that when there are two or more sets of servers, distributed according to a random rule request to the specified server process, load balancing configuration generally need to configure the reverse proxy, to jump through the reverse proxy load balancer. The Nginx currently supports three kinds of native load balancing strategy, there are two kinds of popular third-party policy.

1, RR (default)

Each request individually assigned to a different time order back-end server, if the back-end server is down, can be automatically removed.

Simple Configuration

 

Load balancing is the core code

Here I configured the two servers, of course, it is in fact one, but just not the same port, and 8081 server does not exist, that is not to visit, but we visit http: // localhost time, nor there will be problems, will default to jump to http: // localhost: 8080 in particular due to automatically determine the state of the server Nginx will, if the server is not accessible (server hung up), it will not jump to this server, so it to avoid a hung server case affect the use, since Nginx default RR policy, we do not need so many more settings.

2, weight

Polling a probability proportional to weight ratio and access, for the case where unevenness backend server performance.

E.g

So there is usually only 10 times 1 will have access to 8081, while nine will have access to 8080

3、ip_hash

The above two kinds of methods have a problem that when the next request to the request may be distributed to another server, when our program is not stateless (using the session save data), this time there is a great the very problem, such as to save the login information to the session, then jump to another server when you need to log in again, so many times we need a client access only one server, then you need to use iphash, and iphash each request is assigned according ip hash result of the visit, so that each visitor to access a fixed back-end server, can solve the problem of session.

 

4, fair (third party)

By the response time of the allocation request to the backend server, a short response time priority allocation.

5, url_hash (third party)

Press access url hash result to the allocation request, each url directed to the same back-end server, the back end server is effective when the cache. Hash join statement in the upstream, server statement can not be written other parameters such as weight, hash_method hash algorithm is used

5 or more load-balancing each applicable use in different situations, so you can choose which policy to use mode according to the actual situation, but fair and url_hash need to install third-party modules to use, because the paper describes Nginx can do, so Nginx Installation This article describes the module will not be tripartite

HTTP server

Nginx本身也是一个静态资源的服务器,当只有静态资源的时候,就可以使用Nginx来做服务器,同时现在也很流行动静分离,就可以通过Nginx来实现,首先看看Nginx做静态资源服务器

这样如果访问http://localhost 就会默认访问到E盘wwwroot目录下面的index.html,如果一个网站只是静态页面的话,那么就可以通过这种方式来实现部署。

动静分离

动静分离是让动态网站里的动态网页根据一定规则把不变的资源和经常变的资源区分开来,动静资源做好了拆分以后,我们就可以根据静态资源的特点将其做缓存操作,这就是网站静态化处理的核心思路

 

这样我们就可以吧HTML以及图片和css以及js放到wwwroot目录下,而tomcat只负责处理jsp和请求,例如当我们后缀为gif的时候,Nginx默认会从wwwroot获取到当前请求的动态图文件返回,当然这里的静态文件跟Nginx是同一台服务器,我们也可以在另外一台服务器,然后通过反向代理和负载均衡配置过去就好了,只要搞清楚了最基本的流程,很多配置就很简单了,另外localtion后面其实是一个正则表达式,所以非常灵活

正向代理

正向代理,意思是一个位于客户端和原始服务器(origin server)之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并指定目标(原始服务器),然后代理向原始服务器转交请求并将获得的内容返回给客户端。客户端才能使用正向代理。当你需要把你的服务器作为代理服务器的时候,可以用Nginx来实现正向代理,但是目前Nginx有一个问题,那么就是不支持HTTPS,虽然我百度到过配置HTTPS的正向代理,但是到最后发现还是代理不了,当然可能是我配置的不对,所以也希望有知道正确方法的同志们留言说明一下。

resolver是配置正向代理的DNS服务器,listen 是正向代理的端口,配置好了就可以在ie上面或者其他代理插件上面使用服务器ip+端口号进行代理了。

Guess you like

Origin www.cnblogs.com/dongye95/p/11059024.html