Nginx file_requested 不匹配请求 URL
Nginx file_requested doesn't match request URL
我 运行在 docker 容器中的多个微服务前面使用 traefik + 一个 nginx 容器,其中带有 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 ?感谢任何帮助,谢谢!
事实证明这是 traefik 的问题。我已经以编程方式设置 traefik.frontend.rules
并有一个 PathPrefixStrip
覆盖我想要的规则。我应该阅读 Traefik Matchers a bit more carefully. Also, I found this answer to be very helpful
上的文档
我 运行在 docker 容器中的多个微服务前面使用 traefik + 一个 nginx 容器,其中带有 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 ?感谢任何帮助,谢谢!
事实证明这是 traefik 的问题。我已经以编程方式设置 traefik.frontend.rules
并有一个 PathPrefixStrip
覆盖我想要的规则。我应该阅读 Traefik Matchers a bit more carefully. Also, I found this answer to be very helpful