Creating redirects in Nginx can be useful when you need to redirect traffic from one URL to another. There are two types of redirects you can create in Nginx: temporary (HTTP 302) and permanent (HTTP 301).

Here are the steps to create temporary and permanent redirects in Nginx:

1. Edit the Nginx configuration file for your website or application using a text editor. This file is typically located at "/etc/nginx/conf.d/yourwebsite.conf".

```
sudo nano /etc/nginx/conf.d/yourwebsite.conf
```

2. To create a temporary redirect, add the following lines to the configuration file:

```
location /old-url {
return 302 /new-url;
}
```

Replace "/old-url" with the URL you want to redirect, and "/new-url" with the new URL you want to redirect to.

3. To create a permanent redirect, add the following lines to the configuration file:

```
location /old-url {
return 301 /new-url;
}
```

Replace "/old-url" with the URL you want to redirect, and "/new-url" with the new URL you want to redirect to.

4. Save the configuration file and exit the text editor.

5. Test the redirect by accessing the old URL in a web browser. You should be automatically redirected to the new URL.

Note: If the redirect is not working, you may need to clear your web browser's cache and cookies.

That's it! You have successfully created temporary and permanent redirects in Nginx.

Răspunsul a fost util? 19 utilizatori au considerat informația utilă (58 Voturi)