URL rewriting is not properly configured on your server.

April 19, 2016 615 views
Nginx

i am not be able to run cakephp in our server.
os-centos 7, NGINX
can any one help me how do URL rewriting in our server

1 Answer

Hi! Can you post your nginx server block config? You will need to add something like this in order to enable URL rewriting in nginx:

server {
    ...

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    ...
}

You can always refer to CakePHP's documentation for more information on how to set up URL Rewriting and example configs.

  • Hi kamaln7,
    thanks for your reply. i try this cod but not working. below is my .conf file

    server {
        listen  80;
    
        server_name newsfeedapps.com www.newsfeedapps.com;
    
        location / {
            try_files $uri $uri/ /index.php?$args;
        }
    
        error_page  500 502 503 504  /50x.html;
         location ~ \.php$ {
                root           /usr/share/nginx/html;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;
            }
    }
    

    please have look below url https://newsfeedapps.com/cake/

    edited by kamaln7
    • You need to move root /usr/share/nginx/html; outside of the location block so that it applies to all URLs, not just ones ending with .php. I'd also recommend adding try_files $uri =404; as a security measure in order to prevent people from trying to execute non-php files (e.g. user uploads).

      server {
          listen  80;
          server_name newsfeedapps.com www.newsfeedapps.com;
      
          root           /usr/share/nginx/html;
      
          location / {
              try_files $uri $uri/ /index.php?$args;
          }
      
          error_page  500 502 503 504  /50x.html;
          location ~ \.php$ {
                  try_files $uri =404;
                  fastcgi_pass   127.0.0.1:9000;
                  fastcgi_index  index.php;
                  fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                  include        fastcgi_params;
              }
      }
      

      Make sure you restart nginx so that the changes take effect:

      sudo service nginx restart
      
Have another answer? Share your knowledge.