Score:0

How to avoi nginx to redirect a POST to a GET

mu flag

I am see this in my log

"POST /openDoor HTTP/1.1" 301 169 "-" "PostmanRuntime/7.29.0"

"GET /openDoor/ HTTP/1.1" 200 113 "https:///openDoor" "PostmanRuntime/7.29.0"

I am doing a POST to /openDoor and I get a 301. why?

My nginx conf file is this

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

}

How to avoid this strange redirect?

I tried this, without any results

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;
}
Ivan Shatsky avatar
gr flag
Replace `try_files $uri $uri/ /index.php?q=$uri&$args =404;` with `try_files $uri $uri/ /index.php?q=$uri&$args;`
realtebo avatar
mu flag
nothing changes
Ivan Shatsky avatar
gr flag
What is that `/openDoor` prefix? Is it a route that should be processed by the `root /var/www/project/index.php` central controller or is it a directory where some additional webapp is located?
realtebo avatar
mu flag
/openDoor is a physical directory
Score:0
gr flag

Since openDoor is a physical directory, it is nginx that does that 301 /openDoor to /openDoor/ redirect and with the HTTP 301 redirection user browser will change the request method from POST to GET abandoning the request body. You can try to specify the HTTP 308 redirect explicitly with the

location = /openDoor {
    return 308 $request_uri;
}

Although is isn't directly related to your question, you should replace the

try_files $uri $uri/ /index.php?q=$uri&$args =404;

line with the

try_files $uri $uri/ /index.php?q=$uri&$args;

The reason is that try_files directive processed the files with the location block context, and since you don't have a FastCGI handler in that location your index.php file will be served as a plain text. On the other hand the very last try_files parameter will be treated as a new URI and the right location ~ \.php$ { ... } will be chosen to serve it.

realtebo avatar
mu flag
I cannot use a redirect because I call it using a POST (the customer, not my fault.. long sad story as usually), and in this way I got a POST redirected as GET
Ivan Shatsky avatar
gr flag
You mean you got the `POST` method change to the `GET` one even using the `HTTP 308` redirection?
realtebo avatar
mu flag
Yes, Ivan, exactly.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.