Nginx反向代理設置header


特別提示:本人博客部分有參考網絡其他博客,但均是本人親手編寫過並驗證通過。如發現博客有錯誤,請及時提出以免誤導其他人,謝謝!歡迎轉載,但記得標明文章出處: http://www.cnblogs.com/mao2080/

1、問題所在

  在做前后端聯調時,需要對不同的url增加header。

2、代碼片段

2.1Nginx配置

 1     server {
 2         listen       80;
 3         server_name  test.com;
 4         
 5         location /api/ {
 6             proxy_pass http://localhost:8081/api/;
 7             proxy_set_header tenant 1006;
 8         }
 9         
10         location / {
11             root D:\web;
12             index index.html;
13         }
14 
15     }
2.2Java接口
1     @RequestMapping(value = "/checkHeader", method = {RequestMethod.POST}, consumes = MediaType.APPLICATION_JSON_VALUE)
2     @ResponseBody
3     public String checkHeader(HttpServletRequest request) throws IOException {
4         String tenant = request.getHeader("tenant");
5         System.out.println("tenant:"+tenant);
6         return "Success";
7     }

3、執行效果

tenant:1006

4、參考網站

https://www.iyunw.cn/archives/nginx-fan-xiang-dai-li-tian-jia-zi-ding-yi-headers/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM