当内容类型application / json时,nginx,fastcgi PHP请求体空

时间:2017-05-04 12:31:41

标签: php nginx server fastcgi

我正在将十几个移动应用API从Apache转移到Nginx,我很难让API正常工作。我努力让auth标题通过几天(see here),但我终于设法让它工作了。现在,当我尝试使用内容类型<?php include("config.php"); if(isset($_POST['submit'])) { echo $username= $_POST['username']; echo $password= $_POST['password']; $username = addslashes($username); $password = addslashes($password); $username = mysqli_real_escape_string($link, $username); $password = mysqli_real_escape_string($link, $password); $pass= md5($password); $seladmin ="SELECT id,UserName,Password FROM login WHERE UserName='$username' && Password='$pass'"; $SelRecAdmin = mysqli_query( $link,$seladmin ); $row = mysqli_fetch_array($SelRecAdmin); $tot_num_row=mysqli_num_rows($SelRecAdmin); if($tot_num_row >0) { $_SESSION['adminid']=$row['id']; $_SESSION['adminunm'] = $row['UserName']; header('location:home.php'); exit; } else { $_SESSION['msg']= 'Invalid username or password'; header('location:index.php'); exit; } } ?> 发出请求时,application/json为空。奇怪的是,如果我将内容类型更改为$_REQUEST,则application/x-www-form-urlencoded会按预期显示。

现在,我知道简单的答案是更改移动应用程序以使用该内容类型,但由于我们拥有的应用程序数量,这是不可行的。更不用说,不能保证用户会更新他们的应用程序等。

我有什么想法可以解决这个问题?这是我的Nginx配置文件:

这是我的主要nginx.conf http块:

$_REQUEST

这是我的valet.conf:

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;
}

最后,这是我的fastcgi_params文件:

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;
    }
}

1 个答案:

答案 0 :(得分:1)

PHP只是不填充$ _POST / $ _ REQUEST,数据以JSON格式发送。

您需要自己从php://input阅读,例如使用file_get_contents(恕我直言最快的方式)。

之后,您获得了字符串形式的原始主体内容,以便您可以使用json_decode