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 http
to 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
, DELETE
and OPTIONS
method, after setting 301 jump, all request methods become GET
methods, resulting in Some functions do not work properly.
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
GET
requests directly jumping to 301, - Non-
GET
requestedproxy_pass
for forwarding, passing parameters to the service
nginx
The 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;
}
}