AlexSJC
发布于 2024-08-12 / 32 阅读
0
0

Nginx 斜杠魔法

文档

【location】https://nginx.org/en/docs/http/ngx_http_core_module.html#location

Syntax: location [ = | ~ | ~* | ^~ ] uri { ... }
        location @name { ... }
Default: —
Context: server, location

【proxy_pass】https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

Syntax:	proxy_pass URL;
Default: —
Context: location, if in location, limit_excpt

proxy_pass

概括

proxy_pass 后面不带路径,就原封不动传给后端。
proxy_pass 后面带路径,就去掉 Location 再传给后端。

ℹ 哪些 URL 包含路径?

mdn-url-all.png

  • http://host ❌

  • http://host:port ❌

  • http://host/ ✔

  • http://host:port/ ✔

  • http://host:port/api ✔

  • http://host:port/api/ ✔

情况

location:/ proxy_pass:http://localhost:8080

  • / → /

  • /api → /api

location:/api proxy_pass:http://localhost:8080

  • /api → /

  • /api/xx → /api/xx

  • /api-xx → /api-xx

location:/api/ proxy_pass:http://localhost:8080

  • /api/ → /api/

  • /api/xx → /api/xx

location:/ proxy_pass:http://localhost:8080/

  • / → /

  • /api → /api

location:/api proxy_pass:http://localhost:8080/

  • /api → /

  • /api/ → //

  • /api/xx → //xx

location:/api/ proxy_pass:http://localhost:8080/

  • /api/ → //

  • /api/xx → //xx

location:/api proxy_pass:http://localhost:8080/v1

  • /api → /v1

  • /api/ → /v1/

  • /api/xx → /v1/xx

location:/api/ proxy_pass:http://localhost:8080/v1

  • /api/ → /v1

  • /api/xx → /v1xx

location:/api proxy_pass:http://localhost:8080/v1/

  • /api → /v1/

  • /api/ → /v1//

  • /api/xx → /v1//xx

location:/api/ proxy_pass:http://localhost:8080/v1/

  • /api/ → /v1/

  • /api/xx → /v1/xx

参考

https://cloud.tencent.com/developer/article/2422272

https://cloud.tencent.com/developer/article/2421835

https://blog.csdn.net/q1298252589/article/details/120729989

https://blog.csdn.net/caidingnu/article/details/109448094

https://kwey.github.io/2020/11/14/linux/nginx-root-alias/

https://www.cnblogs.com/guzb/p/column-nginx-config_of_location_and_proxy_pass_and_the_difference_of_absence_of_tail_slash.html


评论