Nginx · December 18, 2023

Nginx for Newbie: setting up absolute_redirect directive

Nginx for Newbie: Setting up the absolute_redirect Directive

Nginx is a popular web server and reverse proxy server that is known for its high performance, scalability, and flexibility. It is widely used by web developers and hosting providers to serve websites and applications efficiently. One of the many features of Nginx is the ability to configure various directives to customize its behavior. In this article, we will explore the absolute_redirect directive and how to set it up in Nginx.

Understanding the absolute_redirect Directive

The absolute_redirect directive in Nginx allows you to control whether redirects issued by the server should be absolute or relative. By default, Nginx uses relative redirects, which means that the redirect URL is relative to the requested URL. However, in some cases, you may want to use absolute redirects, where the redirect URL includes the scheme and hostname.

Using absolute redirects can be useful in scenarios where you have multiple server blocks or when you want to redirect users to a different domain or subdomain. It ensures that the redirect works correctly regardless of the requested URL.

Setting up the absolute_redirect Directive

To set up the absolute_redirect directive in Nginx, you need to modify your server block configuration. The server block is where you define the settings for a specific website or application. Here's an example of how to enable absolute redirects:

server {
    listen 80;
    server_name example.com;
    absolute_redirect on;
    ...
}

In the above configuration, we have added the absolute_redirect directive and set it to "on" to enable absolute redirects. You can also set it to "off" to use relative redirects.

After making the changes, save the configuration file and restart Nginx for the changes to take effect. You can do this by running the following command:

sudo systemctl restart nginx

Testing the absolute_redirect Directive

Once you have enabled the absolute_redirect directive, you can test it by issuing a redirect from your website or application. You can use tools like cURL or web browsers to check the redirect URL.

For example, if you have a redirect configured in your application code or using Nginx's rewrite rules, you can check the redirect URL to see if it is absolute or relative.

Summary

The absolute_redirect directive in Nginx allows you to control whether redirects issued by the server should be absolute or relative. By default, Nginx uses relative redirects, but you can enable absolute redirects by adding the absolute_redirect directive to your server block configuration.

If you are looking for a reliable VPS hosting provider to host your website or application, consider Server.HK. With their top-notch VPS solutions, you can ensure high performance and scalability for your online presence.