How to redirect to another URL using nginx?

Redirecting in Nginx can be achieved by making changes to the configuration file. Here are several common methods for redirection.

  1. come back
  2. The website can be found at URL: https://example.com.
server {
    listen 80;
    server_name example.com;
    return 301 https://example.com$request_uri;
}
  1. Rewrite the following in your own words.
  2. Please visit the new URL at example.com/new-path.
server {
    listen 80;
    server_name example.com;
    rewrite ^/(.*)$ https://example.com/new-path permanent;
}
  1. come back
  2. Here is a new example: https://new-example.com
server {
    listen 80;
    server_name example.com;
    return 301 https://new-example.com$request_uri;
}

After making any changes in the configuration file, it is necessary to reload the Nginx configuration file in order for the changes to take effect. You can use the following command to reload the configuration file:

sudo nginx -s reload
bannerAds