nginx無(wú)法獲取帶下劃線的header值的問題解決
問題描述:因?yàn)轫?xiàng)目用到了xxl,需要header為xxl_sso_sessionid的值,本地沒有問題,放到服務(wù)器上,nginx卻無(wú)法獲取到。
NGINX 默認(rèn)會(huì)將所有包含連字符(-)的請(qǐng)求頭轉(zhuǎn)換為下劃線(_)格式。這是為了符合一些系統(tǒng)的命名規(guī)則,因?yàn)榄h(huán)境變量通常不允許連字符。
要在 NGINX 中直接使用包含連字符的請(qǐng)求頭,需要進(jìn)行一些特殊配置??梢允褂?nbsp;underscores_in_headers
指令來(lái)禁用這種行為,從而保留原始的連字符格式。
解決1. 啟用 underscores_in_headers
首先,確保在 HTTP 塊中啟用 underscores_in_headers
指令:
http { underscores_in_headers on; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_host" "$http_xxl_sso_sessionid" '; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; include /etc/nginx/conf.d/*.conf; }
axios.interceptors.request.use(function (config) { // 在發(fā)送請(qǐng)求之前做些什么 let token = localStorage.getItem("token"); if (token != null && token != 'null') { config.headers["Xxl_sso_sessionid"] = token; } return config; }, function (error) { // 對(duì)請(qǐng)求錯(cuò)誤做些什么 return Promise.reject(error); });
這樣的話,如果前端這樣配置,后端可以直接獲取到header的值。
解決2
前端配置如下,nginx會(huì)自動(dòng)轉(zhuǎn)換成下劃線,也就是xxl_sso_sessionid,就不需要開啟underscores_in_headers on
axios.interceptors.request.use(function (config) { // 在發(fā)送請(qǐng)求之前做些什么 let token = localStorage.getItem("token"); if (token != null && token != 'null') { config.headers["Xxl-sso-sessionid"] = token; } return config; }, function (error) { // 對(duì)請(qǐng)求錯(cuò)誤做些什么 return Promise.reject(error); });
nginx配置
http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_host" "$http_xxl_sso_sessionid" '; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; include /etc/nginx/conf.d/*.conf; upstream takeout-back-api { server localhost:10086 weight=1; } upstream order-back-api { server 8.141.87.136:10000 weight=1; } server { listen 443 ssl; server_name www.domain.com; ssl_certificate xxx.cn_bundle.crt; ssl_certificate_key xxx.cn.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; ssl_prefer_server_ciphers on; location / { root /usr/share/nginx/order-ui/dist; index index.html index.htm; try_files $uri $uri/ /index.html; } location /member { proxy_pass http://takeout-back-api; } location /order { proxy_set_header xxl_sso_sessionid $http_xxl_sso_sessionid; proxy_pass http://order-back-api; } } }
到此這篇關(guān)于nginx無(wú)法獲取帶下劃線的header值的問題解決的文章就介紹到這了,更多相關(guān)nginx獲取下劃線的header值內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Nginx 根據(jù)URL帶的參數(shù)轉(zhuǎn)發(fā)的實(shí)現(xiàn)
這篇文章主要介紹了Nginx 根據(jù)URL帶的參數(shù)轉(zhuǎn)發(fā)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09linux下Nginx+Tomcat負(fù)載均衡配置方法
這篇文章主要介紹了linux下Nginx+Tomcat負(fù)載均衡配置方法,需要的朋友可以參考下2016-09-09nginx高可用集群的實(shí)現(xiàn)過(guò)程
這篇文章主要介紹了nginx高可用集群的實(shí)現(xiàn)過(guò)程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10Nginx+Lua+Redis構(gòu)建高并發(fā)Web應(yīng)用
使用Nginx+Lua+Redis來(lái)構(gòu)建高并發(fā)Web應(yīng)用,Curl請(qǐng)求Nginx,Nginx通過(guò)Lua查詢Redis,返回json數(shù)據(jù)。2013-10-10