@TOC

Let’s take a look at the main role nginx plays in the development process

  • Nginx addresses cross-domain
  • Nginx load balancing

Nginx solves cross-domain problems

If you want to understand what cross-domain is

Cross-domain in a broad sense is when a document or script in one domain tries to request a resource in another domain. That is, cross-domains caused by browser same-origin policy /SOP restrictions are included;

  • So what is the same origin policy?

The Same Origin Policy (SOP) is a convention introduced by Netscape into the browser in 1995. It is the core and most basic security function of the browser. Without the Same Origin policy, the browser is vulnerable to XSS and CSFR attacks. The same origin policy means that protocol, domain name, and port are the same

  • Common cross-domain scenarios are as follows:
URL that whether to allow communication http://www.baidu.com/hello.js http://www.baidu.com/world.js the same domain name, Different file or path to allow (no cross-domain) http://www.baidu.com/hello/world.js http://www.baidu.com:8000/hello.js http://www.baidu.com/hello.js The same domain name, different port Do not allow (a cross-domain) http://www.baidu.com/hello.js https://www.baidu.com/world.js the same domain name, Different protocols Do not allow (a cross-domain) http://www.baidu.com/hello.js http://192.168.22.128/world.js domain name and domain names corresponding IP does not allow (a cross-domain) http://baidu.com/hello.js Primary domain http://x.baidu.com/hello.js is the same, Different subdomains Do not allow (a cross-domain) different domain name http://www.baidu.com/hello.js http://www.baidu.com/hello.js http://www.bai.com/world.js Don't allow (a cross-domain)Copy the code
Common cross-domain solutions are as follows:
Document. Domain + iframe Resolve cross-domain 3. Nginx proxy resolve cross-domain 4Copy the code

The focus is on nginx proxy solutions across domains;

server { listen 80; server_name localhost; Location / {// location/proxy all requests. If location/API matches the request with/API prefix, proxy proxy_pass www.baidu.com; // Server domain name}}Copy the code
  1. The front-end server domain name is localhost
  2. Server The server domain name is www.baidu.com

If you make a request to www.baidu.com according to the browser’s SOP/ same-origin policy, a cross-domain problem will definitely occur. However, as long as we start the nginx server and set server_name to the front-end domain name, the front-end request is equivalent to localhost to localhost, so there is no cross-domain. But the truth is that nginx proxies requests to localhost back to www.baidu.com


Here’s what agency is;

A proxy is a hypothetical layer of servers (intermediate servers) between the server and the client. The proxy will receive the client’s request and forward it to the server, and then forward the server’s response to the client.

Proxy is divided into forward proxy and reverse proxy: specific difference between their own Baidu...Copy the code

Nginx load balancing


Nginx implements load balancing by distributing requests to a list of servers; The concrete implementation is as follows:

Upstream {balance.com server 192.168.2.100:42000; Server 192.168.2.101:42000; Server 192.168.2.102:42000; } server { server_name fe.server.com; listen 80; location /api { proxy_pass http://balance.com; }}Copy the code

The above configuration only specifies the list of servers that nginx needs to forward, and does not specify an allocation policy.

Nginx load balancing policy

  1. Polling strategy

    The polling policy is the default policy that allocates each request to a different server one by one. If the server fails, the request is automatically removed

Upstream {balance.com server 192.168.2.100:42000; Server 192.168.2.101:42000; Server 192.168.2.102:42000; }Copy the code
  1. Minimum connections policy

The length of each queue is balanced by prioritizing requests to the server with the fewest connections.

	upstream balance.com{
			least_conn;
			server 192.168.2.100:42000;
			server 192.168.2.101:42000;
			server 192.168.2.102:42000;
		}
Copy the code
  1. Maximum response time policy

Priority is assigned to the server with the shortest response time.


  upstream balance.com {
	 fair;
	server 192.168.2.100:42000;
	server 192.168.2.101:42000;
	server 192.168.2.102:42000;
 }

Copy the code
  1. Weight strategy

Use weight to specify the server access ratio, which defaults to 1. The ratio of the access rate is equal to the weight ratio;

Upstream {server 192.168.2.100:42000 weight=1; Server 192.168.2.101:42000 weight = 2; Server 192.168.2.102:42000 weight = 3; }Copy the code
  1. The client IP address is bound to ip_hash

Each request is allocated according to the hash value of the access IP address. In this way, consecutive Web requests from the same client are distributed to the same server for processing, which can solve the session problem. If the server fails, it is automatically deleted.

upstream balance.com{ ip_hash; Server 192.168.2.100:42000 weight = 1; Server 192.168.2.101:42000 weight = 2; Server 192.168.2.102:42000 weight = 3; }Copy the code

Above is a blogger is to understand and use to some of the skills and knowledge, as to see about nginx deployment after 404 @ quiet Eno | refresh after deployment within the framework of the vue online at 404 solutions to problems

Respect the original article from @quiet Enonginx deployment/proxy/cross domain