在Nginx中使用fastcgi传递请求标头 [英] Passing request headers using fastcgi in Nginx
问题描述
我已经退出了一些使用Oauth(不是Oauth2)的API.我们正在将所有站点从Apache移到Nginx(使用fastcgi),但我遇到了无法发送请求标头的问题.我已经阅读了许多帖子和Nginx文档,但仍然无法获取标题参数.
I have quit a few API's that use Oauth (not Oauth2). We are moving all of our sites from Apache to Nginx (using fastcgi) and I am running into an issue with request headers not being sent. I have read numerous posts and the Nginx docs and I am still not able to get the header params to pass through.
我正在使用Postman发出API请求并设置以下标头:
I am using Postman to make API requests and setting these headers:
我终于设法获得了var_dump($_SERVER);
时显示的标头的键,但我无法获得要传递的实际值.
I did finally manage to get the key of the header to show up when I var_dump($_SERVER);
but I cannot get the actual value to pass through.
这是我的主要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;
这是var_dump($_SERVER)
响应的图像.注意:此var_dump
在调用应用程序中的任何其他内容之前.
Here is an image of the response of the var_dump($_SERVER)
. NOTE: This var_dump
is before anything else in the app gets called.
推荐答案
好吧,经过2天的挖掘并试图弄清楚这一点,我终于使它工作了.缺少的部分是添加以下行:
Well, after 2 days of digging around and trying to figure this out, I finally got it working. The missing piece was to add this line:
underscores_in_headers on;
在nginx.conf
的主http
块中.因此,这是我的nginx.conf
的最后一个http
块的样子:
In my main http
block in the nginx.conf
. So, here is what the final http
block of my nginx.conf
looks like:
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;
underscores_in_headers on; # This beauty right here :D
include /Users/webdev2/.valet/Nginx/*;
include servers/*;
include valet/valet.conf;
}
这篇关于在Nginx中使用fastcgi传递请求标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!