POST request not redirecting to https
This is my example.conf
's nginx
config file:
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;
}
The issue is when I send a POST
request from POSTMAN
in http
, I get this error:
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>
...
But if change URL to https
, there's no issue:
https://api.example.net/api/user/register/checkuser?phone=0123456789
{"data":{"status":1},"meta":[]}
Am I doing anything wrong?
I should mention that we had a migration from old server Caddy web server
to current one nginx
, and both are Dockerized (extra info only).
From RFC 7231 section 6.4.2, regarding the 301 Moved Permanently status code:
Note: For historical reasons, a user agent MAY change the request method from POST to GET for the subsequent request. If this behavior is undesired, the 307 (Temporary Redirect) status code can be used instead.