Question

Custom 404 not working in my nginx droplet, please help?

Posted October 25, 2019 701 views
Nginx

I’m am new in nginx and before I user apache web server. So I got to know that .htaccess does not works in nginx, after doing some research I got to know about nginx.conf. When I added the 404 code in http inside server tree and reloaded nothing happens and the default 404 page is returned.

error_page 404 /404.html;
location  /404.html {
  internal;
}

I even tried doing this in my /etc/nginx/sites-available/default conf, but still no luck, however Im confused where should I put the code, in sites default conf or in the global nginx conf.
Can some one please give me brief on this topic!
Appreciate your help

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

1 answer

Hi,

Not sure if you saw this tutorial during your research:

https://www.digitalocean.com/community/tutorials/how-to-configure-nginx-to-use-custom-error-pages-on-ubuntu-14-04

It’s a little older but I think it covers the main points, namely where to place the 404.html file itself and where in the config it goes (default), inside the server {} section and how to make the new config active.

If that doesn’t help, please paste more of your config and the location of your 404.html - good luck and let us know how it goes.

by Justin Ellingwood
Nginx is a high performance web server capable of serving content with flexibility and power. When designing your web pages, it is often helpful to customize every piece of content that your users will see. This includes error pages for when they request content that is not...
  • Ok, so it did worked. Thankyou. But there are many server blocks in that file port 40, port 443, some are managed by certbot. So my questions in whenever I need to make a change do I need to change in all that blocks. And the 404 worked fine, now I used .htaccess to nginx converter, it gave me this

            location /path/ {
            if (!-e $request_filename){
                rewrite ^(.*)$ /$1.php;
        }
        rewrite ^/([a-zA-Z0-9\-\_]+)$ /index.php?profile=$1;
        }
    

    But it is not working, I just pasted it in port 80 and port 443 server block, there are other blocked where it is written, Managed by certbot
    Appreciate your help.
    And one more question, whenever I need to make a change regarding url, like .htaccess, where do i make it
    1) In nginx global config?
    2) In /etc/nginx/sites-enabled/default?
    3) Or /etc/nginx/sites-available/default?

    Im confused, thankyou very much!!

Submit an Answer