Abstract: This article from the concept of Nginx, respectively from the concept of reverse proxy, advantages, configuration code three aspects introduced one of Nginx features reverse proxy.

Article source: Technical Sharing of CreditEase Technology Institute & CreditEase Payment and Settlement team Phase I – Senior technical Manager of CreditEase Payment and Settlement Eight Party Data Team zhou Heng

Sharer: Zhou Heng, senior technical Manager, BAFang Data Team, Creditease Payment and Settlement

The original article was published on the public account of payment and settlement Technology team: Wild Pointer

Nginx concept interpretation

The best way to understand something new is to start with the concept. This article, the first in a series of articles on Nginx, takes a look at the mysterious engine, starting with the name Nginx.

Nginx, short for Engine X, also pronounced ‘Engine X’, was developed in 2004 by Igor Sessoyev, a Russian god, to provide high-performance and easy-to-use HTTP reverse proxy functionality. TCP reverse proxy support was added later.

Nginx was originally created to solve the early C10K problem. What is C10K? C represents clients, and 10K represents 10000, that is, one server maintains 10,000 connections at the same time. It was a very difficult problem at the time.

A Google search for Nginx yields the following explanation:

Nginx is an asynchronous framework web server that can also be used as a reverse proxy, load balancer, and HTTP cache.

From this statement, we can get the following key points:

  • Asynchronous framework
  • The reverse proxy
  • Load balancing
  • HTTP cache

This special article will explain the power of Nginx from these key words. This article introduces the Nginx reverse proxy feature and its configuration implementation.

Reverse proxy

2.1 What is a Reverse proxy

Agent is very common in life, housing intermediary is agent, terminal retail is agent, elected representative is agent. These agents can help the demander reduce the complexity of many tasks, improve efficiency and experience.

What is the proxy service in the network, I think you are also very clear, here is a brief review: Suppose we want to read online video B stand in the company, and the specification of the company for reasons of safety and business efficiency, set up a network strategy, are not allowed to access video website, clever programmers can’t be defeated by these things, as long as buy a cloud services, set up the proxy service, your browser Settings on the agent, you can easily access to the video website. This is a common proxy.

So now the question is: “proxy” is well understood, why is the emphasis on reverse proxy? Is there a forward proxy? The answer is yes.

A forward proxy is a common proxy. The forward proxy takes the perspective of the client as the forward proxy, through which the user sends the request. It is the user who chooses to use the proxy.

Reverse proxy: Look at the picture before you explain.

The initiative has been reversed, instead of the client choosing the proxy, the proxy now chooses the server node. Such a proxy is called a “reverse proxy” because of the reversal of control.

2.2 Advantages of reverse proxy

1) Protect service security

  • Hide the IP address of the service node.
  • Place service nodes behind firewalls to avoid direct attacks on service node servers.

2) Service nodes focus more on business and improve performance

  • Because the reverse proxy exists, you can enable the reverse proxy server to implement non-service related functions such as HTTPS and GZIP compression.
  • Dynamic and static separation is provided to send static files to static servers or local file systems, preventing service nodes from processing these irrelevant requests.
  • A caching mechanism is provided to increase the cache of dynamic content that will not change in a short time on the reverse proxy server layer to reduce the request volume of the service server.
  • Since the control lies with the proxy service, requests can be dynamically allocated according to the performance of the service node to achieve the best performance of the service node.

It is Ngxin’s introduction of the reverse proxy feature, which allows requests and responses to pass through Nginx, that opens up a lot of possibilities for Nginx. Such as load balancing, HTTP caching, and so on.

3. Configure the reverse proxy

The configuration of reverse proxies in Nginx is fairly simple.

3.1 Configuring a Single-node Reverse proxy

# simple reverse-proxy
server { 
    listen       80;
    server_name  big.server.com;
    access_log   logs/big.server.access.log  main;

    # pass requests for dynamic content to rails/turbogears/zope, et alLocation / {proxy_pass http://127.0.0.1:8080; }}Copy the code

The rule defined here is to request port 80 of Nginx using the big.server.com domain name, which will proxy the request to 127.0.0.1:8080.

3.2 Configuring a Group of Reverse proxy Service Nodes.

1) Configure a group of reverse proxies and name them.

Upstream: Upstream big_server_com {server 192.168.0.1:8000; Server 192.168.0.1:8001; }Copy the code

Upstream: Downloading data from the server, uploading data to the server, and uploading data to the server.

Name this group of service nodes big_server_com, which contains two nodes: 192.168.0.1:8000 and 192.168.0.1:8001.

2) Configure rules to reverse proxy satisfied requests to this set of service nodes.

server { listen 80; server_name big.server.com; access_log logs/big.server.access.log main; location / { proxy_pass http://big_server_com; }}Copy the code

The rule defined here is that requests to port 80 of Nginx are made from the big.server.com domain name, and all requests for urls with a/suffix are forwarded to the previously defined service node group named big_server_com.

Four,

This article from the concept of Nginx, respectively from the concept of reverse proxy, advantages, configuration code three aspects introduced one of Nginx features reverse proxy. Stay tuned for future articles on Nginx’s three other features: load balancing, HTTP caching, and asynchronous frameworks.