Overview
This topic describes how to create a URL forwarding server for forwarding domain names by using the forwarding feature of Nginx.
Prerequisites
1. Nginx is used, and the ngx_http_rewrite_module module is enabled. By default, the module is enabled.
2. Nginx is installed. The following section describes how to install Nginx.
#Download the installation package.
wget http://nginx.org/download/nginx-1.18.0.tar.gz
#Decompress the installation package.
tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0
./configure --prefix=/usr/local/nginx #Replace the example with the installation path based on your business requirements.
make && make install
#Verify whether the package is installed.
cd /usr/local/nginx/sbin
./nginx -t
#The installation is complete if the following content appears:
nginx: the configuration file /usr/local/nginx//conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx//conf/nginx.conf test is successful
Scenarios
URL forwarding applies in the following three scenarios:
url.dns-example.com is the source domain name for redirection, and www.aliyun.com is the destination domain name for redirection. Replace the domain names based on your business requirements.
Scenario 1: Forward url.dns-example.com to www.aliyun.com.
For example, if a visitor enters the URL http://url.dns-example.com/a.txt in the address bar and press enter, the visitor will be redirected to the page with the URL http://www.aliyun.com/a.txt.
Procedure
Edit the nginx.conf file. The file is located in the path that you specified for the --prefix parameter when you install Nginx.
Specify the server_name and location / parameters in the server section.
Set the server_name parameter to url.dns-example.com. Replace url.dns-example.com based on your business requirements.
Add return 302 http://www.aliyun.com$request_uri to the location / parameter. Replace http://www.aliyun.com based on your business requirements.
NoteRedirects are categorized into the following two types: 301 redirects for permanent moves and 302 redirects for temporary moves. If your website does not use search engine technology, select a 302 redirect.
Before configurations:
After configurations:
Example configurations:
server { server_name url.dns-example.com; #Replace url.dns-example.com with the source domain name for redirection. location / { return 302 http://www.aliyun.com$request_uri; #Replace http://www.aliyun.com with the destination domain name for redirection. } }
Restart Nginx.
cd /usr/local/nginx/sbin #Replace it with the installation path based on your business requirements. ./nginx -s reload
Add a DNS record.
After you configure Nginx, point the source domain name for redirection to the IP address of the server where Nginx is installed. This means that you must add an A record for the domain name url.dns-example.com at the Domain Name System (DNS) service provider to point the domain name to 47.94.XXX.XXX. Take note that you must replace the domain name and server IP address based on your business requirements.
The following section describes how to add an A record in the Alibaba Cloud DNS console. For more information, see Add a DNS record.
Log on to the Alibaba Cloud DNS console.
In the left-side navigation pane, click Domain Name Resolution. On the Authoritative Domain Names tab of the page that appears, find the domain name for which you want to add the A record, and click DNS Settings in the Actions column.
Click Add DNS Record and configure the parameters shown in the following figure.
Verify the result.
#Run Nginx on your server. Enter the domain name based on your business requirements. curl -v http://url.dns-example.com/a.txt
Scenario 2: Forward url.dns-example.com to www.aliyun.com/b/.
For example, if a visitor enters the URL http://url.dns-example.com/a.txt in the address bar and press enter, the visitor will be redirected to the page with the URL http://www.aliyun.com/b/a.txt.
The configuration steps are similar to those in scenario 1. The following example shows the configuration content of the nginx.conf file.
server {
server_name url.dns-example.com; #Replace url.dns-example.com with the source domain name for redirection.
location / {
return 302 http://www.aliyun.com/b$request_uri; #Replace http://www.aliyun.com with the destination domain name for redirection.
}
}
Scenario 3: Forward url.dns-example.com/xxx.xxx to http://www.aliyun.com.
For example, if a visitor enters the URL http://url.dns-example.com/xxx.xxx in the address bar and press enter, the visitor will be redirected to the page with the URL http://www.aliyun.com.
The configuration steps are similar to those in scenario 1. The following example shows the configuration content of the nginx.conf file.
server {
server_name url.dns-example.com; #Replace url.dns-example.com with the source domain name for redirection.
location / {
return 302 http://www.aliyun.com; #Replace http://www.aliyun.com with the destination domain name for redirection.
}
}