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; 
}
#请求路径为: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; 
}

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; 
}
# 请求路径为: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; 
}

如果代理地址加了斜杠,不管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; 
}
# 请求路径为: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; 
}

如果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; 
}
# 请求路径为: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; 
}

其他

精确匹配:server_name www.test.com ;
左侧通配:server_name *.test.com ;
右侧统配:server_name www.test.* ;
正则匹配:server_name ~^www\.test\.*$ ;

匹配优先级:精确匹配 > 左侧通配符匹配 > 右侧通配符匹配 > 正则表达式匹配

#直接返回状态码
location / { 
   return 404;
}
#返回状态码 + 一段文本
location / { 
   return 404 "pages not found"; 
}
#返回状态码 + 重定向地址
location / { 
   return 302 /blog ; 
}
#返回重定向地址
location / { 
   return https://www.test.com ; 
}

http强制跳转到https

server {
  listen 80;
  server_name test.com;    
  rewrite ^(.*)$ https://$server_name$1 permanent;
}
  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
nginx是一个高性能的HTTP和反向代理服务器,它可以通过配置文件来进行灵活的定制。在nginx中,反斜杠主要用于转义特殊字符,以便正确解析和处理请求。 以下是关于nginx斜杠配置的一些详解: 1. 转义符号:在nginx配置文件中,使用反斜杠(\)作为转义符号。当需要使用特殊字符时,可以在其前面加上反斜杠,以避免对它们的解析错误。 2. 转义特殊字符:在nginx配置文件中,一些特殊字符需要进行转义,以确保它们被正确识别和处理。例如,如果要在一个location块中配置路径包含反斜杠(/)字符,可以使用反斜杠进行转义,如下所示: ``` location /path\/with\/slashes { ... } ``` 在这个例子中,路径"/path/with/slashes"中的每个斜杠都被反斜杠转义。 3. 转义正则表达式:在nginx的rewrite指令中,正则表达式是常见的用法之一。当需要在正则表达式中使用特殊字符时,同样需要进行转义。例如,如果要匹配含有点(.)的URL路径,可以使用反斜杠进行转义,如下所示: ``` location / { rewrite ^/path\.html$ /newpath.html; } ``` 在这个例子中,正则表达式中的点(.)被反斜杠转义,以确保它只匹配实际的点字符。 4. 转义URL:在nginx配置中,URL也可能需要进行转义。特别是在配置代理服务器时,可能会遇到一些特殊字符,如空格、问号等。使用反斜杠对这些字符进行转义,可以确保nginx正确地解析和处理URL。 这些是关于nginx斜杠配置的一些详解,希望对你有所帮助。如有更多问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值