{"id":396,"date":"2018-06-27T19:08:30","date_gmt":"2018-06-27T11:08:30","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=396"},"modified":"2018-06-27T19:08:30","modified_gmt":"2018-06-27T11:08:30","slug":"configuring-nginx-and-php-7-stack-in-linux-to-increase-or-decrease-file-upload-size-limit","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/configuring-nginx-and-php-7-stack-in-linux-to-increase-or-decrease-file-upload-size-limit\/","title":{"rendered":"Configuring Nginx and PHP 7 stack in Linux to increase or decrease file upload size limit"},"content":{"rendered":"<p><a href=\"http:\/\/php.net\/\" rel=\"noopener\" target=\"_blank\">PHP<\/a> web applications can utilize Nginx as the <a href=\"https:\/\/www.techcoil.com\/blog\/the-reverse-proxy-server\/\" rel=\"noopener\" target=\"_blank\">reverse proxy server<\/a> and <a href=\"https:\/\/php-fpm.org\/\" rel=\"noopener\" target=\"_blank\">PHP FPM<\/a> as the upstream server. Whenever you encounter HTTP file upload issues, limitation in file upload size is one common cause. <\/p>\n<p>This post shows how to adjust the file upload size limit for your application running on a Nginx-PHP stack in Linux.<\/p>\n<h2>Two sets of file upload size limit configuration to apply for Nginx-PHP LEMP stacks<\/h2>\n<p>The following digram is an illustration of how the browser communicates with the application server in a typical <a href=\"https:\/\/www.techcoil.com\/glossary\/lemp\" rel=\"noopener\" target=\"_blank\">LEMP stack<\/a>.  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/client-to-reverse-proxy-server-to-upstream-server-communication.gif\" alt=\"client to reverse proxy server to upstream server communication\" \/><\/p>\n<p>As shown above, there are two machines that process HTTP requests received from HTTP clients. Therefore, we need to apply two sets of file upload size limit configuration - one for <a href=\"http:\/\/nginx.org\/\" rel=\"noopener\" target=\"_blank\">Nginx<\/a> and the other for the <a href=\"https:\/\/php-fpm.org\/\" rel=\"noopener\" target=\"_blank\">PHP-FPM server<\/a>.<\/p>\n<h2>Configuring file upload size limit for Nginx<\/h2>\n<p>Firstly, let's look at how we can configure file upload size limit for Nginx.<\/p>\n<h3>Locating the Nginx configuration file<\/h3>\n<p>A typical installation of Nginx associates the <code>nginx<\/code> binary with the PATH variable. Therefore, you can locate the Nginx configuration file by running the following command in your terminal program:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo nginx -t\r\n<\/pre>\n<p>When I run this on my <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-setup-codiad-web-ide-on-your-raspberry-pi-3-with-raspbian-stretch-lite-nginx-and-php\/\" rel=\"noopener\" target=\"_blank\">Raspberry Pi 3 running Codiad Web IDE<\/a>, I got the following output:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nnginx: the configuration file \/etc\/nginx\/nginx.conf syntax is ok\r\nnginx: configuration file \/etc\/nginx\/nginx.conf test is successful\r\n<\/pre>\n<p>As can be seen, <code>nginx<\/code> reported that it is taking configurations from <strong><code>\/etc\/nginx\/nginx.conf<\/code><\/strong>. Given that, we know where to look at for adjusting the file upload size limit for Nginx.<\/p>\n<h3>Configuring the file upload size limit for Nginx via <code>client_max_body_size<\/code> directive<\/h3>\n<p>Once you had figured out the location of the configuration file that you wish to edit, you can then proceed to add in the <a href=\"http:\/\/nginx.org\/en\/docs\/http\/ngx_http_core_module.html#client_max_body_size\" rel=\"noopener\" target=\"_blank\"><strong><code>client_max_body_size<\/code><\/strong><\/a> directive with a value that you desire. <\/p>\n<p>This directive defines the maximum allowed size of client request body. In addition, the default value is <strong>1 Megabyte<\/strong>. When we set a value of <strong><code>0<\/code><\/strong>, we disable the checking of the client request body size. <\/p>\n<p>Since a file is included as the client request body, we will adjust the file upload size via the <strong><code>client_max_body_size<\/code><\/strong> directive.<\/p>\n<h3>Universal file upload size limit for HTTP requests made to all upstream servers<\/h3>\n<p>In case you want to allow a file smaller than 8 Megabytes to get through your Nginx server for all upstream servers, you will apply the <code>client_max_body_size<\/code> directive inside the <a href=\"http:\/\/nginx.org\/en\/docs\/http\/ngx_http_core_module.html#http\" rel=\"noopener\" target=\"_blank\"><code>http<\/code><\/a> block:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nhttp {\r\n    # ...\r\n    client_max_body_size 8m;\r\n    # ...\r\n}\r\n<\/pre>\n<h3>Site specific file upload size limit<\/h3>\n<p>In case you want to apply another file upload size limit for HTTP request made to a specific upstream server, you need to look for the server block for that site. For example, the following configuration allows any file smaller than 1 Gigabytes to pass Nginx for my <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-host-your-own-file-sharing-website-on-raspberry-pi-3-with-raspbian-stretch-lite-nginx-projectsend-mariadb-and-php\/\" rel=\"noopener\" target=\"_blank\">Raspberry Pi 3 file sharing website<\/a>:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nserver {\r\n    \r\n    # ...     \r\n    listen 443;\r\n    server_name  ps.yourdomain.com;\r\n    client_max_body_size 1024m;\r\n    # ...\r\n\r\n} \r\n<\/pre>\n<h3>Uri specific file upload size limit<\/h3>\n<p>You can also apply the file upload size limit to a specific Uri. For example, the following configuration will apply a file upload size limit of 2 Gigabytes for HTTP requests sent to http:\/\/www.adomain.com\/upload:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nserver {\r\n    \r\n    # ...     \r\n    listen 80;\r\n    server_name  www.adomain.com;\r\n    \r\n    location \/upload {\r\n        # ...\r\n        client_max_body_size 2048m;\r\n        # ...\r\n    }\r\n\r\n    # ...\r\n\r\n} \r\n<\/pre>\n<h2>Configuring file upload size limit for PHP FPM 7<\/h2>\n<p>Lastly, let's look at how to configure file upload size limit for PHP FPM 7. <\/p>\n<h3>Locating the php.ini configuration file<\/h3>\n<p>Before you proceed to configure the file upload size limit for PHP FPM 7, you need to find the <strong><code>php.ini<\/code><\/strong> configuration file. In general, you can run the following command to look for <strong><code>php.ini<\/code><\/strong>:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo find \/ -name php.ini\r\n<\/pre>\n<p>After the command completes, you may find output similar to the following in your terminal screen:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n\/etc\/php\/7.0\/fpm\/php.ini\r\n\/etc\/php\/7.0\/cli\/php.ini\r\n<\/pre>\n<p>In this case, we will edit <strong><code>\/etc\/php\/7.0\/fpm\/php.ini<\/code><\/strong> to configure the file upload size limit for PHP <strong>FPM<\/strong> 7.<\/p>\n<h3>Configuring file upload size limit for PHP FPM 7 via <code>upload_max_filesize<\/code> and <code>post_max_size<\/code><\/h3>\n<p>In order to change file upload size limit for PHP, we need to change two settings - <strong><code>upload_max_filesize<\/code><\/strong> and <strong><code>post_max_size<\/code><\/strong>. <\/p>\n<p>The default value for <strong><code>upload_max_filesize<\/code><\/strong> is <strong>2 Megabytes<\/strong>. <\/p>\n<p>The default value for <strong><code>post_max_size<\/code><\/strong> is <strong>8 Megabytes<\/strong>. <\/p>\n<p>Note that the <strong><code>post_max_size<\/code><\/strong> has to be larger than <strong><code>upload_max_filesize<\/code><\/strong> for large files. <\/p>\n<p>In addition to those two variables, you may want to set <strong><code>max_input_time<\/code><\/strong> to a higher value for large files. In this case, your clients will be given more time to upload files to your PHP FPM 7 server.<\/p>\n<h3>Universal file upload size limit for HTTP requests made to all PHP applications served by PHP FPM 7<\/h3>\n<p>To apply a common file upload size limit for all PHP applications served by PHP FPM 7, you will edit the values for <strong><code>upload_max_filesize<\/code><\/strong> and <strong><code>post_max_size<\/code><\/strong> from <strong><code>\/etc\/php\/7.0\/fpm\/php.ini<\/code><\/strong>.<\/p>\n<p>For example, if we want to change the file upload size limit to 200 Megabytes, we will first open up <code>\/etc\/php\/7.0\/fpm\/php.ini<\/code>. After the editor loads the file, we will then change the lines with <strong><code>upload_max_filesize<\/code><\/strong> and <strong><code>post_max_size<\/code><\/strong> variables to the following:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nupload_max_filesize = 200M\r\npost_max_size = 202M\r\n<\/pre>\n<h3>Application specific file upload size limit<\/h3>\n<p>To adjust the file upload size limit for different PHP applications served by PHP FPM 7, we can do so via the <a href=\"http:\/\/php.net\/manual\/en\/function.ini-set.php\" rel=\"noopener\" target=\"_blank\">ini_set<\/a> function.<\/p>\n<p>For example, if we want to change the file upload size limit to 300 Megabytes, we will include the following PHP codes in a running script:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n@ini_set( 'upload_max_size' , '300M' );\r\n@ini_set( 'post_max_size', '302M');\r\n<\/pre>\n<h2>Restarting Nginx and PHP FPM 7 for the new file upload size limit to take effect<\/h2>\n<p>Once you had included the new file upload size limit for your Nginx server and PHP FPM server, restart them:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo systemctl restart php7.0-fpm.service\r\nsudo systemctl restart nginx.service\r\n<\/pre>\n<p>After your Nginx server and PHP FPM server had restarted, the new file upload size limit will take effect.<\/p>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-6o\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-6o&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-6o&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F396&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p><a href=\"http:\/\/php.net\/\" rel=\"noopener\" target=\"_blank\">PHP<\/a> web applications can utilize Nginx as the <a href=\"https:\/\/www.techcoil.com\/blog\/the-reverse-proxy-server\/\" rel=\"noopener\" target=\"_blank\">reverse proxy server<\/a> and <a href=\"https:\/\/php-fpm.org\/\" rel=\"noopener\" target=\"_blank\">PHP FPM<\/a> as the upstream server. Whenever you encounter HTTP file upload issues, limitation in file upload size is one common cause. <\/p>\n<p>This post shows how to adjust the file upload size limit for your application running on a Nginx-PHP stack in Linux.<\/p>\n","protected":false},"author":1,"featured_media":774,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[4],"tags":[166,23,29,225,13,444,195,438],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/client-to-reverse-proxy-server-to-upstream-server-communication.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-6o","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/396"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=396"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/396\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/774"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=396"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}