【问题标题】:NGINX -- serving static files on an symfony2 websiteNGINX——在 symfony2 网站上提供静态文件
【发布时间】:2013-06-02 14:33:58
【问题描述】:

我正在尝试在 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;
}

【问题讨论】:

  • 这些文件不在您的网络根目录下吗?如果是这样,为什么不将它们放在那里或对目录进行符号链接?

标签: symfony nginx


【解决方案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。

【讨论】:

    【解决方案2】:

    虽然我不确定这是否可行,但你使用了ifsetrewrite,太多不需要的东西,我会试试这个

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

    【讨论】:

      猜你喜欢
      • 2019-08-04
      • 2018-10-23
      • 1970-01-01
      • 2013-05-08
      • 1970-01-01
      • 1970-01-01
      • 2015-10-19
      • 2021-09-03
      • 2018-05-18
      相关资源
      最近更新 更多