Nginx, fastcgi PHP request body empty when content-type application/json
Nginx, fastcgi PHP request body empty when content-type application/json
我正在将大约十几个移动应用 API 从 Apache 转移到 Nginx,但我很难让 API 正常工作。我努力让 auth headers 通过了几天 (),但我终于成功了。现在,当我尝试使用内容类型 application/json
发出请求时,$_REQUEST
为空。奇怪的是,如果我将内容类型更改为 application/x-www-form-urlencoded
,$_REQUEST
会按预期出现。
现在,我知道简单的答案是更改移动应用程序以使用该内容类型,但由于我们拥有的应用程序数量众多,这是不可行的。更不用说,不能保证用户会更新他们的应用程序等
有什么办法可以解决这个问题吗?这是我的 Nginx 配置文件:
这是我的主要 nginx.conf http 块:
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;
}
这是我的 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;
}
}
最后,这是我的 fastcgi_params 文件:
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 只是不使用作为 JSON.
发送的数据填充 $_POST/$_REQUEST
您需要自己从 php://input
阅读它,例如使用 file_get_contents
(恕我直言,这是最快的方式。)
之后,你得到了字符串形式的原始正文内容,这样你就可以在上面使用json_decode
。
我正在将大约十几个移动应用 API 从 Apache 转移到 Nginx,但我很难让 API 正常工作。我努力让 auth headers 通过了几天 (application/json
发出请求时,$_REQUEST
为空。奇怪的是,如果我将内容类型更改为 application/x-www-form-urlencoded
,$_REQUEST
会按预期出现。
现在,我知道简单的答案是更改移动应用程序以使用该内容类型,但由于我们拥有的应用程序数量众多,这是不可行的。更不用说,不能保证用户会更新他们的应用程序等
有什么办法可以解决这个问题吗?这是我的 Nginx 配置文件:
这是我的主要 nginx.conf http 块:
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;
}
这是我的 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;
}
}
最后,这是我的 fastcgi_params 文件:
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 只是不使用作为 JSON.
发送的数据填充 $_POST/$_REQUEST您需要自己从 php://input
阅读它,例如使用 file_get_contents
(恕我直言,这是最快的方式。)
之后,你得到了字符串形式的原始正文内容,这样你就可以在上面使用json_decode
。