【问题标题】:NGINX Reverse Proxying & Static FilesNGINX 反向代理和静态文件
【发布时间】:2017-09-09 19:14:37
【问题描述】:

我想通过 NGINX 反向代理我的 NodeJS 后端,但我不断收到 403 禁止错误,由 NGINX 记录为

[error] 10#10: *1 directory index of "/usr/share/nginx/html/" is forbidden, 
client: 172.20.0.1, server: localhost, request: "GET / HTTP/1.1", 
host: "localhost:8888

我对服务器块的配置:

server {

  charset utf8;
  listen 80 default_server;

  location / {
      proxy_pass  http://localhost:5000;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_cache_valid 200 1s;
  }

  location /assets/ {
      expires 30d;
      add_header Cache-Control "public";
      root /usr/share/nginx/html/;
      try_files $uri =404;
  }
}

在做了一些研究之后,似乎它可能与NGINX identifying / as a query for a directory listing 相关,并且很可能需要我添加index index.html 来解决问题(它没有)。我的配置也匹配 that presented by the official NGINX configurations 的反向代理。

有人知道如何解决这个问题吗?

任何帮助将不胜感激! 干杯:)

【问题讨论】:

  • 我不明白错误消息中的localhost:8888 部分。您还应该使用 nginx -T 测试您的配置
  • localhost:8888 是我从中调用请求的主机。我不相信这与它有任何关系,但我的设置在 docker 中运行(参见docker-compose file)。此外,虽然它没有提供更多信息,但您可以找到 nginx -T 打印输出 here

标签: nginx server reverse-proxy


【解决方案1】:

它正在使用服务器 localhost,这是另一个 server 块。

您问题中的 server 块是默认服务器,但另一个具有明确的 server_name localhost 语句,优先。

您可能应该删除另一个服务器块,以便只有一个服务器块,它将始终被使用。

问题文件位于/etc/nginx/conf.d/default.conf

server 块的选择在this document 中进行了说明。

【讨论】:

  • 太棒了!这绝对解决了我的问题!我通过将RUN rm -f /etc/nginx/conf.d/default.conf 命令添加到 nginx dockerfile 来应用您的解决方案。干杯:D
猜你喜欢
  • 2012-09-08
  • 2016-08-19
  • 2019-04-17
  • 2023-04-09
  • 2012-11-16
  • 2012-03-26
  • 2021-02-25
  • 2016-07-17
  • 2014-07-25
相关资源
最近更新 更多