Question

$_POST variable empty on nginx

Posted December 21, 2015 11.1k views
Nginx Ubuntu LEMP

Hi,
I just created a LEMP droplet with the default configuration.
I’m using a PHP application that redirects all the requests to the main /index.php and this script takes care of all the routing and includes the correct files depending on the requested URI.
I’ve been able to configure nginx to redirect all the request to that file:

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

But it seems that when I POST a form to my website, the $_POST PHP variable is always empty.
I did a search on Google, and found some answers on StackOverflow( like this and this), also tried to apply what I found on nginx’s documentation, but without any success.

My config file is now the following:

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        root /usr/share/nginx/html;
        index index.php index.html;

        ### Comment this once debug finished
        rewrite_log on;
        error_log /var/log/nginx/localhost.error_log notice;
        ### End

        # Make site accessible from http://localhost/
        (...)

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

        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /usr/share/nginx/html;
        }

        location ~ \.php$ {
                try_files = $uri @service;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
                fastcgi_param QUERY_STRING q=$uri&$args;
                fastcgi_param REQUEST_METHOD $request_method;
                fastcgi_param CONTENT_TYPE $content_type;
                fastcgi_param CONTENT_LENGTH $content_length;
                fastcgi_param REQUEST_BODY $request_body;
        }

        location @service {
                fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/index.php;
                fastcgi_param SCRIPT_NAME     /index.php;
                fastcgi_param QUERY_STRING    q=$uri&$args;
        }

        (...)
}

Can anyone help me?
Thanks in advance,
Simone

1 comment
  • At the end switched to a usual LAMP environment, not being able to workaround this problem.
    I’ll leave the question here, ‘case I’m interested anyway in knowing how to make it work. If someone knows :)

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

4 answers

I have seen that same issue deriving from the php.ini file using a bad value for postmaxsize. I see a lot of them use something like “10MB” which is incorrect. Using the B there is invalid, so PHP calls the value 0, which basically means anything sent to POST is invalid. To fix that, you often just need to check the postmaxsize value to ensure it is something like “10M” or “8M” and not “8MB” or “10MB”.

Hope it helps,
Jason Colyer
DigitalOcean Platform Support Lead

Bumped into the same problem in 2019. Still haven’t found any solution to solve it.

Perhaps this whole server management is to much of a headache and some of us should stick to standard hosting :)

OMG, This problem is huge!

It’s can be fixed with php://input, but $_FILES will empty too.

Who can solve this problem? My be problem on Nginx Proxy or /index.php running script?

Not working on 7.2 && 5.6 docker php-fpm

Oh! I lost 8 hours for search answer :(

Solution for empty $_POST and empty $_FILES;

add on your NGINX config lines:

location / {
    try_files $uri $uri/ /index.php?$args;
}
Submit an Answer