首页 文章

静态文件和反向代理的Nginx映射

提问于
浏览
2

我正在尝试,没有运气(获得404s),让nginx为某个子目录下的请求提供静态文件,而所有其他请求都是反向代理的 . 例如,我希望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 Profiles

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;
    }
}

2 回答

  • 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 .

  • 0

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

    阅读“如果在重定向期间无需更改URI,则可以将错误页面的处理重定向到命名位置”

    我不必这样使用静态目录 . :)

相关问题