【问题标题】:nginx try_files from proxy'd app, then nginx来自代理应用程序的 nginx try_files,然后是 nginx
【发布时间】:2013-02-22 03:26:49
【问题描述】:

如果应用程序正在为它们提供服务,我正在尝试从应用程序获取 nginx 反向代理静态文件,否​​则自己提供它们。目前,我有这个配置:

upstream app_server {
    server unix:/tmp/gunicorn.sock fail_timeout=0;
}

server {
    listen          8080;
    server_name     example.com;
    access_log      /var/log/nginx.access.log;
    error_log       /var/log/nginx.error.log;

    keepalive_timeout 5;

    location /static {
        try_files   $uri @proxy_to_app;
        alias       /path/to/__static;
        sendfile    off;
    }

    location / {
        try_files   $uri @proxy_to_app;
    }

    location @proxy_to_app {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;

        proxy_pass   http://app_server;
    }
}

如果文件在/path/to/__static 中不存在,则此方法有效;它将请求发送到应用程序服务器。但是,如果文件也存在于 /path/to/__static 中,则 nginx 会自行提供文件。

在这两种情况下,反转 try_files 行 (try_files @proxy_to_app $uri) 都会失败。如果客户端请求/static/css/test.css,则应用程序会收到对/css/test.css 的请求,即使应用程序返回404,它似乎也不会尝试/path/to/__static

更新为包含完整配置。

【问题讨论】:

  • 请显示您的完整配置。可能最有趣的部分是“@proxy_to_app”。
  • 已更新完整配置
  • 你读过the documentation吗? try_files 指令根本不适合您的任务。您应该使用error_pageproxy_intercept_errors 的组合。

标签: nginx


【解决方案1】:
location /static/ {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass   http://app_server;
    proxy_intercept_errors on;
    error_page 404 =200 /local$uri;
}

location /local/static/ {
    internal;
    alias /path/to/__static/;
}

【讨论】:

  • error_page 404 = /local/$uri; 行不起作用。需要改成error_page 404 =200 /local$uri;
猜你喜欢
  • 2023-03-29
  • 2016-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多