nginx config for MODX CMS

Jul 17, 2019

Someone asked in the MODX Community Slack today about nginx configuration for MODX. The examples here and here are perfectly valid but the following may be preferable.

# Redirect http to https
server {
    listen 80;

    return 301$request_uri;
# Server on port 443
server {
    # https (because) and http2 (faster)
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    # SSL config
    include /etc/nginx/example-ssl.conf;
    # web root
    root /home/example/public;      
    index index.php index.html index.htm;
    # the MODX part
    location @modx-rewrite {
        rewrite ^/(.*)$ /index.php?q=$1&$args last;
    # instead of "if"
    location / {
        try_files $uri $uri/ @modx-rewrite;
    location ~ \.php$ {
        try_files $uri =404;
        # you can roll this way
        # or that way with a socket
        ##fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;


The above assumes you have a config file somewhere like /etc/nginx/nginx.conf that includes the above for each virtual host, and contains global configs for ssl, gzip, logging, headers, client configuration and other general directives. Also /etc/nginx/example-ssl.conf in the example defines the location of the SSL certificate files and other SSL configurations.