详细介绍如何使用Nginx进行异常处理 - conf
error_page
安装Nginx
首先,确保已经安装了Nginx。可以通过以下命令在Ubuntu系统中安装Nginx:
sudo apt-get update
sudo apt-get install nginx
创建配置文件
/etc/nginx/conf.d/default.conf
http {
server {
listen 80;
server_name example.com;
location / {
root /var/www/html;
index index.html;
}
}
}
添加错误处理模块
/etc/nginx/conf.d/default.conf
error_page 404 = @fallback;
error_page 500 502 503 = @slow;
定义404和500错误页面
接下来,定义两个错误页面,分别处理404(未找到)和500(内部服务器错误)情况。例如:
error_page 404 /404.html;
error_page 500 502 503 /500.html;
location = /404.html {
alias /path/to/404.html; 拼多多帮助力入口
}
location = /500.html {
alias /path/to/500.html;
}
重写默认错误页面
/etc/nginx/conf.d/default.conf
server {
listen 80;
server_name example.com;
location / {
root /var/www/html;
index index.html;
}
error_page 404 /404.html;
error_page 500 502 503 /500.html;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
重启Nginx服务
保存配置文件后,重启Nginx服务以使更改生效。在终端中执行以下命令:
sudo service nginx restart
example.com