Nginx设置之自定义请求头
在前后端分离的大超流下,前端单独布局势必会产生请求跨域的情况,那么通过Nginx代理转发可以帮助我们解决这个问题;在特定接口的转发中往往会有设置自定义请求的场景存在,接下来我们来看一个最简单的请求头配置方式。我们先来说明一下配置项的作用和用法:1、proxy_set_header用于设定自定义请求头,例如:keyid:81dcfe44-0e3f-4161-a6e6-******c30fb8的请求头
·
在前后端分离的大超流下,前端单独布局势必会产生请求跨域的情况,那么通过Nginx代理转发可以帮助我们解决这个问题;在特定接口的转发中往往会有设置自定义请求的场景存在,接下来我们来看一个最简单的请求头配置方式。我们先来说明一下配置项的作用和用法:1、proxy_set_header用于设定自定义请求头,例如:keyid:81dcfe44-0e3f-4161-a6e6-******c30fb8的请求头该怎么配置呢?
proxy_set_header keyid 81dcfe44-0e3f-4161-a6e6-******c30fb8; # 自己系统访问api网关的keyid
2、underscores_in_headers on;用于开启请求头key支持下划线'_'特殊符号,默认为off
underscores_in_headers on;
完成配置如下:
user nginx;
worker_processes 1;
#error_log /var/log/nginx/error.log warn;
#pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
gzip on;
underscores_in_headers on; # 用于解决请求头中包含下划线'_'的key
server {
listen 8888;
listen [::]:8888;
server_name _;
root dist/;
location / {
try_files $uri /index.html;
}
# 配置接口代理
location ^~ /fileCenter {
proxy_set_header keyid 81dcfe44-0e3f-4161-a6e6-******c30fb8; # 自定义请求头keyid
proxy_pass https://apitest.i.sino****.com:8060;
rewrite "^/fileCenter/(.*)$" /$1 break;
}
# 配置接口代理
# location ^~ /baidu {
# proxy_pass https://www.baidu.com;
# rewrite "^/baidu/(.*)$" /$1 break;
# }
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /app;
}
location ~ /\.map {
deny all;
}
}
}
更多推荐
已为社区贡献4条内容
所有评论(0)