• Nginx代理中使用斜杠的区别


    代理地址是:http://127.0.0.1:8000

    总结:代理地址加斜杠替换,代理地址不加斜杠拼接

    1、代理地址不加斜杠

    
    # 请求路径为:http://127.0.0.1:8080/api/getInfo  
    # 实际代理为:http://127.0.0.1:8000/api/getInfo
    location ^~/api/ {
      proxy_pass http://127.0.0.1:8000;
      proxy_set_header Host $http_host; 
      proxy_set_header X-Real-IP $remote_addr; 
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    # 请求路径为:http://127.0.0.1:8080/api/getInfo
    # 实际指向为:http://127.0.0.1:8000/api/getInfo
    location ^~/api {
      proxy_pass http://127.0.0.1:8000;
      proxy_set_header Host $http_host; 
      proxy_set_header X-Real-IP $remote_addr; 
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    location定位的路径设置,是否带斜杠,没有关系。

    2、代理地址 + 斜杠

    # 请求路径为:http://127.0.0.1:8080/api/getInfo
    # 实际代理为:http://127.0.0.1:8000/getInfo
    location ^~/api/ {
      proxy_pass http://127.0.0.1:8000/;
      proxy_set_header Host $http_host; 
      proxy_set_header X-Real-IP $remote_addr; 
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    
    # 请求路径为:http://127.0.0.1:8080/api/getInfo
    # 实际代理为:http://127.0.0.1:8000//getInfo
    location ^~/api {
      proxy_pass http://127.0.0.1:8000/;
      proxy_set_header Host $http_host; 
      proxy_set_header X-Real-IP $remote_addr; 
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    如果代理地址加了斜杠,不管location是否加斜杠,api路径都省略了。

    3、代理地址 + 后缀

    
    # 请求路径为:http://127.0.0.1:8080/api/getInfo
    # 实际代理为:http://127.0.0.1:8000/wxgetInfo
    location ^~/api/ {
      proxy_pass http://127.0.0.1:8000/wx;
      proxy_set_header Host $http_host; 
      proxy_set_header X-Real-IP $remote_addr; 
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    # 请求路径为:http://127.0.0.1:8080/api/getInfo
    # 实际代理为:http://127.0.0.1:8000/wx/getInfo
    location ^~/api {
      proxy_pass http://127.0.0.1:8000/wx;
      proxy_set_header Host $http_host; 
      proxy_set_header X-Real-IP $remote_addr; 
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    如果location带斜杠,会省略url中的斜杠。

    4、代理地址 + 后缀 + 斜杠

    
    # 请求路径为:http://127.0.0.1:8080/api/getInfo
    # 实际代理为:http://127.0.0.1:8000/wx/getInfo
    location ^~/api/ {
      proxy_pass http://127.0.0.1:8000/wx/;
      proxy_set_header Host $http_host; 
      proxy_set_header X-Real-IP $remote_addr; 
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    # 请求路径为:http://127.0.0.1:8080/api/getInfo
    # 实际代理为:http://127.0.0.1:8000/wx//getInfo
    location ^~/api {
      proxy_pass http://127.0.0.1:8000/wx/;
      proxy_set_header Host $http_host; 
      proxy_set_header X-Real-IP $remote_addr; 
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    其他

    精确匹配:server_name www.test.com ;
    左侧通配:server_name *.test.com ;
    右侧统配:server_name www.test.* ;
    正则匹配:server_name ~^www\.test\.*$ ;
    
    
    匹配优先级:精确匹配 > 左侧通配符匹配 > 右侧通配符匹配 > 正则表达式匹配
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    
    #直接返回状态码
    location / { 
       return 404;
    }
    #返回状态码 + 一段文本
    location / { 
       return 404 "pages not found"; 
    }
    #返回状态码 + 重定向地址
    location / { 
       return 302 /blog ; 
    }
    #返回重定向地址
    location / { 
       return https://www.test.com ; 
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    # http强制跳转到https
    server {
      listen 80;
      server_name test.com;    
      rewrite ^(.*)$ https://$server_name$1 permanent;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    kafka集群下线broker节点实践方法
    Maven-基本概念
    axios封装
    HTML5期末考核大作业 基于HTML+CSS+JavaScript沪上美食(9页)
    Flutter 项目实战 实现上传头像和个人资料 (五)
    【脑源成像】术前癫痫的电源成像 评价:现状与未来展望
    使用SpringBatch读取csv文件
    树形DP()
    IDEA踩坑记录:查找用法 找到的不全怎么办
    Win11 22H2怎么卸载更新补丁?Win11 22H2卸载更新补丁的步骤
  • 原文地址:https://blog.csdn.net/weixin_45816407/article/details/132901157