UWSGI未从Nginx服务器接收自定义标头

时间:2018-09-11 20:47:26

标签: nginx flask uwsgi

目标是fmod服务器将名为nginx的自定义请求标头传递到X-PA-AUTH_TOKEN服务器。以下是uwsgi文件中的server块。

nginx.conf

server { listen 9390 default_server; server_name _; location / { include uwsgi_params; uwsgi_pass_request_headers on; uwsgi_pass unix:/run/uwsgi/irm.sock; uwsgi_pass_header X-PA-AUTH_TOKEN; if ($request_method = 'OPTIONS') { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; # # Custom headers and headers various browsers *should* be OK with but aren't # add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,X-PA-AUTH_TOKEN'; # # Tell client that this pre-flight info is valid for 20 days # add_header 'Access-Control-Max-Age' 1728000; add_header 'Content-Type' 'text/plain; charset=utf-8'; add_header 'Content-Length' 0; add_header 'X-PA-AUTH_TOKEN' '0'; return 204; } if ($request_method = 'POST') { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,X-PA-AUTH_TOKEN'; add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range,X-PA-AUTH_TOKEN'; } if ($request_method = 'GET') { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,X-PA-AUTH_TOKEN'; add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range,X-PA-AUTH_TOKEN'; } } } 服务器从uwsgi服务器接收常规的标头,例如“ Content-Type”,就好了。

但是,nginx服务器仍然没有收到来自uwsgi服务器的自定义请求标头X-PA-AUTH_TOKEN,例如,在发出POST请求时。

我在这里做什么错了?

1 个答案:

答案 0 :(得分:0)

我相信默认情况下,nginx将带有下划线(X-PA-AUTH_TOKEN)的标头标记为无效,因此它们被阻止了。您可以在nginx配置(docs here)中启用underscores_in_headers: on;,也可以将标头重命名为X-PA-AUTH-TOKEN

但是请注意,如果启用underscores_in_headers,则在烧瓶侧实际收到的内容将被重命名为X-Pa-Auth-Token,因此我想最好是首先重命名标头而不要打扰使用nginx设置。