2016-03-26 35 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