We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34109
    • 119 Posts
    Hi,

    I'm planning on using statcache for a site with multiple contexts. I've changed the plugin to write each context's cache to a different folder, but I'm struggling with the NGINX rule for the redirect. Each context can have more than one domain associated with it and there are 100+ contexts just to add to the complication

    The standard NGINX rule is:

    location / {
        try_files /statcache$uri~index.html /statcache$uri $uri $uri/ @modx-rewrite;
    }
    location @modx-rewrite {
        rewrite ^/(.*)$ /index.php?q=$1 last;
    }


    I need to somehow set up a rule for each site which is going to be cached and check in that context's cache folder.

    Any ideas?

    Cheers,

    Chris
      Studio Republic
      http://www.studiorepublic.com
      0845 226 3205
      @christodhunter
      • 22303 MODX Staff
      • 10,725 Posts
      Chris:

      Can you explain how you are routing requests to your various Contexts? Are these all coming in via one virtual host?
        • 34109
        • 119 Posts
        Hey Jason,

        Thanks for the speedy reply! Currently there's one server definition, with a wildcard for one domain (most sites are subdomains) and a bunch of other domains added in the list. Here's the current rules with the domains blanked out.

        server {
            listen *.XXX.com:80;
        
            server_name XXXX;
            root /var/www/html/XXXX/modx;
            index index.php;
            error_log XXX warn;
            access_log XXX main;
            client_max_body_size 30M;
        
            location / {
            try_files /core/cache/statcache/web/$uri~index.html /core/cache/statcache/web/$uri $uri $uri/ @modx-rewrite;
            }
        
            location @modx-rewrite {
            rewrite ^/(.*)$ /index.php?q=$1 last;
            }
        
            location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.socket.default;
                fastcgi_index index.php;
                #fastcgi_read_timeout 600s;
                include fastcgi_params;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                        fastcgi_ignore_client_abort     on;
                        fastcgi_param  SERVER_NAME $http_host;
            }
            location ~ /\.ht {
                deny all;
            }
        }
        
          Studio Republic
          http://www.studiorepublic.com
          0845 226 3205
          @christodhunter
          • 34109
          • 119 Posts
          In the location rule,
          /core/cache/statcache/web/$uri~index.html
          web is the default context. This is the bit I'd obviously need to change on each iteration of the rule.
            Studio Republic
            http://www.studiorepublic.com
            0845 226 3205
            @christodhunter
            • 22303 MODX Staff
            • 10,725 Posts
            I'm not sure this is possible without some kind of map available to the nginx config relating each context to the domains that are to be routed to it. Let me try to look into it when I get a chance and see if there might be a sane way to handle the mapping...
              • 34109
              • 119 Posts
              Thanks Jason, really appreciate it. It's unlikely that all the contexts will need to be routed through statcache, so could multiple server {} sections be set up, one for each context, with a generic, non-cached version following? Can you even have multiple servers with the same root folder?

              Can you guess I haven't worked with NGINX before?!
                Studio Republic
                http://www.studiorepublic.com
                0845 226 3205
                @christodhunter
                • 22303 MODX Staff
                • 10,725 Posts
                The server_name values for each server block is what is going to handle the domain routing.

                You could create one server block for each Context and create a custom index.php that initializes the proper context, with the appropriate statcache rules inside of each. But you would need a different root directory for each Context.

                I think you are simply going to have to create a map of variables in the nginx conf to be able to get a $context_name variable which can then be used in the rewrite rule.
                  • 34109
                  • 119 Posts
                  I think I'm going to go with a bit of a hybrid solution, using the context's http_host setting to determine the folder name for its cache and a few different server rules for each type of domain being hosted. It would be much easier if they all followed the same format, but nothing's ever that easy!

                  Thanks again for your help Jason - feeling the MODX love today!
                    Studio Republic
                    http://www.studiorepublic.com
                    0845 226 3205
                    @christodhunter
                    • 22303 MODX Staff
                    • 10,725 Posts
                    No problem Chris. Let me know if you have some suggestions or changes for statcache to help handle these scenarios.