Table of Contents
**
Home Backend Development PHP Tutorial Create nginx reverse proxy with Docker

Create nginx reverse proxy with Docker

May 28, 2017 am 09:32 AM
reverse proxy

A reverse proxy server is a server that is usually located in front of the web server and can provide attachment functions that the web server itself does not have.

For example, a reverse proxy can provide SSL termination, load balancing, request routing, caching, compression, and even A/B testing.

When using docker containers to run web services, running a reverse proxy can simplify deployment.

**

Why use reverse proxy for docker?

**
Docker containers are assigned random IPs and ports, which makes locating these containers difficult from a client perspective. By default, these IPs and ports are private and cannot be accessed from the outside unless they are bound to the host.

Binding the container to the host will prevent containers running on the same port. For example, only one docker can be bound to port 80 at a time. Additionally, this complicates the deployment of new versions of containers. Because the new version can only start the service after the old version stops the service.

Reverse proxy can solve the above problems while improving reliability by providing 0 downtime.

**

Generate reverse proxy configuration

**

Set the reverse proxy when the container starts and stops Configuration is a responsible matter. Typically configurations require manual update, which takes a lot of time and is error-prone.

Fortunately, docker provides a remote call API that can easily observe and access the container's IP and port, already configured metadata. In addition, docker also provides a real-time eventAPI, which can be used to send notifications when the container starts and stops. These APIs can be used to automatically generate reverse proxy configurations.

docker-gen is a small application. It uses docker's API to import the container's metadata into the template. After the template is generated, you can use it to restart the service.

Using docker-gen, you can automatically generate nginx configuration and reload nginx when the configuration changes. The same method can be used for docker log management.

**

nginx reverse proxy for docker

**
The following nginx template example can be used to generate docker containers Reverse proxy configuration. This template uses golang. The groupby template function is used to group the running container . The grouping is based on the VIRTUAL_HOST environment variable . This method simplifies traversing containers to generate a load-balanced backend, and also supports zero-downtime deployment.

{{ range

#containers := groupBy

“Env.VIRTUAL_HOST” }} upstream {{

##host }} {

{{ range

value :=

containers }} {{ with

address := index

value.Addresses 0 }} server {{

address.IP }}:{{ $address.Port }};
{{ end }}
{{ end }}

}

server {
#ssl_certificate /etc/nginx/certs/demo.pem;
#ssl_certificate_key /etc/nginx/certs/demo.key;

gzip_types text/plain text/css application/json application/x-javascript
           text/xml application/xml application/xml+rss text/javascript;

server_name {{ $host }};

location / {
    proxy_pass http://{{ $host }};
    include /etc/nginx/proxy_params;
}
Copy after login
Copy after login

}
{{ end }}

This template is run using the following command:
docker-gen -only-exposed -watch -notify “/etc/init.d/nginx reload” templates/nginx.tmpl /etc/nginx/sites-enabled/default

-only-exposed - 仅使用暴露出端口的容器.
-watch - 运行后,观察容器的事件,并重新生成模板.
-notify "/etc/init.d/nginx reload" - 重新加载nginx.
templates/nginx.tmpl - nginx模板.
/etc/nginx/sites-enabled/default - 目标文件.
Copy after login
Copy after login

The following is the configuration Created templates for two containers demo1 and demo2

upstream demo1.localhost {
server 172.17.0.4:5000;
server 172.17.0.3:5000;
}

server {
#ssl_certificate /etc/nginx/certs/demo.pem;
#ssl_certificate_key /etc/nginx/certs/demo.key;

gzip_types text/plain text/css application/json application/x-javascript
           text/xml application/xml application/xml+rss text/javascript;

server_name demo1.localhost;

location / {
    proxy_pass http://demo.localhost;
    include /etc/nginx/proxy_params;
}
Copy after login
Copy after login

}

upstream demo2.localhost {
server 172.17.0.5:5000;
}

server {
#ssl_certificate /etc/nginx/certs/demo.pem;
#ssl_certificate_key /etc/nginx/certs/demo.key;

gzip_types text/plain text/css application/json application/x-javascript
           text/xml application/xml application/xml+rss text/javascript;

server_name demo2.localhost;

location / {
    proxy_pass http://demo2.localhost;
    include /etc/nginx/proxy_params;
}
Copy after login
Copy after login

}

**

Try it

* *

You can try the build I made. https://index.docker.io/u/jwilder/nginx-proxy/
Run the nginx proxy container:

docker run -e VIRTUAL_HOST=foo.bar.com -t …

If you use HTTPS and want to run other containers, you can take a look atgit project on the hub for more information.

**

Conclusion

**
Generating nginx reverse proxy configuration for docker containers can be done automatically by using the docker API . This approach simplifies deployment and improves availability.

This solution is very convenient for containers running on a single host. Provides configuration dependency service discovery for distributed hosts. You can take a look at what the docker service discovered and find a solution.

translate. See the original text: http://jasonwilder.com/blog/2014/03/25/automated-nginx-reverse-proxy-for-docker/

A reverse proxy server is a type of server that is usually located in front of the web server. Server, which can provide attachment functions that the web server itself does not have.

For example, a reverse proxy can provide SSL termination, load balancing, request routing, caching, compression, and even A/B testing.

When using docker containers to run web services, running a reverse proxy can simplify deployment.

**

Why use reverse proxy for docker?

**
Docker containers are assigned random IPs and ports, which makes locating these containers difficult from a client perspective. By default, these IPs and ports are private and cannot be accessed from the outside unless they are bound to the host.

Binding the container to the host will prevent containers running on the same port. For example, only one docker can be bound to port 80 at a time. Additionally, this complicates the deployment of new versions of containers. Because the new version can only start the service after the old version stops the service.

Reverse proxy can solve the above problems while improving reliability by providing 0 downtime.

**

Generate reverse proxy configuration

**

Set the reverse proxy when the container starts and stops Configuration is a responsible matter. Often configurations need to be updated manually, which takes a lot of time and is error-prone.

Fortunately, docker provides a remote call API that can easily observe and access the container's IP and port, with metadata already configured. In addition, docker also provides a real-time event API that can be used to send notifications when the container starts and stops. These APIs can be used to automatically generate reverse proxy configurations.

docker-gen is a small application. It uses docker's API to import the container's metadata into the template. After the template is generated, you can use it to restart the service.

Using docker-gen, you can automatically generate nginx configuration and reload nginx when the configuration changes. The same method can be used for docker log management.

**

nginx reverse proxy for docker

**
The following nginx template example can be used to generate docker containers Reverse proxy configuration. This template uses golang. The groupby template function is used to group running containers. The grouping is based on the VIRTUAL_HOST environment variable. This method simplifies traversing containers to generate a load-balanced backend, and also supports zero-downtime deployment.

{{ range

containers := groupBy “Env.VIRTUAL_HOST” }}
upstream {{

host }} {

{{ range

##value :=

containers }} {{ with

##address := index

value.Addresses 0 }} server {{

##address.IP }}:{{ $address .Port }};
{{ end }}
{{ end }}


}

server {

#ssl_certificate /etc/nginx/certs/demo.pem;

#ssl_certificate_key /etc/nginx/certs/demo.key;

gzip_types text/plain text/css application/json application/x-javascript
           text/xml application/xml application/xml+rss text/javascript;

server_name {{ $host }};

location / {
    proxy_pass http://{{ $host }};
    include /etc/nginx/proxy_params;
}
Copy after login
Copy after login

}
{{ end }}

This template is run using the following command:
docker-gen - only-exposed -watch -notify “/etc/init.d/nginx reload” templates/nginx.tmpl /etc/nginx/sites-enabled/default

-only-exposed - 仅使用暴露出端口的容器.
-watch - 运行后,观察容器的事件,并重新生成模板.
-notify "/etc/init.d/nginx reload" - 重新加载nginx.
templates/nginx.tmpl - nginx模板.
/etc/nginx/sites-enabled/default - 目标文件.
Copy after login
Copy after login

The following are configured two containers demo1 and demo2 Template

upstream demo1.localhost {

server 172.17.0.4:5000;

server 172.17.0.3:5000;

}

server {
#ssl_certificate /etc/nginx/certs/demo.pem;

#ssl_certificate_key /etc/nginx/certs/demo.key;

gzip_types text/plain text/css application/json application/x-javascript
           text/xml application/xml application/xml+rss text/javascript;

server_name demo1.localhost;

location / {
    proxy_pass http://demo.localhost;
    include /etc/nginx/proxy_params;
}
Copy after login
Copy after login

}
upstream demo2.localhost {

server 172.17.0.5:5000;

}


server {
#ssl_certificate /etc/nginx/certs/demo.pem;

#ssl_certificate_key /etc/nginx/certs/demo.key;

gzip_types text/plain text/css application/json application/x-javascript
           text/xml application/xml application/xml+rss text/javascript;

server_name demo2.localhost;

location / {
    proxy_pass http://demo2.localhost;
    include /etc/nginx/proxy_params;
}
Copy after login
Copy after login

}
**

Try it

* *

You can try the build I made. https://index.docker.io/u/jwilder/nginx-proxy/

Run the nginx proxy container:



##docker run -e VIRTUAL_HOST=foo.bar.com -t …

If you use HTTPS and want to run other containers, you can check out the project on github for more information.

**

Conclusion

** Generating nginx reverse proxy configuration for docker containers can be done automatically by using the docker API . This approach simplifies deployment and improves availability.

This solution is very convenient for containers running on a single host. Provides configuration dependency service discovery for distributed hosts. You can take a look at what the docker service discovered and find a solution.

The above is the detailed content of Create nginx reverse proxy with Docker. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use Nginx with FastAPI for reverse proxy and load balancing How to use Nginx with FastAPI for reverse proxy and load balancing Aug 01, 2023 am 09:44 AM

How to use Nginx with FastAPI for reverse proxy and load balancing Introduction: FastAPI and Nginx are two very popular web development tools. FastAPI is a high-performance Python framework, and Nginx is a powerful reverse proxy server. Using these two tools together can improve the performance and reliability of your web applications. In this article, we will learn how to use Nginx with FastAPI for reverse proxy and load balancing. What is reverse generation

Application of access control and flow control in Nginx reverse proxy Application of access control and flow control in Nginx reverse proxy Jun 10, 2023 pm 06:58 PM

Nginx is a high-performance, open source, and versatile web server that is also widely used as a reverse proxy server. Reverse proxy servers can be used to provide features such as load balancing, high availability, access control, and traffic control. This article will introduce the application of access control and flow control in Nginx reverse proxy. 1. Access control IP address blacklist/whitelist Nginx can implement access control on requests by configuring IP address blacklist or whitelist. IP addresses in the blacklist will be denied access, while IP addresses in the whitelist

Detailed explanation of reverse proxy and request forwarding in Gin framework Detailed explanation of reverse proxy and request forwarding in Gin framework Jun 23, 2023 am 11:43 AM

With the rapid development of web applications, more and more enterprises tend to use Golang language for development. In Golang development, using the Gin framework is a very popular choice. The Gin framework is a high-performance web framework that uses fasthttp as the HTTP engine and has a lightweight and elegant API design. In this article, we will delve into the application of reverse proxy and request forwarding in the Gin framework. The concept of reverse proxy The concept of reverse proxy is to use the proxy server to make the client

Nginx reverse proxy cache configuration to accelerate static web page access Nginx reverse proxy cache configuration to accelerate static web page access Jul 04, 2023 pm 06:09 PM

Nginx reverse proxy cache configuration to achieve static web page access acceleration Introduction: With the rapid development of the Internet, access speed has become a very important factor in website operations. In order to improve the access speed of web pages, we can use Nginx reverse proxy caching technology to accelerate web pages. This article will introduce how to use Nginx to configure reverse proxy cache to accelerate static web pages. Nginx reverse proxy cache configuration: Install Nginx: First you need to install the Nginx server, which can be done through apt-ge

How to use Nginx Proxy Manager to implement reverse proxy under HTTPS protocol How to use Nginx Proxy Manager to implement reverse proxy under HTTPS protocol Sep 26, 2023 am 08:40 AM

How to use NginxProxyManager to implement reverse proxy under HTTPS protocol. In recent years, with the popularity of the Internet and the diversification of application scenarios, the access methods of websites and applications have become more and more complex. In order to improve website access efficiency and security, many websites have begun to use reverse proxies to handle user requests. The reverse proxy for the HTTPS protocol plays an important role in protecting user privacy and ensuring communication security. This article will introduce how to use NginxProxy

Using Nginx Proxy Manager to implement reverse proxy load balancing strategy Using Nginx Proxy Manager to implement reverse proxy load balancing strategy Sep 26, 2023 pm 12:05 PM

Use NginxProxyManager to implement reverse proxy load balancing strategy NginxProxyManager is an Nginx-based proxy management tool that can help us easily implement reverse proxy and load balancing. By configuring NginxProxyManager, we can distribute requests to multiple backend servers to achieve load balancing and improve system availability and performance. 1. Install and configure NginxProxyManager

[Summary] Common reasons and solutions for PHP reverse proxy inaccessibility [Summary] Common reasons and solutions for PHP reverse proxy inaccessibility Mar 21, 2023 pm 07:10 PM

When using a reverse proxy, you may encounter inaccessibility issues. Especially when using PHP as a reverse proxy, this problem seems to be more prominent. This article explains common causes and solutions to this problem.

Nginx reverse proxy cache configuration to improve website access speed Nginx reverse proxy cache configuration to improve website access speed Jul 04, 2023 pm 10:01 PM

Nginx reverse proxy cache configuration to improve website access speed Introduction: In the Internet era, website access speed is crucial. A website that loads slowly makes users impatient and can lead to user churn. In order to improve the access speed of the website, a common way is to reduce the load on the server and speed up the loading of the page by using reverse proxy cache. This article will introduce how to use Nginx to configure reverse proxy cache to improve website access speed. 1. What is Nginx reverse proxy cache? Ngin

See all articles