Magento – magento with nginx not found page show as defualt one with one magento html

magento2magento2.2nginx

when i type any invlaid url it shows like below

enter image description here

instead i want to show my website page

current setting of nginx.conf (in magento root folder)

root $MAGE_ROOT/pub;
index index.php;
autoindex off;
charset UTF-8;
error_page 404 403 = /errors/404.php;
#add_header "X-UA-Compatible" "IE=Edge";

i tried changing it and

root $MAGE_ROOT/pub;
index index.php;
autoindex off;
charset UTF-8;
error_page 404 403 = https://example.com/no-route;
#add_header "X-UA-Compatible" "IE=Edge";

then it directed me to that url , i do not want redirect it show show not found message on that url only please help me with what

Magento code base : /usr/share/nginx/html/m1

Server Nginx Setting

server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  localhost;
        # root         /usr/share/nginx/html/m1;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        set $MAGE_ROOT /usr/share/nginx/html/m1;
        include /usr/share/nginx/html/m1/nginx.conf;

        #location / {
        #}

        # redirect server error pages to the static page /40x.html
        #
        error_page 404 /404.html;
            location = /40x.html {
        }

        # redirect server error pages to the static page /50x.html
        #
        #error_page 500 502 503 504 /50x.html;
        #    location = /50x.html {
        #}


        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
        }


        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

Magento folder nginx.conf

index index.php;
autoindex off;
charset UTF-8;
error_page 404 403 = /errors/404.php;
#add_header "X-UA-Compatible" "IE=Edge";

Best Answer

For Magento2 You can use below config in your main nginx configuration file

upstream fastcgi_backend {
  # here use your php fpm sock file path
  server   unix:/var/run/php/php7.1-fpm.sock;
}

server {

  listen 80;
  server_name www.domain.com;
  set $MAGE_ROOT /magento-root-directory-path;
  include /magento-root-directory-path/nginx.conf.sample;
}

For Magento1.x You can use below config in your main nginx configuration file
https://gist.github.com/hanhpv/3cf5192ef05d10b512e84d76fb905fac

server { 
    listen 80; 
    root /magento-root-directory-path;
    index index.php index.html index.htm;

    server_name domain; 
    access_log /usr/local/etc/nginx/logs/access.log;
    access_log /usr/local/etc/nginx/logs/error.log;

    ## These locations would be hidden by .htaccess normally 
    location ^~ /app/                { deny all; } 
    location ^~ /includes/           { deny all; } 
    location ^~ /lib/                { deny all; } 
    location ^~ /media/downloadable/ { deny all; } 
    location ^~ /pkginfo/            { deny all; } 
    location ^~ /report/config.xml   { deny all; } 
    location ^~ /var/                { deny all; } 
    location /var/export/            { deny all; } 

    # deny htaccess files 
    location ~ /\. { 
        deny  all; 
        access_log off; 
        log_not_found off; 
    } 

    location ~*  \.(jpg|jpeg|png|gif|ico)$ { 
        expires 365d; 
        log_not_found off; 
        access_log off; 
    } 

    location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler 
        rewrite ^(.*.php)/ $1 last; 
    }

    ## rewrite anything else to index.php
    location / { 
        index index.html index.php;
        try_files $uri $uri/ /index.php?$query_string;
        expires 30d;
        rewrite /api/rest /api.php?type=rest;
    }

    # pass the PHP scripts to FPM socket 
    location ~ \.php$ { 
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param MAGE_RUN_TYPE store;
        fastcgi_param  MAGE_RUN_CODE $MAGE_RUN_CODE;
        fastcgi_index index.php; 
        include fastcgi.conf; 
    }
}