美文网首页
2022-04-30_nginx之proxy_set_heade

2022-04-30_nginx之proxy_set_heade

作者: kikop | 来源:发表于2022-04-30 11:04 被阅读0次

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

http://localhost:8090/api/file/showProxySetHeader

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)]

2.3.3proxy_set_header设置为:http_host

image-20220430103136471.png

相关文章

  • 2022-04-30_nginx之proxy_set_heade

    20220430_nginx之proxy_set_header指令学习笔记.md 1概述 proxy_set_he...

  • Nginx配置获取客户端真实IP的proxy_set_heade

    一、问题背景 在实际应用中,我们可能需要获取用户的ip地址,比如做异地登陆的判断,或者统计ip访问次数等,通常情况...

  • 十之

    博学之,审问之,慎思之,明辨之,笃行之。 励志之,健身之,涅槃之,弘毅之,自强之!

  • 读记|唐诗人:诗心煎红尘(二)

    愈之挫之 险之退之 借之济之 忠之犯之 勇之夺之 衰之立之 坚之韧之 载之言之 一代宗师 成之传之 字曰子厚 道解...

  • 《寄君归》

    思之念之 见之不忘 吾亦求之 求之不得 吾亦念之 兮之盼之 来之归之 欲予离之 得之兮之 心思念之 盼来归之 归之...

  • 飘零

    艾雪儿 难耐心中怦然之 抑之,控之,思之,忘之 能否任之,弃之,拥之,念之 山河为鉴 赴之,游之,悦之,相守之…

  • 《美》

    刚之美,软之美,善之美,心之美。 水之美,声之美,爱之美,景之美,笑之美,物之美,月之美。 仁之美,慈之美,德之美...

  • 众说纷云

    众说纷云 文‖曾之一 20200220 古人说 真之假之善之恶之美之丑之 今人说 真之假之善之恶之美之丑之 后人说...

  • 安沨

    博学之,审问之,慎思之,明辨之,笃行之。

  • 2019-01-13

    博学之、明辨之、慎思之、审问之、笃行之

网友评论

      本文标题:2022-04-30_nginx之proxy_set_heade

      本文链接:https://www.haomeiwen.com/subject/jafyyrtx.html