我在我的日志中看到了这个
“POST /openDoor HTTP/1.1” 301 169 “-” “PostmanRuntime/7.29.0”
“GET /openDoor/HTTP/1.1” 200 113 “https:///openDoor” “PostmanRuntime/7.29.0”
我正在向 /openDoor 发送 POST 请求,但得到了 301。为什么?
我的 nginx conf 文件是这样的
server {
client_body_buffer_size 30M;
client_max_body_size 30M;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
proxy_buffer_size 1024k;
proxy_buffers 32 2048k;
proxy_busy_buffers_size 2048k;
}
location / {
index index.php;
try_files $uri $uri/ /index.php?q=$uri&$args =404;
}
error_log /var/log/nginx/error.log warn;
access_log /var/log/nginx/access.log combined;
server_name <redacted>;
root /var/www/project;
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/<redacted>/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/<redacted>/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
如何避免这种奇怪的重定向?
我试过了,但没有任何结果
location /openDoor {
add_header Cache-Control no-cache;
expires -1;
add_header Cache-Control no-store;
try_files /openDoor/index.php =404;
}
location /openDoor/ {
add_header Cache-Control no-cache;
expires -1;
add_header Cache-Control no-store;
try_files /openDoor/index.php =404;
}
答案1
由于openDoor
是物理目录,因此 nginx 会执行 301/openDoor
重定向/openDoor/
,并且使用 HTTP 301 重定向,用户浏览器会将请求方法从 更改POST
为GET
放弃请求正文。您可以尝试指定HTTP 308 重定向明确地
location = /openDoor {
return 308 $request_uri;
}
虽然这与您的问题没有直接关系,但您应该替换
try_files $uri $uri/ /index.php?q=$uri&$args =404;
符合
try_files $uri $uri/ /index.php?q=$uri&$args;
原因是该try_files
指令使用块上下文处理文件location
,并且由于您在该位置没有 FastCGI 处理程序,因此您的index.php
文件将以纯文本形式提供。另一方面,最后一个try_files
参数将被视为新的 URI,并将location ~ \.php$ { ... }
选择正确的 URI 来提供它。