2013-06-02 55 views
1

我試圖在nginx中設置一個虛擬位置,以便從目錄提供大型靜態文件。 主要應用程序是一個symfony2程序。NGINX - 在symfony2網站上提供靜態文件

我想要做的是在url中指定一個GET參數中的文件路徑,這樣nginx就可以直接將文件提供給客戶端。

在我的例子位置是/ GETFILE

這裏是我的配置到目前爲止

server 
{ 
    listen            80; 
    server_name           website_url; 
    set             $path_web /myapp/webdir; 

    client_max_body_size        200m; 

location /getfile {    
    root /path_to_dir; 
     if ($args ~ ^oid=(.*+)) { 
      set $key1 $1; 
      rewrite ^.*$ $key1; 
     } 
    } 

location ~ \.php($|/) 
{ 
     set $script  $uri; 
     set $path_info ""; 

     if ($uri ~ "^(.+\.php)(.*)") { 
      set $script  $1; 
      set $path_info $2; 
     } 


     fastcgi_pass unix:/tmp/php-fpm.sock; 
     fastcgi_index index.php; 
     fastcgi_param SCRIPT_FILENAME $path_web$script; 
     fastcgi_param SCRIPT_NAME  $script; 
     fastcgi_param PATH_INFO  $path_info; 

     include /etc/nginx/conf.d/fastcgi.conf; 
} 


    root             $path_web; 
    index             app.php app_dev.php; 

    location/
    { 
    set $idx_file app.php; 

    if ($args ~ debug=true) { 
     set $idx_file app_dev.php; 
     } 

     if (!-f $request_filename) { 
    rewrite ^(.*)$ /$idx_file last; 
     } 
    } 

    location ~ /\.ht 
    { 
    deny     all; 
    } 



    # logs path 
    access_log           /path_to_logdir/access.log main; 
    error_log           /path_to_logdir/error.log; 
} 
+0

這些文件不在你的web根目錄下嗎?如果是這樣,爲什麼不把它們放在那裏或符號鏈接目錄? – Gerry

回答

0

雖然我不知道這是否會工作或沒有,但你已經使用了if和一個setrewrite,太多的非必要的事情,我會嘗試這個

location /getfile?oid=(.*) {    
     rewrite^/path/$1; 
} 
1

我加入我的答案,因爲沒有明確的一個。 我寧願使用try_files,所以它看起來像這樣。

location /my/path { 
    # try to serve file directly, fallback to rewrite 
    try_files $uri $uri @rewrite; 

} 
location @rewrite { 
    rewrite ^/(.*)$ /app.php/$1 last; 
} 

然後,服務器將首先在給定路徑中查找圖像,如果找不到則回退到app.php。它比ifs和elses好得多,因爲它永遠不會回退到app.php。