【问题标题】:Nginx file_requested doesn't match request URLNginx file_requested 与请求 URL 不匹配
【发布时间】:2019-01-13 00:29:44
【问题描述】:

我在 docker 容器中的多个微服务前面运行 traefik + 一个带有 traefik 路径规则的 nginx 容器,用于开发环境的本地服务。当我在端口 3000 上本地运行服务时,nginx 接受它的路径并执行proxy_pass http://host.docker.internal:3000。这几乎适用于所有事情,除了我对静态文件的请求有问题,在这种情况下是.png 文件。

我的 nginx.conf:

user  nginx; worker_processes  auto; pid  /run/nginx.pid;

# Error Log available at error_log  /var/log/nginx/error.log;

events {
    worker_connections  2048;    
}

http {

    # Basic
    include  /etc/nginx/mime.types;
    default_type  application/octet-stream;

    # Logging
    log_format  main    'remote_addr:[$remote_addr]  time_local:[$time_local]  upstream_addr:[$upstream_addr]  status:[$status]  bytes_sent:[$body_bytes_sent]  hostname:[$hostname]  file_requested:[$request_filename] ';
    access_log  /var/log/nginx/http-access.log  main;

    # Tuning for Performance
    sendfile  on;
    expires off;
    client_body_timeout 12;
    client_header_timeout 12;
    keepalive_timeout 15;
    send_timeout 10;

    # Set Client_Buffer Sizes
    client_body_buffer_size 20K;
    client_header_buffer_size 1k;
    large_client_header_buffers 2 16k;
    client_max_body_size 100M;

    # Include Site Conf Files
    include /etc/nginx/sites/*.conf; 
}

我在/etc/nginx/sites中的文件:

server {
    listen 80;
    server_name traefik.domain;

    location /localservice {
        # Set Proxied Headers to Originals
        proxy_pass          http://host.docker.internal:3000;
        proxy_read_timeout  90;
    }

    location /static/localservice {
        # Set Proxied Headers to Originals
        proxy_pass          http://host.docker.internal:3000;
        proxy_read_timeout  90;
    }

}

我在 /microservice 下的所有文件都被正确路由并返回 200,除了 /static 下的文件。从 nginx http-access.log 我有一些奇怪的东西我无法弄清楚:

remote_addr:[172.17.0.2] time_local:[13/Jan/2019:00:24:24 +0000] upstream_addr:[host.ip:3000] status:[404] bytes_sent:[7626] hostname:[f7215dd04bdd] file_requested:[/etc/nginx/html/logos/image.png]

这很奇怪,因为正在生成的请求 url 是: http://traefik.domain/static/localservice/logos/image.png。 nginx 是否从某处的路径重写 /static ?任何帮助表示赞赏,谢谢!

【问题讨论】:

    标签: docker nginx traefik


    【解决方案1】:

    这原来是 traefik 的问题。我以编程方式设置了traefik.frontend.rules,而PathPrefixStrip 覆盖了我想要的规则。我应该更仔细地阅读Traefik Matchers 上的文档。另外,我发现这个答案很有帮助Why does PathPrefixStrip work when PathPrefix won't?

    【讨论】:

      猜你喜欢
      • 2021-04-16
      • 1970-01-01
      • 2018-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-18
      • 1970-01-01
      • 2017-01-17
      相关资源
      最近更新 更多