Nginx file_requested与请求URL不匹配

时间:2019-01-13 00:29:44

标签: docker nginx traefik

我正在docker容器+一个具有traefik路径规则的ngimx容器的多个微服务前面运行traefik,该traefik路径规则适用于开发环境的本地服务。当我在端口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吗?任何帮助表示感谢,谢谢!

1 个答案:

答案 0 :(得分:0)

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