2012-09-15 51 views
2

我试图让nginx为特定子目录下的请求提供静态文件,而所有其他请求都是反向代理的,但没有运气(获得404s)。例如,我想要http://domain.com/project/static/index.htmlhttp://domain.com/project/static/subdir/file2.html分别映射到/var/www/domain.com/htdocs/index.html和/var/www/domain.com/htdocs/subdir/file2.html。对于静态文件和反向代理的Nginx映射

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

这是我主动nginx的轮廓

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

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

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

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

回答

1

,如果您看了error log,你会立刻明白是什么问题。但你没有。 =(

你的配置:。

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

这不正是你写的东西它取代/project/static/在你的URI的文件路径/var/www/domain.com/htdocs所以,如果请求看起来像http://domain.com/project/static/index.html然后nginx的将尝试打开/var/www/domain.com/htdocsindex.html我假设你不想成为/var/www/domain.com/htdocsindex.html,但你想为/var/www/domain.com/htdocs/index.html那么你应该这样写:

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

Documentation