4

I am trying to set up my first website with nginx and I cannot seem to make extensionless php pages to work. I would like my pages like /aboutme.php to appear as /aboutme or I would like to just change my URLs to /aboutme instead of /aboutme.php and make those work. I managed to make the last scenario work on CPanel hosting by editing .htaccess like this:

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L, QSA]

However, if I convert .htaccess code to nginx using online tools, it doesn't work. I also tried to edit my nginx.conf with try_files and extensionless-php with no success, I get 404. My plain nginx.conf without try_files and extensionless-php looks like this:

server {
listen   localhost:8081; 
server_name mywebsite.com; # I use my website here
root /usr/share/nginx/web/website1;
location / {
        root   /usr/share/nginx/web/website1;
        index  index.php index.html index.htm;
  }
location ~ \.php$ {
      root           /usr/share/nginx/web/website1;
      fastcgi_pass   127.0.0.1:9000;
          fastcgi_index  index.php;
      fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
      include        fastcgi_params;
  }
} 

Edit: my question got marked as a duplicate, so I tried to configure it as it was said with no success, if I visit '/aboutme' I get '404 not found', if I visit '/aboutme.php', it's opened as '/aboutme.php' with '.php':

server {
listen       localhost:8081;
server_name  mywebsite.com;
root         /usr/share/nginx/web/website1;

location / {
    try_files $uri $uri/ @extensionless-php;
    index index.html index.htm index.php;
}

location ~ \.php$ {
    try_files $uri =404;
    include       /etc/nginx/fastcgi_params;
    fastcgi_pass  127.0.0.1:9000;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

location @extensionless-php {
    rewrite ^(.*)$ $1.php last;
}
}
Carol
  • 41
  • 1
  • 3
  • That config looks right. What's in the nginx error log? – Michael Hampton Mar 14 '18 at 18:28
  • I tried the solution with no success, edited my question. – Carol Mar 14 '18 at 18:28
  • 2018/03/14 14:39:32 [error] 7714#7714: *27 open() "/usr/share/nginx/web/website1/aboutme" failed (2: No such file or directory), client: 127.0.0.1, server: mywebsite.com, request: "GET /about HTTP/1.1", host: "mywebsite.com", referrer: "http://mywebsite.com/" there is aboutme.php present in directory, nginx version 1.10.3 – Carol Mar 14 '18 at 18:45
  • It looks like the rewrite didn't take place. Did you restart nginx? – Michael Hampton Mar 14 '18 at 18:46
  • Yes, I tried restarting nginx multiple times, I also tried restarting my VPS multiple times. – Carol Mar 14 '18 at 18:51
  • That's a really odd place to keep your web pages (/usr/share/nginx/web). If they really are there, has the user that runs nginx got permissions to read them? – Onyx Mar 14 '18 at 20:32
  • I tried changing the location, running as root without succes. – Carol Mar 14 '18 at 21:07

1 Answers1

0

You could try using the following:

location / {
    try_files $uri $uri.php $uri/ =404;
    index index.html index.htm index.php;
}

You should also check the directory owner / access permissions, and see if the user nginx and php-fpm is running as (defined in nginx config) can access those directories.

Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63
  • It doesn't work in `try_files` as nginx will just serve the PHP source. – Michael Hampton Mar 15 '18 at 22:35
  • Interesting, how is this setup different from the front-controller pattern? Here, the front-controller is `$uri.php`, does the fact that there is variable expansion cause nginx not to process the file with the PHP `location` block? – Tero Kilkanen Mar 16 '18 at 07:59
  • Apparently so. The formerly linked duplicate contained [some discussion](https://serverfault.com/a/758183/126632) of this. – Michael Hampton Mar 16 '18 at 13:28