POST 请求未重定向到 https

POST 请求未重定向到 https

这是我example.conf的配置nginx文件:

server {
  listen 80;
  server_name api.example.net;
  return 301 https://api.example.net$request_uri;
  access_log off;
  error_log /dev/stderr;
}

server {
  listen 443 ssl;
  root /var/www/example_api/public;
  server_name api.example.net;
  ssl_certificate /etc/letsencrypt/live/api.example.net/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/api.example.net/privkey.pem;

  location / {
    # try to serve file directly, fallback to index.php
    try_files $uri /index.php$is_args$args;
    add_header Access-Control-Allow-Methods "GET, POST, OPTIONS, PUT, DELETE, PATCH";
    add_header Access-control-Allow-Headers "Content-Range, Authorization,X-Requested-With, counter, id_token, Keep-Alive, User-Agent, Cache-Control, Content-Type, MyApp-Handle-Errors-Generically";
    add_header Access-Control-Expose-Headers "Content-Range, Authorization, id_token, Keep-Alive, User-Agent, Cache-Control, Content-Type, MyApp-Handle-Errors-Generically";
    add_header Access-Control-Max-Age "31536000";
}

  #location ~ ^/index\.php(/|$) {
  # https://stackoverflow.com/questions/68350978/nginx-serving-only-but-not-any-other-files
  location ~ \.php(/|$) {
    try_files $uri $uri/ /index.php?$query_string;
    fastcgi_pass api:9000;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
    fastcgi_param DOCUMENT_ROOT $realpath_root;
    add_header Access-Control-Allow-Methods "GET, POST, OPTIONS, PUT, DELETE, PATCH";
    add_header Access-control-Allow-Headers "Content-Range, Authorization,X-Requested-With, counter, id_token, Keep-Alive, User-Agent, Cache-Control, Content-Type, MyApp-Handle-Errors-Generically";
    add_header Access-Control-Expose-Headers "Content-Range, Authorization, id_token, Keep-Alive, User-Agent, Cache-Control, Content-Type, MyApp-Handle-Errors-Generically";
    add_header Access-Control-Max-Age "31536000";
  }

  location ~ \.php$ {
    return 404;
  }

  access_log off;
  error_log /dev/stderr;
}

问题是,当我POST从 发送请求POSTMANhttp,收到此错误:

api.example.net/api/user/register/checkuser?phone=0123456789

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8" />
    <meta name="robots" content="noindex,nofollow,noarchive" />
    <title>An Error Occurred: Method Not Allowed</title>
...

但如果将 URL 改为https,就没有问题了:

https://api.example.net/api/user/register/checkuser?phone=0123456789

{"data":{"status":1},"meta":[]}

我做错什么了吗?

我应该提到,我们已经从旧服务器迁移Caddy web server到当前服务器nginx,并且两者都是 Dockerized(仅限额外信息)。

答案1

RFC 7231第 6.4.2 节,关于301 永久移动状态码:

注意:由于历史原因,用户代理可能会将后续请求的请求方法从 POST 更改为 GET。如果不希望出现这种情况,则可以使用 307(临时重定向)状态代码。

相关内容