[Nginx] After https is compatible with http, the post request returns 405, 301, indicating that the interface method is not allowed

The app's local request is an http port, and later upgrade https to force a 301 jump, the settings are as follows

server {
    
    
	listen 80;
	server name www.XXX.com;
	rewrite ^/(.*)$   https://www.XXX.com/$1 permanent;
}

Problem description and reason

The request was forced httpto jump to https, and it was found that some functions of the App could not be used.
Because the App has set a total of 4 request methods, namely GET, POST, DELETEand OPTIONSmethod, after setting 301 jump, all request methods become GETmethods, resulting in Some functions do not work properly.
insert image description here

insert image description here

problem solved

The solution is also very simple: Before making a jump, you want to determine whether the request is a get or a post request. If it is a post request, use the proxy_pass method, otherwise use the rewrite method.

  • Don't worry about all GETrequests directly jumping to 301,
  • Non- GETrequested proxy_passfor forwarding, passing parameters to the service

nginxThe configuration is as follows:

server {
    
    
   listen       80;
   server_name  zhyl.xxx.com.cn;
   
   location / {
    
    
     # 所有GET请求直接301跳转不用管,非GET请求的用proxy_pass来转发,将参数传递给服务
     if ($request_method ~ ^(POST|DELETE|OPTIONS)$) {
    
    
         proxy_pass https://zhyl.xxx.com.cn;
         break ;
     }

     #把http的域名请求转成https
     rewrite ^(.*)$ https://${server_name}$1 permanent;
   }
 }

Guess you like

Origin blog.csdn.net/weixin_42960907/article/details/127357788