Docker Nginx Proxy: Sử dụng Nginx Proxy trong môi trường Docker

## Cách sử dụng proxy nginx trong docker

Nginx là một máy chủ proxy ngược nguồn mở phổ biến có thể được sử dụng để cải thiện hiệu suất và khả năng mở rộng của các ứng dụng web của bạn.Trong hướng dẫn này, chúng tôi sẽ chỉ cho bạn cách sử dụng proxy nginx trong thùng chứa Docker.

### Đ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:

* Một động cơ docker chạy trên máy của bạn
* Một dockerfile xác định hình ảnh nginx
* Một ứng dụng web mà bạn muốn ủy quyền

### tạo một dockerfile

Bước đầu tiên là tạo một dockerfile xác định hình ảnh nginx.Dockerfile này có thể đơn giản như sau:

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

Dockerfile này sẽ kéo phiên bản mới nhất của hình ảnh Nginx từ sổ đăng ký Docker Hub.

### Xây dựng hình ảnh nginx

Khi bạn đã tạo Dockerfile, bạn có thể xây dựng hình ảnh Nginx bằng cách chạy lệnh sau:

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

Lệnh này sẽ xây dựng hình ảnh nginx và gắn thẻ nó bằng tên "nginx".

### Tạo một ứng dụng web

Bước tiếp theo là tạo một ứng dụng web mà bạn muốn ủy quyền.Đối với hướng dẫn này, chúng tôi sẽ sử dụng một ứng dụng PHP đơn giản hiển thị ngày và giờ hiện tại.

Bạn có thể tạo ứng dụng PHP bằng cách chạy lệnh sau:

`` `
MKDIR MyApp
CD myapp
Touch Index.php
`` `

Mở tệp `index.php` trong trình soạn thảo văn bản và thêm mã sau:

`` `
<? PHP
ngày lặp lại ('y-m-d h: i: s');
`` `

Lưu tệp `index.php` và thoát trình soạn thảo văn bản.

### Chạy bộ chứa nginx

Bây giờ bạn đã tạo hình ảnh Nginx và ứng dụng web, bạn có thể chạy bộ chứa Nginx.Để thực hiện việc này, hãy chạy lệnh sau:

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

Lệnh này sẽ chạy bộ chứa Nginx và lộ cổng 80 trên máy chủ.

### Kiểm tra proxy nginx

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

`` `
http: // localhost
`` `

Bạn sẽ thấy ngày và giờ hiện tại được hiển thị trong trình duyệt.

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

Trong hướng dẫn này, bạn đã học cách sử dụng proxy nginx trong một thùng chứa docker.Nginx Proxy có thể là một công cụ mạnh mẽ để cải thiện hiệu suất và khả năng mở rộng của các ứng dụng web của bạn.

### hashtags

* #Docker
* #Nginx
* #Reverse proxy
* Ứng dụng #web
* #hiệu suất
=======================================
## How to Use Nginx Proxy in Docker

Nginx is a popular open-source reverse proxy server that can be used to improve the performance and scalability of your web applications. In this tutorial, we will show you how to use Nginx proxy in a Docker container.

### Prerequisites

To follow this tutorial, you will need the following:

* A Docker engine running on your machine
* A Dockerfile that defines the Nginx image
* A web application that you want to proxy

### Create a Dockerfile

The first step is to create a Dockerfile that defines the Nginx image. This Dockerfile can be as simple as the following:

```
FROM nginx:latest
```

This Dockerfile will pull the latest version of the Nginx image from the Docker Hub registry.

### Build the Nginx image

Once you have created the Dockerfile, you can build the Nginx image by running the following command:

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

This command will build the Nginx image and tag it with the name "nginx".

### Create a web application

The next step is to create a web application that you want to proxy. For this tutorial, we will use a simple PHP application that displays the current date and time.

You can create the PHP application by running the following command:

```
mkdir myapp
cd myapp
touch index.php
```

Open the `index.php` file in a text editor and add the following code:

```
<?php
echo date('Y-m-d H:i:s');
```

Save the `index.php` file and exit the text editor.

### Run the Nginx container

Now that you have created the Nginx image and the web application, you can run the Nginx container. To do this, run the following command:

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

This command will run the Nginx container and expose port 80 on the host machine.

### Test the Nginx proxy

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

```
```

You should see the current date and time displayed in the browser.

### Conclusion

In this tutorial, you learned how to use Nginx proxy in a Docker container. Nginx proxy can be a powerful tool for improving the performance and scalability of your web applications.

### Hashtags

* #Docker
* #Nginx
* #Reverse proxy
* #web application
* #performance
 
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