Share #16 Sử Dụng $default Route

phamanviet.nga

New member
** Cách sử dụng biến `$ default_route` trong nginx **

Biến `$ default_route` trong Nginx được sử dụng để chỉ định tuyến mặc định cho các yêu cầu không khớp với bất kỳ tuyến đường nào khác được xác định.Điều này có thể hữu ích để phục vụ nội dung tĩnh hoặc chuyển hướng yêu cầu đến một vị trí cụ thể.

Để sử dụng biến `$ default_route`, bạn cần thêm nó vào khối` vị trí` của tệp cấu hình nginx của bạn.Ví dụ:

`` `
vị trí / {
root/var/www/html;
Chỉ mục INDEX.html;
Default_Route /index.html;
}
`` `

Trong ví dụ này, biến `$ default_route` được đặt thành`/index.html`.Điều này có nghĩa là bất kỳ yêu cầu nào không khớp với bất kỳ tuyến đường được xác định nào khác sẽ được chuyển hướng đến `/index.html`.

Bạn cũng có thể sử dụng biến `$ default_route` để phục vụ nội dung tĩnh.Ví dụ: cấu hình sau sẽ phục vụ tất cả các tệp từ thư mục `/var/www/static`:

`` `
Vị trí /tĩnh {
root/var/www/static;
Default_Route;
}
`` `

Bất kỳ yêu cầu nào không phù hợp với bất kỳ tuyến đường được xác định nào khác sẽ được phục vụ từ thư mục `/var/www/static`.

Biến `$ default_route` có thể là một công cụ hữu ích để đơn giản hóa cấu hình nginx của bạn và đảm bảo rằng tất cả các yêu cầu được xử lý chính xác.

** Hashtags: **

* #Nginx
* #cấu hình
* #lộ trình
* #Nội dung
* #Redirects
=======================================
**How to Use the `$default_route` Variable in Nginx**

The `$default_route` variable in Nginx is used to specify the default route for requests that do not match any other defined routes. This can be useful for serving static content or redirecting requests to a specific location.

To use the `$default_route` variable, you need to add it to the `location` block of your Nginx configuration file. For example:

```
location / {
root /var/www/html;
index index.html;
default_route /index.html;
}
```

In this example, the `$default_route` variable is set to `/index.html`. This means that any requests that do not match any other defined routes will be redirected to `/index.html`.

You can also use the `$default_route` variable to serve static content. For example, the following configuration would serve all files from the `/var/www/static` directory:

```
location /static {
root /var/www/static;
default_route;
}
```

Any requests that do not match any other defined routes will be served from the `/var/www/static` directory.

The `$default_route` variable can be a useful tool for simplifying your Nginx configuration and ensuring that all requests are handled correctly.

**Hashtags:**

* #Nginx
* #Configuration
* #Routing
* #Static content
* #Redirects
 
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