NGINX + PHP, $_POST data comes empty after passing through nginx
Here is the issue, when i try to use NGINX (1.18) and PHP (7.4.3) with FPM (lastest from APT) i run into an issue where all of the POST data from my script returns as null
$_POST['name'];
// console after post: name is undefined
The URL i tried posting too:
/posts/new/post
(the user goes on /posts/new to make a new post, the $_POST request goes to /posts/new/posts)
here is my nginx config:
server {
root /forum/;
index index.php;
server_name ***.net;
location / {
try_files $uri $uri/ =404;
}
location /posts/ {
try_files $uri $uri/ @rewrites;
}
# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
location @rewrites {
rewrite ^/posts/(?<id>[a-zA-Z0-9]+)$ /posts/?post=$id;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/***.net/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/***.net/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
}
when i was building these scripts, they worked fine on the PHP built in testing server.
Also note: this server is behind a proxy, but this was tested behind the proxy (going to 192.168.x.x)
edit: also i forgot to state that the $_POST is via a XMLHTTP post via $.ajax from Jquery.
edit 2: Apon looking at the Headers Sent via the Script (on FPM) no post data is sent, but when using the testing server, the post data is sent
Solution 1:
after about 2 hours of useless looking, a simple fix: change
location / {
try_files $uri $uri/ =404;
}
to
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
what caused this issue:
when running a query against an INDEX /
(without /index.php
) the query was not passed onto the /index.php
, so all $_POST
returned as NULL