20220430_nginx之proxy_set_header指令学习笔记.md
1概述
proxy_set_header实现:前端请求通过nginx代理重定义发往后端服务器的请求头,本文通过设置不同的变量值,来实际验证一下结果。
1.1变量定义说明
// 变量定义说明
$host:浏览器请求的ip
$http_host:浏览器请求的ip和端口号,不改变请求头的值
$proxy_host:被代理服务的ip和端口号,改变请求头的值
1.2nginx内置重写的2个值
// nginx内置重写的2个值
proxy_set_header HOST $proxy_host;
proxy_set_header Connection close;
2代码示例
2.1nginx配置
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
# 1.负载均衡
# 配置后台项目负载均衡服务名:backserver(即upstream的后台请求端口映射)
# 指定机器的地址
upstream myvantbackserver {
# 2.设置对应的后台服务地址server,不要加http://
server localhost:8086;
}
server {
# 2.配置nginx监听端口
listen 8090;
# 3.配置nginx服务名,可以是主机域名
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
# 4.添加前后台约定的web访问接口前缀标识api,设置location匹配路由规则2及转发路径proxy_pass
# 设定api的接口前缀映射转发规则,前缀后面的保持不变
location /api {
# 4.1.反向代理
# api后台接口请求地址,http://localhost:8080/api/
# http://localhost:8090/api/file/showProxySetHeader 替换为-->
# http://localhost:8086/api/file/showProxySetHeader
proxy_pass http://myvantbackserver/api/;
# $proxy_host $host $http_host;
proxy_set_header HOST $http_host;
}
#error_page 404 /404.html;
# 5.资源路由交给前端Vue框架处理
# 前端请求访问交给vue主路由入口页面index.html(内置$route.path处理)
error_page 404 /index.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
2.2后台代码
// C200myvantmobileappdemo\myvantparentspringbootdemo\myvantbackdemo\
// src\main\java\com\kikop\controller\FileController.java
@RequestMapping("/showProxySetHeader")
public JSONObject showProxySetHeader(HttpServletRequest request, HttpServletResponse response) {
try {
JSONObject result = new JSONObject();
// Map<String, String[]> reqPparameterMap = request.getParameterMap();
// result.put("reqParameters", reqPparameterMap);
// 1.获取协议请求头
Map<String, String> reqHeaderMap = new LinkedHashMap<>();
Enumeration<String> enumeration = request.getHeaderNames();
if (enumeration != null) {
while (enumeration.hasMoreElements()) {
String key = enumeration.nextElement();
String value = request.getHeader(key);
reqHeaderMap.put(key, value);
}
}
result.put("headers", reqHeaderMap);
return result;
} catch (Exception ex) {
ex.printStackTrace();
return CommonResponse.getCommonResponse(ex);
}
}
2.3测试
说明:机器为同一台,nginx监听端口:8090,后台服务端口:8086
2.3.1proxy_set_header设置为:proxy_host(默认)
// 后台服务器得到的请求头如下:
{
"headers": {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"accept-encoding": "gzip, deflate, br",
"accept-language": "zh-CN,zh;q=0.9,en;q=0.8",
"cache-control": "max-age=0",
"connection": "close",
"host": "myvantbackserver",
"sec-ch-ua": "" Not A;Brand";v="99", "Chromium";v="100", "Google Chrome";v="100"",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": ""Windows"",
"sec-fetch-dest": "document",
"sec-fetch-mode": "navigate",
"sec-fetch-site": "none",
"sec-fetch-user": "?1",
"upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36"
}
}
image-20220430102918504.png
2.3.2proxy_set_header设置为:host
[图片上传失败...(image-219073-1651288039893)]
网友评论