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?

2 Answers
2

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.

Leave a Reply

Your email address will not be published. Required fields are marked *