http-server-redirect #
The ngx_http_server_redirect_module is a custom nginx module designed to facilitate dynamic server redirection based on configurable rules. It allows users to redirect incoming requests to different servers conditionally.
Source: upstream source
Directives
schedule_redirect #
syntax: schedule_redirect on | off (on/off flag); · context: server
Here is an example: This example redirects requests to newserver.com if the Server-Redirect header has value and value is not 0. ### Directive: schedule_redirect Syntax: schedule_redirect on | off Default: schedule_redirect off Context: server Redirect the current request to another server from the first request path.
server_redirect #
syntax: server_redirect 1 arg; · context: server
This process is internal and no 302 redirection will occur. ## Configuration ### Directive: server_redirect Syntax: server_redirect target_host [if=condition] Default: - Context: server Redirect the current request to another server.
Example
http {
server {
listen 80;
server_name example.com;
# Redirect if request has 'X-Redirect' header and value is not 0 or empty.
server_redirect newserver.com if=$http_x_redirect;
# You can use ngx_http_var_module to generate judgment variables based on conditions.
# https://git.hanada.info/hanada/ngx_http_var_module
# var $is_ipv6 if_find $remote_addr :;
# server_redirect newserver.com if=$is_ipv6;
# This module takes effect after the real_ip module,
# Therefore, the real_ip module's directives will take effect on the server before server redirect.
# real_ip_header x-client-ip;
location / {
proxy_pass http://newserver.com;
}
}
server {
listen 80;
server_name newserver.com;
# You can get original host from this variable.
add_header x-original-host $server_redirect_original_host;
location / {
proxy_pass http://upstream.com;
}
}
}