Website pages not opening other than homepage with nginx

Gurjyot Singh asked:

I have set up a website on nginx server block and it’s loading fine. But as soon as try to open any webpage other than homepage, it shows 404 Not Found message. Due to this none of the pages are viewable other than the homepage.

Here is sites-available file.

server {
        listen 80 ;
        listen [::]:80 ;

        root /var/www/;

        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html;


        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;

        # pass the PHP scripts to FastCGI server listening on

        location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;

        location ~ /\.ht {
        deny all;

Can you please tell what’s the issue that none of the pages is opening. I have also used nginx -t and everything goes successful and there are no errors in error.log file.

My answer:

Those paths do not exist as files in the filesystem. They are handled by your web application. But, you are not actually routing them there in try_files. You instead are routing them to a 404 page.

                try_files $uri $uri/ =404;

Instead, you should be routing requests which aren’t static files to your web application. For example:

                try_files $uri $uri/ /index.php$is_args$args;

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.