How to Make Nginx Redirect?

7 minutes read

To make nginx redirect, you need to use the nginx configuration file. Here's how you can do it:

  1. Open the nginx configuration file. By default, it is located at /etc/nginx/nginx.conf or /etc/nginx/conf.d/default.conf.
  2. Inside the configuration file, find the server block where you want to set up the redirection. This could be the main server block or a specific virtual host server block.
  3. Add the following lines within the server block: location /old-url { return 301 http://example.com/new-url; } In this example, /old-url is the URL you want to redirect from, and http://example.com/new-url is the URL where you want to redirect the traffic.
  4. Save the configuration file and exit.
  5. Finally, restart or reload the nginx service for the changes to take effect: On Ubuntu or Debian: sudo service nginx restart On CentOS or Fedora: sudo systemctl restart nginx


This configuration will perform a 301 redirect, which means a permanent redirect. It will redirect any request made to /old-url to http://example.com/new-url.


Make sure to adjust the URLs and paths according to your specific needs.

Best Nginx Books to Ready in 2024

1
Nginx HTTP Server - Third Edition: Harness the power of Nginx to make the most of your infrastructure and serve pages faster than ever

Rating is 5 out of 5

Nginx HTTP Server - Third Edition: Harness the power of Nginx to make the most of your infrastructure and serve pages faster than ever

2
Mastering NGINX Second Edition

Rating is 4.9 out of 5

Mastering NGINX Second Edition

3
NGINX Cookbook: Over 70 recipes for real-world configuration, deployment, and performance

Rating is 4.8 out of 5

NGINX Cookbook: Over 70 recipes for real-world configuration, deployment, and performance

4
Nginx HTTP Server: Harness the power of Nginx to make the most of your infrastructure and serve pages faster than ever before, 4th Edition

Rating is 4.7 out of 5

Nginx HTTP Server: Harness the power of Nginx to make the most of your infrastructure and serve pages faster than ever before, 4th Edition

5
NGINX Cookbook: Advanced Recipes for High-Performance Load Balancing

Rating is 4.6 out of 5

NGINX Cookbook: Advanced Recipes for High-Performance Load Balancing

6
Nginx Simplified: Practical Guide to Web Server Configuration and Optimization

Rating is 4.5 out of 5

Nginx Simplified: Practical Guide to Web Server Configuration and Optimization


How to redirect a specific domain to a different URL using nginx?

To redirect a specific domain to a different URL using NGINX, you can follow these steps:

  1. Open the NGINX configuration file for the domain using a text editor. The configuration files are usually located in the /etc/nginx/conf.d/ or /etc/nginx/sites-available/ directory, depending on your server setup.
  2. In the configuration file, locate the server block for the specific domain you want to redirect.
  3. Inside the server block, add a location block with the following syntax: location / { return 301 http://www.new-domain.com/new-url; } Replace http://www.new-domain.com/new-url with the URL you want to redirect to.
  4. Save the changes to the configuration file.
  5. Test the NGINX configuration for syntax errors using the command sudo nginx -t. If the configuration is valid, reload NGINX to apply the changes by executing sudo service nginx reload or sudo systemctl reload nginx.


After completing these steps, whenever someone accesses the specific domain, NGINX will redirect them to the new URL.


How to make nginx redirect to a specific port?

To make Nginx redirect to a specific port, you can configure a reverse proxy in Nginx. Here is how you can do it:

  1. Open the Nginx configuration file using a text editor. The file is usually located at /etc/nginx/nginx.conf or /etc/nginx/sites-available/default.
  2. Inside the http block, add a new server block to define the redirect rule. For example: server { listen 80; location / { proxy_pass http://localhost:8080; # Redirect to port 8080 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } In this example, Nginx listens on port 80 and redirects all incoming requests to http://localhost:8080.
  3. Save the configuration file and exit the text editor.
  4. Restart Nginx to apply the changes. The command to restart Nginx depends on your operating system. For example: sudo service nginx restart # For Ubuntu/Debian sudo systemctl restart nginx # For CentOS/RHEL


With this configuration, Nginx acts as a reverse proxy, redirecting all requests coming to the specified port (e.g., port 80) to the desired port on the same or another server.


How to redirect based on the request method (GET/POST/PUT) in nginx?

To redirect based on the request method in Nginx, you can use the map directive combined with return statements. Here's an example configuration:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
http {
  map $request_method $redirect_url {
    default       https://www.example.com/;  # default redirect URL
    GET           https://www.example.com/get;
    POST          https://www.example.com/post;
    PUT           https://www.example.com/put;
  }

  server {
    listen 80;
    server_name example.com;

    location / {
      return 302 $redirect_url;
    }
  }
}


In this example, the map directive is used to define the redirect URL based on the request method. The default value is set as the default redirect URL in case the request method does not match any of the specified conditions.


Inside the server block, the location block is used to configure the behavior for all requests to the root path ("/"). The return statement with a status code of 302 (temporary redirect) is used to perform the redirection, using the value from the $redirect_url variable based on the request method.


Remember to adjust the server_name and the redirect URLs (https://www.example.com/get, https://www.example.com/post, https://www.example.com/put) according to your own domain and requirements.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

To use NGINX to host a website, follow these steps:Install NGINX: Begin by installing NGINX on your server or computer. The installation process may vary depending on your operating system. NGINX has official documentation to guide you through the installation...
To redirect a specific URL using .htaccess, you can use the Redirect directive followed by the URL you want to redirect from and the URL you want to redirect to. For example, to redirect "example.com/oldpage" to "example.com/newpage", you can a...
To increase the NGINX timeout, you need to make changes to the NGINX configuration file. Here's how:Locate the NGINX configuration file. It is typically named nginx.conf or nginx.conf.sample and is usually located in the /etc/nginx/ directory. Open the NGI...
To enable Brotli compression in NGINX, you can follow these steps:Start by installing the necessary tools. Ensure that you have the NGINX web server installed on your system. You also need the Brotli compression library and the ngx_brotli module for NGINX. Onc...
To configure Nginx in Ubuntu, you need to perform the following steps:Install Nginx: Begin by installing Nginx using the package manager of Ubuntu. Enter the command sudo apt-get install nginx in the terminal to perform the installation. Start Nginx: After the...
To install Nginx in Arch Linux, you can follow these steps:Update the package manager by running the command: sudo pacman -Syu Install Nginx by executing the command: sudo pacman -S nginx Once the installation is complete, start the Nginx service using: sudo s...