简体   繁体   中英

Nginx, fastcgi PHP request body empty when content-type application/json

I am moving about a dozen mobile app API's over from Apache to Nginx and I am having a hard time getting the API's to work correctly. I struggled with getting the auth headers to pass through for a couple of days ( see here ) but I finally managed to get that working. Now, when I attempt to make a request with the content type of application/json , $_REQUEST is empty. Oddly enough, if I change the content type to application/x-www-form-urlencoded , the $_REQUEST is present as expected.

Now, I know the simple answer would be to change the mobile apps to use that content type, but that is not feasible because of the amount of applications we have. Not to mention, there is not guarantee that the users will update their app, etc.

Any ideas how I can solve this? Here is my Nginx conf files:

Here is my main nginx.conf http block:

http {
    include mime.types;
    default_type  application/octet-stream;

    sendfile on;
    keepalive_timeout  6000;
    client_max_body_size 128M;

    gzip  on;
    gzip_comp_level 5;
    gzip_min_length 256;
    gzip_proxied any;
    gzip_vary on;

    gzip_types
    application/atom+xml
    application/javascript
    application/json
    application/rss+xml
    application/vnd.ms-fontobject
    application/x-font-ttf
    application/x-web-app-manifest+json
    application/xhtml+xml
    application/xml
    font/opentype
    image/svg+xml
    image/x-icon
    text/css
    text/plain
    text/x-component;

    include /Users/webdev2/.valet/Nginx/*;
    include servers/*;
    include valet/valet.conf;
}

And here is my valet.conf:

server {
    listen 80 default_server;
    root /;
    charset utf-8;

    location /41c270e4-5535-4daa-b23e-c269744c2f45/ {
        internal;
        alias /;
        try_files $uri $uri/;
    }

    location / {
        rewrite ^ /Users/webdev2/.composer/vendor/laravel/valet/server.php last;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log /Users/webdev2/.valet/Log/nginx-error.log;

    error_page 404 /Users/webdev2/.composer/vendor/laravel/valet/server.php;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/Users/webdev2/.valet/valet.sock;
    fastcgi_pass_request_headers on;
    fastcgi_pass_header Authorization;
    fastcgi_pass_header http_oauth_token;
    fastcgi_pass_header oauth_token_secret;
        fastcgi_index /Users/webdev2/.composer/vendor/laravel/valet/server.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME /Users/webdev2/.composer/vendor/laravel/valet/server.php;
        fastcgi_read_timeout 300;
    }

    location ~ /\.ht {
        deny all;
    }
}

And finally, here is my fastcgi_params file:

fastcgi_param QUERY_STRING  $query_string;
fastcgi_param REQUEST_METHOD  $request_method;
fastcgi_param CONTENT_TYPE  $content_type;
fastcgi_param CONTENT_LENGTH  $content_length;
fastcgi_param SCRIPT_FILENAME  $request_filename;
fastcgi_param SCRIPT_NAME  $fastcgi_script_name;
fastcgi_param REQUEST_URI  $request_uri;
fastcgi_param DOCUMENT_URI  $document_uri;
fastcgi_param DOCUMENT_ROOT  $document_root;
fastcgi_param SERVER_PROTOCOL  $server_protocol;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE  nginx/$nginx_version;
fastcgi_param REMOTE_ADDR  $remote_addr;
fastcgi_param REMOTE_PORT  $remote_port;
fastcgi_param SERVER_ADDR  $server_addr;
fastcgi_param SERVER_PORT  $server_port;
fastcgi_param SERVER_NAME  $server_name;
fastcgi_param HTTPS   $https if_not_empty;
fastcgi_param REDIRECT_STATUS  200;
fastcgi_param HTTP_PROXY  "";
fastcgi_param HTTP_AUTHORIZATION $http_authorization;
fastcgi_param OAUTH_TOKEN $http_oauth_token;
fastcgi_param OAUTH_TOKEN_SECRET $http_oauth_token_secret;

PHP simply does not populate $_POST/$_REQUEST with data send as JSON.

You need to read it from php://input yourself, for example using file_get_contents (quickest way IMHO.)

After that, you got the raw body content in string form, so that you can use json_decode on it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM