问题场景
一个新增数据的请求,在本地测试环境没有任何问题,但是部署到正式环境中,发现有的请求执行了两次
原因
nginx 有一个默认重发机制,对于请求时间太长的请求,会重新发送请,再执行一次。
后果
如果是查询请求,无可厚非,甚至应该鼓励。但是对于新增请求,就会导致一条数据新增了两次。
解决方案
- nginx中可以配置不重新发送请求,这种方式最好配置特定路径,不要把所有的设置为不重发请求。
server {
#加在这为全局匹配
proxy_next_upstream off;
server_name www.域名.com;
root /home/wwwroot/项目名称;
index index.php index.html index.htm;
location / {
if (!-e $request_filename) {
rewrite ^/index.php(.*)$ /index.php?s=$1 last;
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}
}
# 这个地方是重点 包含thirdpayde的url都禁用重发机制
# 这个地方是重点 包含thirdpayde的url都禁用重发机制 ~ \wthirdpay\w 为正则, 可以将\wthirdpay\w替换你要匹配的路径
location ~ \wthirdpay\w {
proxy_next_upstream off;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PHP_VALUE open_basedir=$document_root:/tmp/:/proc/;
include fastcgi_params;
}
- (推荐)将get请求改为post请求,nginx对post请求不会重新发送