php - How to customize a 404 error page (ubuntu nginx) -
i in process of setting own website, , 1 of things have run ugly 404 error page. running lemp stack on ubuntu, default error page nginx page. needless say, not how default error pages look, , change it. have seen various methods of how this, lot of them deal apache (which may same), .htaccess, etc.
however, sure there must easier way this. isn't 404 page simple .html page should able edit? how can edit default 404 page in easiest way possible without using else's?
the answer yes, there easy way customize 404 page html file, @ least on lemp stack. assuming have not re-organized "too many" files, thing needs done create single file.
explanation:
if go /etc/nginx/sites-available/default.conf
, open it, notice block looks similar this:
server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /usr/share/nginx/html; index index.php index.html index.htm; # make site accessible http://localhost/ server_name localhost unaviamedia.ca; location / { # first attempt serve request file, # directory, fall displaying 404. try_files $uri $uri/ =404; # uncomment enable naxsi on location # include /etc/nginx/naxsi.rules } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; } ....... }
disclaimer:
can see line: root /usr/share/nginx/html;
, home directory "/usr/share/nginx/html
." if yours different, need change next file path mention.
notice line: error_page 404 /404.html;
, midway down page? path 404 html error page site.
explanation:
error_page
- specifies line pertains http error pages.404
- specific http error code page respond to./404.html
- "root-relative" path file, file name included.- if root different 1 above, need change file path reflect difference (as mentioned above).
however, if go path above, notice there no file name in directory. therefore, default nginx error page display.
to change this, merely make custom html page 404 error page, , save in root directory "404.html." then, above path (error_page 404 /404.html;
) able display custom page when 404 error occurs.
i'm add necessary "error_page" lines whatever other http error codes may wish cover for, or route several same generic error page (but have not tried this). also, i'm sure setup same other server types, again, have not tried this.
please let me know if incorrect (or done better), or if lead accidental problems farther down road.
Comments
Post a Comment