This technical guide outlines the procedure for configuring server-side HTTP redirects utilizing the Nginx web server. The process involves modifying a specific configuration file, validating the syntax, and initiating a service restart to apply the new directives.
1. Configuration File Access and Location
The Nginx configuration for redirects is centrally managed within the file:
/etc/nginx/conf.d/example.net/10-redirects.conf
As the designated SSH user (jrc-8h3k-zzzz), you
possess the necessary write permissions to modify this configuration
file. Access the server via SSH and employ a preferred text editor
(e.g., vim, nano) to manipulate the file
contents:
vi /etc/nginx/conf.d/example.net/10-redirects.conf
2. Modifying the Redirect Directives
The 10-redirects.conf file typically contains initial
examples, which should be extended or replaced with custom location
blocks or rewrite directives to define the desired
redirection logic.
A. Utilizing the return Directive
For simple, permanent (301) or temporary (302)
redirects, the return directive is the most efficient
method within a server or location block.
Syntax Example (Permanent Redirect):
location /old-page {
return 301 /new-page;
}
This instructs Nginx to respond with an HTTP 301 Moved Permanently status code, directing the client to the new URI.
B. Utilizing the rewrite Directive
The rewrite directive offers more powerful, regex-based
pattern matching for complex redirection scenarios.
Syntax Example (Regex-based Redirect):
rewrite ^/old/(\w+)/$ /new/index.php?category=$1 permanent;
This example uses a regular expression to capture a portion of the
URI and use it in the target URI, followed by the 301
flag (permanent).
Key Directives:
-
location: Defines a configuration block based on the request URI. -
return: Stops processing and returns the specified status code and optional URL. -
rewrite: Changes the requested URI using regex; requires a flag (e.g.,permanent,redirect,last,break).
3. Applying Configuration Changes
After editing and saving the 10-redirects.conf file,
the Nginx service must be reloaded or restarted for the new directives
to take effect.
A. Command Line Interface (CLI) Procedure
-
Syntax Verification: Prior to restarting, execute the Nginx test command to validate the configuration syntax and prevent service failure.
nginx -tA successful test will output messages confirming the syntax is
OKand the test is successful. -
Service Restart: If the configuration test passes, issue the
systemctlcommand to initiate a graceful service restart of Nginx.systemctl restart nginxThis action reloads the worker processes with the new configuration.
B. AutoPilot Interface
Alternatively, changes can be applied via the GUI:
- Navigate to the AutoPilot Actions Tab.
- Select the "Restart Services" dropdown.
- Choose "Nginx".
- Click the "Restart" button.
4. Verification and Validation
Post-restart, validate the redirection functionality by issuing
requests to the source URL(s) defined in the configuration. Use a
web browser or command-line tools like cURL to inspect the HTTP
response headers for the expected 301 or 302
status code and the correct Location header specifying
the destination URL.
cURL Verification Example:
curl -I https://example.net/old-page
Verify that the output contains the intended HTTP status and
Location header.
Comments
0 comments
Please sign in to leave a comment.