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

    I've got nginx set up and working with php5-fpm and modx.

    The only issue I'm having is with getting friendly urls to work and wondered if anyone could spot the issue with my config.

    server {
    root /usr/share/nginx/html;
    index index.php;

    error_log /usr/share/nginx/html/error.log notice;
    rewrite_log on;

    location / {
    try_files $uri $uri/ @modx;
    }

    location @modx {
    rewrite ^/(.*)$ /index.php?q=$1;
    }

    location ~ \.php(.*)$ {
    fastcgi_index index.php;
    fastcgi_pass php5-fpm-sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    }
    }

    The error log shows the following when I hit a friendly url,

    [notice] 4869#0: *1412 "^/(.*)$" matches "/modx-2.2.6-pl/another-page/", client: 89.145.217.142, server: , request: "GET /modx-2.2.6-pl/another-page/ HTTP/1.1", host: "ec2-54-246-50-76.eu-west-1.compute.amazonaws.com", referrer: "http://ec2-54-246-50-76.eu-west-1.compute.amazonaws.com/modx-2.2.6-pl/"

    [notice] 4869#0: *1412 rewritten data: "/index.php", args: "q=modx-2.2.6-pl/another-page/", client: 89.145.217.142, server: , request: "GET /modx-2.2.6-pl/another-page/ HTTP/1.1", host: "ec2-54-246-50-76.eu-west-1.compute.amazonaws.com", referrer: "http://ec2-54-246-50-76.eu-west-1.compute.amazonaws.com/modx-2.2.6-pl/"

    [error] 4869#0: *1412 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 89.145.217.142, server: , request: "GET /modx-2.2.6-pl/another-page/ HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.soc:", host: "ec2-54-246-50-76.eu-west-1.compute.amazonaws.com", referrer: "http://ec2-54-246-50-76.eu-west-1.compute.amazonaws.com/modx-2.2.6-pl/"

    I think it's something to do with SCRIPT_FILENAME but I can't work out what it is, I've tried setting it to $request_filename and that hasn't worked either

    Any help would be most appreciated
      • 1841
      • 141 Posts
      I am using Modx Revolution with Nginx and rewrites work fine for me, looking at your nginx config the only difference I can see for the re-write is:

      rewrite ^/(.*)$ /index.php?q=$1 last;
      


      I notice my location ~ \.php$ is a little different though, mine looks like this:

      location ~ \.php$ {
                      try_files $uri =404;
              fastcgi_split_path_info ^(.+\.php)(.*)$;
              fastcgi_pass    unix:/tmp/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;
      }


      Not sure if that's any help, it was a while ago since I set it up, if you still have issues let me know and I'll try and dust off the cobwebs from the old grey matter.
        • 40889
        • 2 Posts
        This is what's working for me (nginx on Debian 6, php5-fpm, modx 2.2.6-pl):

        server {
            server_name example.com;
            root /sites/modx/example;
            index index.php;
            client_max_body_size 30M;
            location / {
             try_files $uri $uri/ @modx;
            }
        	
        	location @modx {
        	rewrite ^/(.*)$ /index.php?q=$1 last;
        	}
         location ~* ^.+\.(jpg|jpeg|gif|css|png|js|ico|swf|flv|eot|ttf|woff|svg)$ {
               add_header Cache-Control "public";
               access_log        off;
               log_not_found     off;
               expires           30d;
        
        }
            location ~ /\.ht {
                deny  all;
            }
            location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(.*)$;
                fastcgi_pass   unix:/tmp/php-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;
            }
        
        }
        [ed. note: openconcept last edited this post 13 years, 10 months ago.]
          • 42284
          • 13 Posts
          Thanks for your replies.

          I managed to work out the issues was because the site was running in a folder called "modx-2.2.6-pl" but the root variable from the nginx config was missing this from the path, which caused the friendly URLs not to work as this was being include in the URL