Linux VPS 设置404页面,让访问路径不再访问
卡尔云官网
www.kaeryun.com
在Linux VPS(虚拟专用服务器)上设置404页面,是提升用户体验和SEO优化的重要步骤,当访问不应该访问的路径时,服务器返回404页面可以告知用户资源不存在,避免不必要的访问和潜在的安全风险,本文将详细指导您如何在Linux VPS上设置404页面。
检查服务器配置
在设置404页面之前,请确保服务器配置正确,404页面的设置需要通过Apache、Nginx或其他Web服务器配置完成,以下是一些常见配置检查步骤:
-
确认HTTP版本
确保服务器使用HTTP/1.0而不是HTTP/1.1。
指令:sudo service apache2 status
输出示例:State: Start Last mod: 12:34:56 ago Version: 2.4.48
-
确认配置文件路径
Apache或Nginx的配置文件路径通常是/etc/apache2/
或/etc/nginx/sites-enabled/
。
指令:sudo cat /etc/apache2/conf/httpd.conf
输出示例:<Location /> Error_log off LogLevel debug <Directory /> <Options -MultiViews> </Options> <Error 500> <View /500.html> <Error_log 500.log> <Directory 500> <Delete> <CgiBin cgi> <CgiDir /usr/bin/python3> </CgiDir> <CgiResponse 200> <Location /500.html> </CgiResponse> </Error> </Error 500> <Directory /var/www/html> <Deny from=> <AllowOverride Yes> <Location /var/www/html/500.html> <Error 500> <View 500.html> <Error_log 500.log> <Directory 500> <Delete> <CgiBin cgi> <CgiDir /usr/bin/python3> </CgiDir> <CgiResponse 200> <Location /500.html> </CgiResponse> </Error> <HTMLError 500> <Title 500页面> <Description 500页面> <Redirect 500.html> </HTMLError> </Location> <Index index.html> </Directory> </Location>
-
确认SSL配置
如果服务器使用SSL证书,需要确保404页面在HTTPS下正常工作。
指令:sudo ls /etc/ssl/certs/*
输出示例:/etc/ssl/certs/sslcerts.1 /etc/ssl/certs/sslcerts.2
修改Apache配置文件
在Apache中,可以通过配置error_log
和location
来设置404页面。
-
修改
error_log
配置
指令:sudo nano /etc/apache2/error_log
输入以下内容:Error_log off LogLevel debug <Error 500> <View /500.html> <Error_log 500.log> <Directory 500> <Delete> <CgiBin cgi> <CgiDir /usr/bin/python3> </CgiDir> <CgiResponse 200> <Location /500.html> </CgiResponse> </Error> <HTMLError 500> <Title 500页面> <Description 500页面> <Redirect 500.html> </HTMLError>
-
修改
location
配置
指令:sudo nano /etc/apache2/conf/httpd.conf
在<Directory /var/www/html>
下添加以下内容:<Directory /var/www/html> <Deny from=> <AllowOverride Yes> <Location /var/www/html/500.html> <Error 500> <View 500.html> <Error_log 500.log> <Directory 500> <Delete> <CgiBin cgi> <CgiDir /usr/bin/python3> </CgiDir> <CgiResponse 200> <Location /500.html> </CgiResponse> </Error> <HTMLError 500> <Title 500页面> <Description 500页面> <Redirect 500.html> </HTMLError> </Location> <Index index.html> </Directory>
-
重启Apache服务
指令:sudo systemctl restart apache2
设置静态404页面
如果服务器使用Nginx,可以更灵活地设置静态404页面。
-
修改Nginx配置文件
指令:sudo nano /etc/nginx/sites-enabled/default
在server_root
部分添加以下内容:server_root /var/www/html; include /etc/nginx/ssl.conf; root /var/www/html; error_log off; error_log_file error.log; error_log_size 10M; error_log_max 100; error_log retention 14days; error_log keepalive off;
-
添加404页面配置
指令:sudo nano /etc/nginx/sites-enabled/myvps.nginx
添加以下内容:server { listen 80; server_name myvps.com; index index.html; location / { error_log off; root /var/www/html; try_files $uri $uri/ /500.html; } location /500.html { error_log on; error_log_file 500.log; error_log_size 10M; error_log_max 100; error_log retention 14days; error_log keepalive off; HTMLError 500 <Title 500页面> <Description 500页面> <Redirect 500.html> </HTMLError> } }
-
重启Nginx服务
指令:sudo systemctl restart nginx
测试404页面
-
打开浏览器,访问错误路径(如
http://myvps.com/404
)
预期结果:显示404页面,包含<Title>
、<Description>
和<Redirect>
-
检查日志文件
- Apache日志:
/var/log/apache2/error.log
- Nginx日志:
/var/log/nginx/myvps.log
验证是否有404错误记录。
- Apache日志:
-
检查HTML文件
创建一个html
文件,放置在/var/www/html/500/
目录下。 示例:<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>404页面</title> </head> <body> <h1>404 - 未找到页面</h1> <p>对不起,页面不存在。</p> <a href="/500.html">前往500页面</a> </body> </html>
注意事项
-
确保权限正确
确保html
文件和/var/www/html/500/
目录有正确的权限,允许普通用户访问。 -
测试HTTPS
如果服务器使用HTTPS,需要确保404页面在HTTPS下正常工作。 -
监控性能
大量访问404页面可能会增加服务器负担,建议设置合理的访问限制和缓存策略。
通过以上步骤,您可以在Linux VPS上轻松设置404页面,提升用户体验和SEO表现。
卡尔云官网
www.kaeryun.com