3

I'm moving a site from an Apache server to a nginx server. A lot of the pages have a .php extension at the end of the permalink. Trying to view those pages results in a nginx 404 Not Found. However those pages worked fine on Apache. Here is the server block config for the site:

# Vhost Config: example.com

server {

  root /var/www/vhosts/sites/www.example.com/web;
  index index.php index.html index.htm;

  server_name example.com www.example.com;

  port_in_redirect off;

  location /Denied {
    return 403;
  }

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

  location ~* ^(/sitemap).*(\.xml)$ {
    rewrite ^ /index.php;
  }

  error_page 404 /404.html;
  error_page 500 502 503 504 /50x.html;

  location = /50x.html {
    root /usr/share/nginx/html;
  }
  location = /404.html {
    root /usr/share/nginx/html;
  }

  location ~ ^/.*\.php$ {
    try_files $uri = 404;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
  }

  location ~* \.(js|css|png|jpg|jpeg|gif|ico|xml)(\?ver=[0-9.]+)?$ {
    expires 1w;
    log_not_found off;
  }

  location ~ /\. {
    deny all;
  }

  location = /ping.html
  {
    access_log off;
  }

}

I commented out "#try_files $uri = 404;" to see if that would work however that resulted in a "File not found." message.

So the main problem is trying to get WordPress and nginx to see the permalink with a .php extension before it tries to run a .php file which it sees is not a real file.

Is this possible?

1

I ended up changing

location ~ ^/.*\.php$ {
    try_files $uri = 404;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}

to

location ~ ^/.*\.php$ {
    try_files $uri $uri/ /index.php?$args;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}

It now works, but I feel that is a security issue now.

0

This sounds like a path info issue.

http://wiki.nginx.org/PHPFcgiExample

    location ~ [^/]\.php(/|$) {
            fastcgi_split_path_info ^(.+?\.php)(/.*)$;
            if (!-f $document_root$fastcgi_script_name) {
                    return 404;
            }

            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi_params;
    }

This may get you going in the right direction, I hope.

  • Thanks for the response. I tried the example (using fastcgi_pass unix:/var/run/php5-fpm.sock; instead) and the same behavior occurred. – jpcadillac Jun 8 '15 at 20:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.