Can nginx replace the 301 redirected content sent by the server?

recently, I have been learning to use Nginx"s https reverse proxy and choose HowNet as the real server to be proxied for testing.

configuration file cnki.conf:

server {
   listen 8080;
   server_name *.zhongxiaocnki.net;
   include enable_ssl.conf;
-sharp-sharp-sharp  log files -sharp-sharp-sharp 
   access_log logs/access.log; 
   error_log logs/error.log debug; 

   location / {
   proxy_pass http://cnki.net;                                      -sharphttps
   proxy_next_upstream error timeout invalid_header http_500 http_502 http_503; 
   proxy_set_header Host $host:$server_port; 
   proxy_set_header X-Real-IP $remote_addr; 
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
   proxy_set_header X-Forwarded-Proto https; 
   proxy_redirect off; 
   
   include url_rewrite/*.conf; 
}
}

configuration file url_rewrite/*.conf:

set $prtc "https";

sub_filter_once off;                                                                                
sub_filter_types *;
sub_filter "http://piccache.cnki.net"  "$prtc://$host:8081";
sub_filter "http://r.cnki.net"  "$prtc://$host:8082";
sub_filter "http://kns.cnki.net"  "$prtc://$host:8083";
sub_filter "http://my.cnki.net"  "$prtc://$host:8084"; 
sub_filter "http://nvsm.cnki.net"  "$prtc://$host:8085"; 
sub_filter "http://www.cnki.net"  "$prtc://$host:8080"; 

proxy cnki.net, when accessing https://zhongxiaotest.com:8080 and proxy some subdomains through other ports by replacing url in response.

now there are two problems:
1. Some url on the page actually point to a directory, but the url does not add"/"at last. As a result, when accessing the URL, the server will return a 301redirect. There is a Location field in the redirect that gives the new url, ending with"/", but the url is HTTP:

.

clipboard.png
(here I didn"t use HTTPS, in order to intercept the package. At this time, this URL happens to be port 8082 of my agent"s HTTP, so it can be accessed successfully. If my agent listens on HTTPS"s 8082, this URL cannot be accessed.)

searched the Internet for a long time about Nginx handling redirection, most of which taught you how to configure redirection yourself, but how to modify the redirection of the server could not be found. But another way was found: listen to HTTPS as well as HTTP, and then rewrite HTTP to HTTPS in the Server block of HTTP. But the port specified in my scene is 8082. A port cannot be monitored by HTTP and HTTPS at the same time, right?

so I want to ask if Nginx can change the Location in the 301redirection sent by the server to the URL it wants. If possible, what instructions are used for which module?

2. Some redirects are done in the script, such as this url: " https://zhongxiaotest.com:8083//kns/RedirectPage.aspx?action=usercenter ". When you click to access the RedirectPage.aspx, action to jump to the user center, how to deal with the jump in this script?
ask for advice!

Mar.10,2021

proxy_redirect learn about

  • Nginx reverse proxy an IP address field

    if an IP address starts at 11.119.0.10-ends at 11.119.0.254 , the outside cannot be accessed, so you need to do a reverse proxy in nginx. The virtual machine started is like taking an IP, port at random in this range. if it is to write an agent, one is ...

    Mar.20,2021
Menu