Automatic redirection from http to http by nginx service under Linux

Keywords: Nginx vim curl

1. Realize automatic jump to http://bbs.westos.org when accessing http://xin.westos.org/bbs//

 

(1) Firstly, a virtual host is configured and tested to ensure that the configured virtual host is correct.

[root@server1 ~]# vim /usr/local/nginx/conf/nginx.conf   #Write the following server module in the http module (write 151-159 lines, 160 lines} which is provided by the http module.)
151     server {
152         listen 80;
153         server_name bbs.westos.org;
154     
155         location / {
156                 root /bbs;
157                 index index.html;
158         }
159     }
160 }


[root@server1 ~]# mkdir /bbs
[root@server1 ~]# echo bbs.westos.org > /bbs/index.html

[root@server1 ~]# /usr/local/nginx/sbin/nginx -s reload   #After writing the configuration file, overload the nginx service


[root@foundation83 ~]# vim /etc/hosts   #Write local parsing files on the physical machine to ensure that bbs.westos.org is parsed
172.25.83.1     bbs.westos.org

 

(2) When accessing http://xin.westos.org/bbs/, automatically jump to http://bbs.westos.org/

[root@server1 ~]# vim /usr/local/nginx/conf/nginx.conf   #Add 140 lines of comments and 141 lines of page rewrite commands
137     server {
138         listen 80;
139         server_name xin.westos.org;
140         #rewrite ^/(.*)$ https://xin.westos.org/$1 permanent;
141         rewrite ^/bbs$ http://Bbs.westos.org/permanent;# indicates that when accessing http://xin.westos.org/bbs, it automatically jumps to http://bbs.westos.org.
142         #set_real_ip_from 172.25.83.0/24;
143         #real_ip_header X-Forwarded-For;
144         #real_ip_recursive on;
145 
146         #location / { 
147                 #return 200 "client real ip: $remote_addr\n";
148                 #root /web;
149                 #index index.html;
150         #}   
151     }
152     server {
153         listen 80;
154         server_name bbs.westos.org;
155 
156         location / {
157                 root /bbs;
158                 index index.html;
159         }
160     }
161 }

[root@server1 ~]# /usr/local/nginx/sbin/nginx -s reload   #After modifying the configuration file, overload the nginx service

 

(3) Testing:

 

Test 1: Testing on the command line

[root@server1 ~]# curl -I xin.westos.org/bbs
HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.2
Date: Fri, 26 Apr 2019 09:13:27 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://Bbs.westos.org/ # We will find that the automatic jump of xin.westos.org/bbs - > http://bbs.westos.org is realized.
   

 

Test 2: Visit in the browser: http://xin.westos.org/bbs To help us automatically jump to the following interface.

2. Realize that when accessing http://xin.westos.org/bbs/..., automatically jump to http://bbs.westos.org/... (i.e. improve the content of 1)

 

Before making improvements, let's do a test to see the need for improvement.

[root@server1 ~]# curl -I xin.westos.org/bbs/index.html
HTTP/1.1 404 Not Found
Server: nginx/1.14.2
Date: Fri, 26 Apr 2019 09:17:28 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive   #Here, we find a problem (no redirection was achieved after adding / index.html to bbs), which is obviously unreasonable.

Access in the browser: http://xin.westos.org/bbs/index.html It didn't automatically jump to http://bbs.westos.org.

Next, let's make improvements.

 

(1) Writing nginx.conf file

[root@server1 ~]# vim /usr/local/nginx/conf/nginx.conf   #Add 141 lines of comments and 142 lines of page rewrite commands
137     server {
138         listen 80;
139         server_name xin.westos.org;
140         #rewrite ^/(.*)$ https://xin.westos.org/$1 permanent;
141         #rewrite ^/bbs$ http://bbs.westos.org/ permanent;
142         rewrite ^/bbs/(.*)$ http://bbs.westos.org/$1 permanent;
143         #set_real_ip_from 172.25.83.0/24;
144         #real_ip_header X-Forwarded-For;
145         #real_ip_recursive on;
146 
147         #location / { 
148                 #return 200 "client real ip: $remote_addr\n";
149                 #root /web;
150                 #index index.html;
151         #}   
152     }
153     server {
154         listen 80;
155         server_name bbs.westos.org;
156 
157         location / {
158                 root /bbs;
159                 index index.html;
160         }
161     }
162 }


[root@server1 ~]# /usr/local/nginx/sbin/nginx -s reload   #After writing the configuration file, overload the nginx service

 

(2) Testing:

 

Test 1: Testing on the command line

[root@server1 ~]# curl -I xin.westos.org/bbs/index.html
HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.2
Date: Fri, 26 Apr 2019 09:21:57 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://Bbs.westos.org/index.html# We will find that the automatic jump of xin.westos.org/bbs/index.html - > http://bbs.westos.org/index.html is realized.
   

 

Test 2: Visit in the browser: http://xin.westos.org/bbs/index.html To help us automatically jump to the following interface.

 

3. When accessing http://bbs.westos.org, automatically jump to http://xin.westos.org/bbs/...

 

(1) Writing nginx.conf file

[root@server1 ~]# vim /usr/local/nginx/conf/nginx.conf   #Add 142 lines of comments, 143 lines, 144 lines and 145 lines of web page rewriting commands, add bbs.westos.org to the server_name variable of 139 lines, remove 150, 152, 153, 154 lines of comments, and add the virtual host annotations of bbs.westos.org.
137     server {
138         listen 80;
139         server_name xin.westos.org bbs.westos.org;
140         #rewrite ^/(.*)$ https://xin.westos.org/$1 permanent;
141         #rewrite ^/bbs$ http://bbs.westos.org/ permanent;
142         #rewrite ^/bbs/(.*)$ http://bbs.westos.org/$1 permanent;
143         if ($host = "bbs.westos.org"){
144                 rewrite ^/(.*)$ http://xin.westos.org/bbs/$1 permanent;
145         }
146         #set_real_ip_from 172.25.83.0/24;
147         #real_ip_header X-Forwarded-For;
148         #real_ip_recursive on;
149 
150         location / { 
151                 #return 200 "client real ip: $remote_addr\n";
152                 root /web;
153                 index index.html;
154         }   
155     }
156     #server {
157         #listen 80;
158         #server_name bbs.westos.org;
159 
160         #location / {
161                 #root /bbs;
162                 #index index.html;
163         #}
164     #}
165 }


[root@server1 ~]# /usr/local/nginx/sbin/nginx -s reload   #After writing the configuration file, overload the nginx service

 

(2) Testing:

 

[root@server1 ~]# vim /etc/hosts   #Write local parsing files
172.25.83.1    bbs.westos.org

[root@server1 ~]# cp -r /bbs/ /web/   #Copy bbs directory to / web Directory
[root@server1 ~]# ll /web/
total 8
drwxr-xr-x 2 root root 24 Apr 26 17:43 bbs
-rw-r--r-- 1 root root 15 Apr 26 15:13 index.html
-rw-r--r-- 1 root root  5 Apr 26 16:24 test.html

 

Test 1: Testing on the command line

[root@server1 ~]# curl -I bbs.westos.org
HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.2
Date: Fri, 26 Apr 2019 09:46:09 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://Xin.westos.org/bbs/ # We can see that automatic jumps have been achieved (bbs.westos.org - > https://xin.westos.org/bbs)

[root@server1 ~]# curl -I bbs.westos.org/index.html
HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.2
Date: Fri, 26 Apr 2019 09:46:12 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://Xin.westos.org/bbs/index.html# We can see that automatic jumping has been achieved (bbs.westos.org/index.html - > https://xin.westos.org/bbs/index.html)

 

Test 2: Visit in the browser: http://bbs.westos.org/ To help us automatically jump to the following interface.

Access in the browser: http://bbs.westos.org/index.html To help us automatically jump to the following interface.

Posted by Chapel on Fri, 26 Apr 2019 06:24:35 -0700