【问题标题】:Nginx mapping for static files and reverse proxy静态文件和反向代理的 Nginx 映射
【发布时间】:2012-09-08 05:52:20
【问题描述】:

我正在尝试让 nginx 为某个子目录下的请求提供静态文件,但没有运气(得到 404),而所有其他请求都是反向代理的。例如,我希望对 http://example.com/project/static/index.htmlhttp://example.com/project/static/subdir/file2.html 的请求分别映射到 /var/www/example.com/htdocs/index.html 和 /var/www/example.com/htdocs/subdir/file2.html .

所有其他请求都应该被反向代理。例如,http://example.com/project/thisWillBeProxied.htmlhttp://example.com/project/subdir/soWillThis.html

这是我的活动 nginx 配置文件

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

    location / {
            proxy_pass         http://reverse-proxy-host/;
    }

    location ^~ /project/static/ {
            alias   /var/www/example.com/htdocs;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
            root   /var/www/nginx-default;
    }
}

【问题讨论】:

    标签: nginx reverse-proxy


    【解决方案1】:

    error log,帮助解释发生了什么。

    你的配置:

    location ^~ /project/static/ {
        alias   /var/www/example.com/htdocs;
    }
    

    它完全按照您所写的。它将 URI 中的 /project/static/ 替换为文件路径 /var/www/example.com/htdocs。因此,如果请求看起来像 http://example.com/project/static/index.html,那么 nginx 将尝试打开 /var/www/example.com/htdocsindex.html。我假设你不想为/var/www/example.com/htdocsindex.html 服务,但你想为/var/www/example.com/htdocs/index.html 服务,那么你应该写:

    location ^~ /project/static/ {
        alias   /var/www/example.com/htdocs/;
    }
    

    Documentation.

    【讨论】:

      【解决方案2】:

      我将尝试使用它: http://wiki.nginx.org/HttpCoreModule#error_page

      从“如果在重定向期间不需要更改 URI,则可以将错误页面的处理重定向到指定位置”

      我不必以这种方式使用静态目录。 :)

      【讨论】:

        猜你喜欢
        • 2017-09-09
        • 2016-08-19
        • 2019-04-17
        • 2023-04-09
        • 2020-05-31
        • 2012-11-16
        • 2012-03-26
        • 2021-02-25
        • 2022-11-28
        相关资源
        最近更新 更多