Nginx Project Deployment

Nginx Project Deployment

Daniel

The company's project has just been launched, with a small number of users, low traffic, and low concurrency. A jar package can solve the problem. However, as the project continues to expand and the user base increases, one server may not be able to meet our needs (one person used one server at the beginning, but now multiple people use one server. After a long time, the server will be overheated). 

So, we will think about adding servers, so our multiple projects will be launched on multiple servers. If users need to access, we need to make a proxy server, and use the proxy request server to forward and request between the front and back ends, including cross-domain and other issues.

Well, we have to talk about nginx's reverse proxy here. Forward proxy actually means that, for example, when we request xxx through VPN, we use a proxy server from somewhere else. This is a process from client to server. However, reverse proxy means that we have multiple servers, which are finally mapped to the proxy server. The client finally visits, for example, baidu.com, but in fact, there are multiple servers under it.

Code on screen

Since it has multiple servers, the performance and conditions of each server are different. Here we have to talk about another ability of nginx---load balancing, which can add different weights to different servers. The more powerful server can increase its load and reduce the load of other servers. This is what everyone often calls nginx: Nginx is a high-performance HTTP and reverse proxy server, which also supports IMAP/POP3/SMTP proxy servers.

Nginx's main configuration file (conf/nginx.conf) is organized as follows:

· Global block Global settings related to Nginx operation

· Events Settings related to network connection

· http proxy, cache, log, virtual host, etc.

· server Parameter settings for virtual hosts (an http block can contain multiple server blocks)

· location defines request routing and page processing methods

Cross-domain issues are often encountered in front-end development. Nginx can be easily solved by acting as a proxy. In fact, the principle is the same as cors. Set the request header.

server {
  location /api {
      proxy_pass http://backend_server;
      add_header Access-Control-Allow-Origin *;
      add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
      add_header Access-Control-Allow-Headers 'Origin, Content-Type, Accept';
  }
}

Caching issues:

proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off;

server {
  location / {
    proxy_cache my_cache;
    proxy_pass http://backend;
    add_header X-Cache-Status $upstream_cache_status;
  }
}

...

read more from Danel's blog

Report Page