2

I'm setting up a Magento store with nginx, and the store is working great. However, now I would like to set a higher client_max_body_size value (let's say 100m), but only for the admin section.

I've already searched online, but I can't figure out how to get this to work. I'm probably misunderstanding the location blocks in this scenario, so maybe you can help me further.

I have the following server block:

server {
    listen                              80;
    server_name                         {domain};
    root                                {root};

    location / {
        index                           index.html index.php;
        try_files                       $uri $uri/ @handler;
        expires                         max;
    }

    ## These locations should be protected
    location ^~ /app/                   { deny all; }
    location ^~ /includes/              { deny all; }
    location ^~ /lib/                   { deny all; }
    location ^~ /media/downloadable/    { deny all; }
    location ^~ /pkginfo/               { deny all; }
    location ^~ /report/config.xml      { deny all; }
    location ^~ /var/                   { deny all; }

    location  /. {
        return                          404;
    }

    location /api {
        rewrite     ^/api/rest          /api.php?type=rest last;
    }

    location @handler {
        rewrite     /                   /index.php;
    }

    location ~ .php/ {
        rewrite     ^(.*.php)/          $1 last;
    }

    location ~ .php$ {
        if (!-e $request_filename) {
            rewrite /                   /index.php last;
        }

        expires                         off;

        fastcgi_pass                    hhvm;

        proxy_read_timeout              300s;
        proxy_connect_timeout           300s;
        fastcgi_read_timeout            300s;

        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param   MAGE_RUN_CODE   $mage_code;
        fastcgi_param   MAGE_RUN_TYPE   $mage_type;

        include                         fastcgi_params;
    }
}

Now let's say the admin is on http://domain.com/index.php/admin_section/. So I must apply a higher client_max_body_size within a location block location /index.php/admin_section/ { ... }. But when I do so, that rule is being ignored by the last location ~ .php$ { ... } block.

I know I can adjust some config rules within a if ($request_uri ~ /admin_section/) { ... } statement, but nginx won't accept the client_max_body_size directive within an if statement.

Then I tried to add a location block before and within the ~ .php$ location block and before any other location block. I also tried to copy the content of ~ .php$ block to the /index.php/admin_section/ block and put it before and after the ~ .php$ block, but nothing seems to work.

How do I get this to work?

redelschaap
  • 235
  • 1
  • 3
  • 14

1 Answers1

1

The following block may work. It replicates the PHP location block (which is the usual final destination for your admin URI).

location ^~ /index.php/admin_section/ {

    client_max_body_size            100m;

    expires                         off;
    fastcgi_pass                    hhvm;

    proxy_read_timeout              300s;
    proxy_connect_timeout           300s;
    fastcgi_read_timeout            300s;

    include                         fastcgi_params;
    fastcgi_param   SCRIPT_FILENAME $document_root/index.php;
    fastcgi_param   MAGE_RUN_CODE   $mage_code;
    fastcgi_param   MAGE_RUN_TYPE   $mage_type;
}

I cannot test it, but hopefully there are no side-effects to break Magento.

Richard Smith
  • 12,834
  • 2
  • 21
  • 29
  • With this I get a white screen with a `RequestInitDocument Not Found` message, with a 404 status code. Can you explain what the `^` is doing there? – redelschaap Jan 26 '16 at 11:05
  • I left the `;` off the end of the SCRIPT_FILENAME line, does that help? – Richard Smith Jan 26 '16 at 11:12
  • Nope, I already noticed that. Doesn't work with `$fastcgi_script_name` or the full path to index.php (/var/www/path/to/index.php) either. – redelschaap Jan 26 '16 at 11:14
  • Can you provide the contents of `fastcgi_params`? There is probably another header that needs overriding. – Richard Smith Jan 26 '16 at 11:18
  • You're right, `SCRIPT_FILENAME` was being overwritten in the `fastcgi_params` file. Moved the include above the `fastcgi_param` lines. It's working now, but I'm 100% sure I already tried this with `location ~ /` instead of `location ^~ /`. Can you tell me what the `^` is doing there, and how it differs from leaving it out? – redelschaap Jan 26 '16 at 11:22
  • I have updated the answer. `^~` is totally different to `~`. It marks a prefix location that should take precedence over all regex blocks. See [this document](http://nginx.org/en/docs/http/ngx_http_core_module.html#location). – Richard Smith Jan 26 '16 at 11:26
  • Ok, thanks for the info, didn't know that! But I thought it would automatically take precedence because `/index.php/admin_section/` is more specific / matches better than `.php`, but I guess location blocks are more complicated than this... – redelschaap Jan 26 '16 at 11:32