2016-03-26 34 views
2

我想启用大文件上传到一个位置,然后该位置被重写到另一个位置。看起来重写是重置其他配置。如何在nginx中结合rewrite和client_max_body_size?

我的配置:

server { 
    listen 80; 

    server_name example.com; 
    root /var/www; 
    index index.php; 

    # this location requires large file upload 
    location /upload { 
     client_max_body_size 512M; 
     rewrite ^(.*)$ /index.php?request=$1 last; 
    } 

    # all other locations 
    location/{ 
     rewrite ^(.*)$ /index.php?request=$1 last; 
    } 

    # pass the PHP scripts to FPM 
    location ~ \.php$ { 
     include /etc/nginx/includes/php; 
    } 
} 

如果我移动client_max_body_size出location,进入server,那么它的工作原理。

如果我把它放在location /uploadlocation ~ \.php$,那么它也可以。但我不希望其他位置能够上传大文件。

我在想我可以直接在location /upload上直接使用PHP,但是一旦我运行重写,它将会寻找另一个位置。这是否意味着我将不得不有两个单独的位置的PHP脚本?重写后有什么办法可以让client_max_body_size通过其他地方保留?

回答

0

如果您需要特定的client_max_body_size,需要在处理最终URI的location中设置(或继承)。在你的情况下,那是location ~ \.php$

正如您在您的问题中指出的,最简单的解决方案是在location /upload中处理PHP文件。这很容易实现,因为您已经在单独的包含文件中拥有PHP配置。

无论是fastcgi_param指令的rewrite ... break;或压倒一切两者应该为你工作:

选项1:

location /upload { 
    client_max_body_size 512M; 

    rewrite ^(.*)$ /index.php?request=$1 break; 

    include /etc/nginx/includes/php; 
} 

详见this document

选项2:

location /upload { 
    client_max_body_size 512M; 

    include /etc/nginx/includes/php; 

    fastcgi_param QUERY_STRING request=$uri&$query_string; 
    fastcgi_param SCRIPT_FILENAME $document_root/index.php; 
} 
+0

真棒!第一个似乎正在工作!我认为重定向会导致它进入一个循环,但我猜它何时会在包含它的fcgi_pass中终止它。 – DAB

+0

其实我回来了。我仍然有我的最大身体大小在PHP的位置。我打开了调试日志,看起来有'fastcgi_pass'的php include被忽略,并且重写开始再次测试所有条件。这里是调试日志:https://gist.github.com/macdabby/13832f4257a73f82c01c。我会尝试一些参数,但这似乎也没有工作。 – DAB