Hướng dẫn cấu hình Reverse Proxy Nginx với Docker

phuongnga835

New member
### Hướng dẫn định cấu hình proxy nginx ngược với Docker

** Hashtags: **

* #Nginx
* #Reverse proxy
* #Docker
* #Kubernetes
* #Containerization

**Giới thiệu**

Proxy ngược là một máy chủ nằm trước một hoặc nhiều máy chủ khác và chuyển tiếp yêu cầu từ máy khách đến các máy chủ đó.Điều này có thể được sử dụng để cải thiện hiệu suất, bảo mật hoặc khả năng mở rộng.

Trong hướng dẫn này, chúng tôi sẽ chỉ cho bạn cách định cấu hình proxy ngược bằng nginx với Docker.Chúng tôi sẽ sử dụng một ví dụ đơn giản về một máy chủ web phục vụ các tệp tĩnh.

** Điều kiện tiên quyết **

Để làm theo hướng dẫn này, bạn sẽ cần những điều sau đây:

* Docker được cài đặt trên máy của bạn
* Một trình soạn thảo văn bản

** Bước 1: Tạo Dockerfile **

Bước đầu tiên là tạo một dockerfile sẽ xây dựng hình ảnh nginx.Dockerfile sẽ chứa các hướng dẫn sau:

1. Kéo hình ảnh Nginx chính thức từ Docker Hub.
2. Sao chép tệp cấu hình Nginx vào hình ảnh.
3. Tiếp xúc với cổng mà Nginx sẽ lắng nghe.
4. Xây dựng hình ảnh.

Đây là Dockerfile hoàn chỉnh:

`` `
Từ Nginx: Mới nhất

Sao chép nginx.conf /etc/nginx/nginx.conf

Phơi bày 80

Cmd ["nginx", "-g", "daemon off;"]
`` `

** Bước 2: Tạo tệp cấu hình **

Bước tiếp theo là tạo một tệp cấu hình cho Nginx.Tệp cấu hình sẽ cho nginx làm thế nào để các yêu cầu proxy đến máy chủ web.

Đây là tệp cấu hình hoàn chỉnh:

`` `
phụ trợ ngược dòng {
Web máy chủ: 8080;
}

máy chủ {
Nghe 80;
server_name localhost;

vị trí / {
Proxy_Pass http: // phụ trợ;
}
}
`` `

** Bước 3: Xây dựng hình ảnh **

Khi bạn đã tạo tệp DockerFile và cấu hình, bạn có thể xây dựng hình ảnh bằng cách chạy lệnh sau:

`` `
Docker Build -t Nginx -Proxy.
`` `

** Bước 4: Chạy hình ảnh **

Khi hình ảnh được xây dựng, bạn có thể chạy nó bằng cách chạy lệnh sau:

`` `
docker chạy -D -P 80:80 nginx -proxy
`` `

Điều này sẽ bắt đầu bộ chứa Nginx và cổng MAP 80 trên máy chủ đến cổng 80 trên container.

** Bước 5: Kiểm tra proxy **

Bây giờ proxy đang chạy, bạn có thể kiểm tra nó bằng cách mở trình duyệt và điều hướng đến URL sau:

`` `
http: // localhost
`` `

Bạn sẽ xem trang chỉ mục từ máy chủ web.

**Phần kết luận**

Trong hướng dẫn này, chúng tôi đã chỉ cho bạn cách định cấu hình proxy ngược bằng nginx với Docker.Chúng tôi đã sử dụng một ví dụ đơn giản về một máy chủ web phục vụ các tệp tĩnh, nhưng bạn có thể sử dụng cùng một kỹ thuật này để ủy quyền bất kỳ loại ứng dụng nào.

** Tài nguyên bổ sung **

* [Tài liệu Nginx] (https://nginx.org/en/docs/)
* [Tài liệu Docker] (https://docs.docker.com/)
* [Tài liệu Kubernetes] (https://kubernetes.io/docs/)
=======================================
### Instructions for configuring Reverse proxy nginx with docker

**Hashtags:**

* #Nginx
* #Reverse proxy
* #Docker
* #Kubernetes
* #Containerization

**Introduction**

A reverse proxy is a server that sits in front of one or more other servers and forwards requests from clients to those servers. This can be used to improve performance, security, or scalability.

In this tutorial, we will show you how to configure a reverse proxy using nginx with Docker. We will use a simple example of a web server that serves static files.

**Prerequisites**

To follow this tutorial, you will need the following:

* Docker installed on your machine
* A text editor

**Step 1: Create a Dockerfile**

The first step is to create a Dockerfile that will build the nginx image. The Dockerfile will contain the following instructions:

1. Pull the official nginx image from Docker Hub.
2. Copy the nginx configuration file into the image.
3. Expose the port that nginx will listen on.
4. Build the image.

Here is the complete Dockerfile:

```
FROM nginx:latest

COPY nginx.conf /etc/nginx/nginx.conf

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]
```

**Step 2: Create a configuration file**

The next step is to create a configuration file for nginx. The configuration file will tell nginx how to proxy requests to the web server.

Here is the complete configuration file:

```
upstream backend {
server web:8080;
}

server {
listen 80;
server_name localhost;

location / {
proxy_pass http://backend;
}
}
```

**Step 3: Build the image**

Once you have created the Dockerfile and configuration file, you can build the image by running the following command:

```
docker build -t nginx-proxy .
```

**Step 4: Run the image**

Once the image is built, you can run it by running the following command:

```
docker run -d -p 80:80 nginx-proxy
```

This will start the nginx container and map port 80 on the host machine to port 80 on the container.

**Step 5: Test the proxy**

Now that the proxy is running, you can test it by opening a browser and navigating to the following URL:

```
```

You should see the index page from the web server.

**Conclusion**

In this tutorial, we showed you how to configure a reverse proxy using nginx with Docker. We used a simple example of a web server that serves static files, but you can use this same technique to proxy any type of application.

**Additional resources**

* [NGINX documentation](https://nginx.org/en/docs/)
* [Docker documentation](https://docs.docker.com/)
* [Kubernetes documentation](https://kubernetes.io/docs/)
 
Join ToolsKiemTrieuDoGroup
Back
Top
AdBlock Detected

We get it, advertisements are annoying!

Sure, ad-blocking software does a great job at blocking ads, but it also blocks useful features of our website. For the best site experience please disable your AdBlocker.

I've Disabled AdBlock