简体   繁体   中英

Alternate max_post_size and upload_max_filesize for different subdomains and/or directories php.ini nginx fastcgi php-fpm

I have nginx and php5-fpm working wonderfully, and www.mysite.com , this.mysite.com , and that.mysite.com all go to different directories elegantly. I have been working on a site for uploading files. I'd like the maximum file size to be 10 GB. For this to work, I have to tell php.ini that max_post_size and upload_max_filesize are 10240 MB instead of the default 2 MB.

I am well aware of the security implications. I would therefore like those php.ini values of 10240 MB to apply ONLY to one or both of:

  • upload.mysite.com

and/or

  • www.mysite.com/upload

One option is to also install Apache to listen on a different port, do some redirect/rewrite magic, and have mod_php's php.ini file with the 10240 MB values handle only the uploads site. I'd prefer to not do that.

WITHOUT having a separate web server handle requests to my upload page, how can I accomplish this in a single instance of nginx?

Use client_max_body_size and set it to the desired value in your server blocks. Nginx will directly drop the handling of the request if the request body exceeds the size specified in this directive. Please note that you won't get any POST submitted in that case.

With php-fpm you can have several pools running, one for each website.

Then you can alter any php.ini setting inside the pool configuration (but be carefull, you cannot use MB or G shortcuts), using this syntax inside the pool:

; UPLOAD
php_admin_flag[file_uploads]         =1
php_admin_value[upload_tmp_dir]="/some/path/var/tmp"
;Maximum allowed size for uploaded files. 10240MB *1024 *1024 -> 10737418240
php_value[upload_max_filesize] ="10737418240"
php_admin_value[max_input_time]      = (...)
php_admin_value[post_max_size]  (...)

This is really near the available syntax for Apache virtualhosts. PHP configuration has never been stuck in php.ini files.

As you can see you can use php_value or php_admin_value , the big difference is that when using php_value the setting can later be altered by the application, using ini_set command. So you could use a low value for upload_max_filesize and alter it in the application only on the upload script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM