On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • Hi,

    I have 2 problems with rewrites for MODX Cloud:


    1. Rewrite static.mydomain.com to assets folder

    I want this subdomain to point to the /assets/ folder of my Cloud. I tried to set the root for this subdomain like this:
    location / {
    	
    	if ($host = 'static.chsmedien.com') {
    		root /paas/cxxxx/www/assets/;
    	}
    	
    	try_files $uri $uri/ @modx-rewrite;
    }

    ...but this is not working.


    2. Send retina images based on cookie

    Based on this http://shauninman.com/tmp/retina/, I'm setting a cookie in the browser and the webserver should send @2x images. But this doesn't work, too.
    location ~* \.(?:ico|gif|jpe?g|png)$ {
        expires 355d;
        
        # Serves static files in high resolution only if required
    	# Naming convention for high resolution images:
    	# [filename]@2x[extension], e.g.:
    	# example@2x.png
    	set $hidpi_uri $1@2x$2;
    	
    	if ($http_cookie !~ 'device-pixel-ratio=2') {
        	# If the device-pixel-ratio cookie is not set to 2, fall back to
        	# default behaviour, i.e. don't try to serve high resolution image
        	break;
        }
    	
    	# device-pixel-ratio cookie is set to 2
    	# Serve high resolution image if available,
    	# otherwise fall back to standard resolution
    	try_files $hidpi_uri $uri =404;
    
    }



    My complete rules:
    # x-ua-xompatible header
    add_header X-UA-Compatible IE=edge,chrome=1;
    
    
    
    # expire headers for assets
    # 1: images
    location ~* \.(?:ico|gif|jpe?g|png)$ {
        expires 355d;
        
        # Serves static files in high resolution only if required
    	# Naming convention for high resolution images:
    	# [filename]@2x[extension], e.g.:
    	# example@2x.png
    	set $hidpi_uri $1@2x$2;
    	
    	if ($http_cookie !~ 'device-pixel-ratio=2') {
        	# If the device-pixel-ratio cookie is not set to 2, fall back to
        	# default behaviour, i.e. don't try to serve high resolution image
        	break;
        }
    	
    	# device-pixel-ratio cookie is set to 2
    	# Serve high resolution image if available,
    	# otherwise fall back to standard resolution
    	try_files $hidpi_uri $uri =404;
    
    }
    # 2: js/css
    location ~* \.(?:js|css)$ {
    	expires 30d;
    }
    
    
    
    # Built-in filename-based cache busting
    # This will route all requests for /css/style.20120716.css to /css/style.css
    # Read also this: https://github.com/h5bp/html5-boilerplate/blob/master/doc/htaccess.md#cache-busting
    location ~* (.+)\.(\d+)\.(js|css)$ {
    	try_files $uri $1.$3;
    }
    
    
    
    
    
    
    # modx furls rewrite
    location / {
    	
    	if ($host = 'static.chsmedien.com') {
    		root /paas/c0073/www/assets/;
    		break;
    	}
    	
    	try_files $uri $uri/ @modx-rewrite;
    }
      chsmedien - Digital Marketing Agency
      MODX Professional & Ambassador

      http://chsmedien.com | http://twitter.com/christianseel
    • @christianseel - i'd love to know how to get these working too. Have you tried contacting cloud support? They're very responsive.
      • out of interest - have you tried adding "static.chsmedien.com" to your domains under that particular cloud?
        • Yep domain is added. Didn't ask support...
            chsmedien - Digital Marketing Agency
            MODX Professional & Ambassador

            http://chsmedien.com | http://twitter.com/christianseel
          • Hi Christian,

            I think I can help with the first problem.

            Your test to see if the $host is "static.chsmedien.com" is in the location / {} block. However, any requests for assets are probably matching your (?:ico|gif|jpe?g|png) or js|css location blocks. Only one location block is used for a request, unless the request is rewritten.

            I would suggest moving this conditional out of a location block altogether (i.e. to near the top of the rules), and doing a rewrite instead:

            if ($host = 'static.chsmedien.com') {
              rewrite ^ /assets$uri last;
            }
            


            Hope this helps.
              Mike Schell
              Lead Developer, MODX Cloud
              Email: mike@modx.com
              GitHub: https://github.com/netProphET/
              Twitter: @mkschell
            • Wouldn't hurt to add the following in your rules too:

              if ($host = "www.chsmedien.com") {
                rewrite ^ $scheme://chsmedien.com$uri permanent;
              }
              • Thanks netProphEt, that helped! I did not know that only one location block is used, that makes it quiet more complicated.
                Your suggestion works, as far as I can see. But the retina rewrite still doesn't work.

                My complete rules now:
                # x-ua-xompatible header
                add_header X-UA-Compatible IE=edge,chrome=1;
                
                
                if ($host = 'static.chsmedien.com') {
                  rewrite ^ /assets$uri last;
                }
                
                
                # expire headers for assets
                # 1: images
                location ~ ^(/images/[^\.]+)(\.(?:jpe?g|png|gif))$ {
                    expires 355d;
                    
                    # Serves static files in high resolution only if required
                	# Naming convention for high resolution images:
                	# [filename]@2x[extension], e.g.:
                	# example@2x.png
                	set $hidpi_uri $1@2x$2;
                
                # disable this for testing...	
                #	if ($http_cookie !~ 'device-pixel-ratio=2') {
                    	# If the device-pixel-ratio cookie is not set to 2, fall back to
                    	# default behaviour, i.e. don't try to serve high resolution image
                #    	break;
                #    }
                	
                	# device-pixel-ratio cookie is set to 2
                	# Serve high resolution image if available,
                	# otherwise fall back to standard resolution
                	try_files hidpi_uri $uri =404;
                
                }
                # 2: js/css
                location ~* \.(?:js|css)$ {
                	expires 30d;
                }
                
                
                
                # Built-in filename-based cache busting
                # This will route all requests for /css/style.20120716.css to /css/style.css
                # Read also this: https://github.com/h5bp/html5-boilerplate/blob/master/doc/htaccess.md#cache-busting
                location ~* (.+)\.(\d+)\.(js|css)$ {
                	try_files $uri $1.$3;
                }
                
                
                
                # rewrite blog/rss to feedburner
                rewrite  ^/blog/rss$  http://feeds.feedburner.com/chsmedien  permanent;
                
                
                
                # modx furls rewrite
                location / {
                	try_files $uri $uri/ @modx-rewrite;
                }
                  chsmedien - Digital Marketing Agency
                  MODX Professional & Ambassador

                  http://chsmedien.com | http://twitter.com/christianseel
                • Okay, I got also the 2nd working. The problem was the location block, the rewritten assets folder was missing.

                  # x-ua-xompatible header
                  add_header X-UA-Compatible IE=edge,chrome=1;
                  
                  
                  
                  if ($host = "static.chsmedien.com") {
                    rewrite ^ /assets$uri;
                  }
                  
                  
                  
                  location ~ ^(/assets/images/[^\.]+)(\.(?:jpe?g|png|gif))$ {
                      expires 365d;
                      
                      # Serves static files in high resolution only if required
                  	# Naming convention for high resolution images:
                  	# [filename]@2x[extension], e.g.:
                  	# example@2x.png
                  	set $hidpi_uri $1@2x$2;
                  
                  	if ($http_cookie !~ 'device-pixel-ratio=2') {
                      	# If the device-pixel-ratio cookie is not set to 2, fall back to
                      	# default behaviour, i.e. don't try to serve high resolution image
                      	break;
                      }
                  	
                  	# device-pixel-ratio cookie is set to 2
                  	# Serve high resolution image if available,
                  	# otherwise fall back to standard resolution
                  	try_files $hidpi_uri $uri =404;
                  
                  }
                  
                  
                  
                  location ~* \.(?:js|css)$ {
                  	expires 30d;
                  }
                  
                  
                  
                  # rewrite blog/rss to feedburner
                  rewrite  ^/blog/rss$  http://feeds.feedburner.com/chsmedien  permanent;
                  
                  
                  
                  # modx furls rewrite
                  location / {
                  	try_files $uri $uri/ @modx-rewrite;
                  }


                  The only thing that is missing is filename-based chaching rewrites:
                  # Built-in filename-based cache busting
                  # This will route all requests for /css/style.20120716.css to /css/style.css
                  # Read also this: https://github.com/h5bp/html5-boilerplate/blob/master/doc/htaccess.md#cache-busting
                  location ~* (.+)\.(\d+)\.(js|css)$ {
                  	try_files $uri $1.$3;
                  }

                  This would be again in confilct with the existing location block for css/js files. But I currently have no idea how to combine them.
                    chsmedien - Digital Marketing Agency
                    MODX Professional & Ambassador

                    http://chsmedien.com | http://twitter.com/christianseel