Question

MODX friendly URLs on Nginx

Has anyone deployed a MODX app using Nginx? I’m using a LEMP stack on Ubuntu and I would like to be able to utilise MODX’s friendly urls.

Having more experience with Apache, I’m unsure of what re-write rules Nginx requires in the configuration file.

Thanks


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Never mind, don’t use that one. In an nginx environment its better not to use an if statement, so try this one:

server {
    listen 80;
    server_name www.modxsite.com modxsite.com;
    root /path/to/www.modxsite.com;
    index index.php index.html;
 
    location / {
        #try to get file directly, try it as a directory or fall back to modx
        try_files $uri $uri/ @modx;
    }
 
    location @modx {
        #including ? in second rewrite argument causes nginx to drop GET params, so append them again
        rewrite ^/(.*)$ /index.php?q=$1&$args;
    }
 
    location ~ \.php(.*)$ {
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

Please give a report if you got any luck with this config

I haven’t used MODx before but nginx is pretty smooth to use. You probably need something like this:

server {
        listen 80;
        server_name modxsite.com
        root /home/sites/modxsite.com;
        index index.php;
        client_max_body_size 30M;
        location / {
                root /home/sites/modxsite.com;
                if (!-e $request_filename) {
                        rewrite ^/(.*)$ /index.php?q=$1 last;
                }
        }
        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(.*)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include fastcgi_params;
                fastcgi_ignore_client_abort on;
                fastcgi_param  SERVER_NAME $http_host;
        }
 
        location ~ /\.ht {
                deny  all;
        }
}

Try to use that, and please give a report when done. Hope this will work for you.

You answered just as I was posting my first response.

Your second solution (omiting the if statement also works).

server {
        listen 80;
        server_name site.co.uk;
        root /home/path/to/www;
        index index.php;
        

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;


        charset utf-8;
        
        location / {
            #try to get file directly, try it as a directory or fall back to modx
            try_files $uri $uri/ @modx;
        }
    
        location @modx {
            #including ? in second rewrite argument causes nginx to drop GET params, so append them again
            rewrite ^/(.*)$ /index.php?q=$1&$args;
        }
    
        location ~ \.php(.*)$ {
            include fastcgi_params;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }
        
        location = /favicon.ico { access_log off; log_not_found off; }
        location = /robots.txt  { access_log off; log_not_found off; }

        access_log off;
        error_log  /var/log/nginx/site.co.uk-error.log error;

        error_page 404 /index.php;
        

        location ~ /\.ht {
                deny  all;
        }
}