Home >Backend Development >PHP Tutorial >Several ways to write Nginx dynamic DNS reverse proxy

Several ways to write Nginx dynamic DNS reverse proxy

不言
不言Original
2018-05-09 10:37:042178browse

This article mainly introduces several ways to write Nginx dynamic DNS reverse proxy in detail, and introduces in detail the four methods of Nginx dynamic DNS reverse proxy. It is of great practical value. Friends who need it can refer to it

The update of this article has been delayed for half a month, and it really moved China.

Nginx will cache DNS by default. As we all know, if you do a reverse proxy, it is actually the target IP of the access, so once the target IP is cached, it will be very troublesome.

The four ways of writing the word fennel are famous far and wide. I never expected that Nginx dynamic DNS reverse proxy also has N ways of playing. It mainly depends on the Nginx version you use. Of course, the operation and maintenance told me that it is best to Don't use dynamic DNS, but based on business needs, we have no choice.

The first way to write the word fennel: Nginx

In Nginx, you can save the country by setting variables. Of course, there are disadvantages. There is no way to achieve load balancing through variables. , very troublesome. Of course, if you only have a single machine (almost non-existent in actual business scenarios), you can still give it a try:

server {
  ...
  resolver 127.0.0.1;
  set $backend "http://dynamic.example.com:80";
  proxy_pass $backend;
  ...
}

##Second Another writing method: Nginx Plus

Of course, Nginx is not very easy to use. The enhanced version of Plus provides a better writing method, which supports writing in the server:

server {
  ...
  resolver 127.0.0.1 valid=30s;
  proxy_pass http://upstream-sites;
  ...
}

The third way of writing: Tengine

Tengine is Taobao’s modified Nginx. In fact, I was quite annoyed because at first I don't know that we are using Tengine, so the configuration is useless, but Tengine supports dynamic domain name resolution very well, and the official documentation is also very clear.

upstream backend {
  dynamic_resolve fallback=stale fail_timeout=30s;
  server a.com;
  server b.com;
}
server {
  ...
  location / {
    proxy_pass http://backend;
  }
}

Others

Of course, you can also use Lua scripts to implement it, or other people’s modules, but consider I was afraid of causing trouble, so I decided not to mess around with it, so I didn't adopt this plan.

The above is the entire content of this article. For more related content, please pay attention to the PHP Chinese website.

Related recommendations:

Nginx reverse proxy websocket configuration example

The above is the detailed content of Several ways to write Nginx dynamic DNS reverse proxy. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn