当前位置: 首页 > 知识库问答 >
问题:

nginx的问题?

吴驰
2023-04-20

比如现在有个接口,地址是/api/user/info

location /api {
   proxy_pass http://aaa.com
}

那么最终转发后的地址是
http://aaa.com/api/user/info

如果规则改成这样,在转发地址后加个/,

location /api {
   proxy_pass http://aaa.com/
}

最终转发后的地址是:
http://aaa.com/user/info

原因是什么呢?为什么转发地址后加/就会把/api给剃掉呢?
还有写成 /api 和 /api/ 好像效果是一样的,这两者的区别在哪呢?

共有2个答案

庾鸿飞
2023-04-20

proxy_pass

A request URI is passed to the server as follows:

  • If the proxy_pass directive is specified with a URI, then when a request is passed to the server, the part of a normalized request URI matching the location is replaced by a URI specified in the directive:

       location /name/ {
           proxy_pass http://127.0.0.1/remote/;
       }
  • If proxy_pass is specified without a URI, the request URI is passed to the server in the same form as sent by a client when the original request is processed, or the full normalized request URI is passed when processing the changed URI:

       location /some/path/ {
           proxy_pass http://127.0.0.1;
       }

如果 proxy_pass 后面的地址有路径(URI),那么路径会替换 location 里匹配的部分。如果没有路径,那么会直接使用请求的路径。

后面的 / 就是一个路径,有没有它分别进了这两种不同的逻辑。

袁高峰
2023-04-20

推荐看一下这篇教程,规则而已,了解即可。

proxy_pass 带不带路径会决定原始path的替换,方便改写转发后的路由。

https://xuexb.github.io/learn-nginx/example/proxy_pass.html#u...

 类似资料:
  • wordpress搭建的博客从宝塔上换到centos7系统中,nginx配置https,访问域名在开启clash网络代理情况下可以正常访问,但是关闭clash就访问失败。使用PHP8.2、MySql5.7、Nginx1.25 nginx配置 开启防火墙放开80、22、443端口,后关闭防火墙还是不行,然后查看nginx和PHP-fpm的端口,还更换PHP—fpm版本,更该wordpress权限,更

  • 问题内容: 我在gunicorn + nginx上运行django。我在上传文件时遇到了问题。实际上,上传工作正常,但gunicorn超时,因此在nginx中造成了这种情况: 如果刷新页面,可以看到所有照片都已上传。问题在于,这会导致超时,从而给人留下无法上传的印象。 这是我的gunicorn conf: 我尝试更改超时,但是没有用。 问题答案: 你可以尝试通过添加以下内容来升级Nginx中的代理

  • 我有一个socket.iowebservice运行端口8080在我的正式服,它响应超文本传输协议请求,但我认为它有困难解决代理时,我的客户端是发送通过web套接字协议(ws://) 我的客户告诉我服务器以400(错误请求)错误响应,所以要么是我客户端出了问题,要么是我正式服出了问题。我指望这是我的正式服,但我自己或我的同事都不知道具体在哪里。 这些是我们为节点生产盒提供的nginx配置.js。 我

  • 包含片段/fastcgi-php.com;= 这是我的配置,我一辈子都不能让它工作。 我的期望是有超文本传输协议://mediaserver.localdomain/去"/var/www/html/组织者/index.php" 当我转到http://mediaserver时。localdomain/homelab/it提取“/opt/homelab/index.php” 但只有超文本传输协议://

  • nginx 证书问题 已经使用自签名证书生成了一个证书, 并配置到 nginx 中了, 域名 CN 也配置正确了 然后在客户端 hosts 文件中也配置了对应映射 导入了对应的证书文件 然后访问域名, 说证书无效, 每次都到这里卡住, 不知道这证书哪里无效了, 我都导入到了受信任的根证书里面了, 并且在 chrome 的证书管理中也能看到

  • 我在EKS上运行服务已经有一段时间了,今天无意中删除了nginx入口。现在,当我尝试使用以下url中的入口控制器模板设置入口时,只需稍加修改: https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v0.34.1/deploy/static/provider/aws/deploy-tls-termination.