Nginx错误页面优雅显示

生产环境中常见的http状态码列表:

200 服务成功返回页面,这是成功的http请求

301 永久跳转permanent

302 临时跳转 redirect

403 禁止访问,这个请求是合法的,但是服务器端因为事先根据规则而拒绝了请求,一般是服务器权限设置不正确。

404 服务器找不到客户端所请求的页面

500 服务器内部错误

502 坏的网关,一般为代理服务器出现问题

503 服务器当前不可用,可能因为服务器超载或者是维护所致

504 网关超时

优雅显示

我们可以将403.404页面重定向到网站首页或者指定的页面中,优化用户体验

示范:

server {

listen 80;

server_name www.titi.com;

location / {

root html/www;

index index.html index.htm;

}

error_page 404 /404.html; <<当出现403页面时,会跳转到403.html 页面中

access_log /app/logs/bbs_access.log main ;

}

上面的 /403,html 页面位于 html/www下的403.html页面

然后客户端进行测试

[root@localhost ~]# curl www.etiantian.org/fjh.html

this is 404 page

记住:代码中的 /404.html 是相当于你设定的虚拟主机制定的目录下

 

示范2:将50x.html单独防到指定的网站资源中

 

server {

listen 80;

server_name www.etiantian.org;

location / {

root /www/html;

index index.html index.php index.htm ;

# auth_basic "pless put passwd";

# auth_basic_user_file htpasswd;

access_log logs/fangwen.log main; }

error_page 404 /404.html;

error_page 502 501 504 503 /50x.html;

location = /50x.html { <<制定到/error 目录下

root /error;

}

 

示范3:改变状态码为新的状态码,并显示指定页面

error_page 404 = 200 /empty.gif;

server {

listen 80;

server_name www.banna.com;

location / {

root /data/www/html ;

index index.html index.htm;

fastcgi_intercept_errors on ;

error_page 404 = 200 /ta.jpg ;

access_log /app/llogs/bbs_access.log main;

}

}

示范4:错误状态码URL重定向

server {

listen 80;

server_name www.banna.com;

location / {

root /data/www/html ;

index index.html index.htm;

fastcgi_intercept_errors on ;

error_page 404 http://www.tianmao.com/error/404.html;

access_log /app/llogs/bbs_access.log main;

}

}

示范5:将状态码转换为另一个location中

location / {

error_page 404 = @fallback;

}

location @fallback {

proxy_pass hhttp://backend;

 

阿里天猫的优雅显示参考:

error_page 500 501 502 503 504 http://err.tmall.com/error1.html;

error_page 400 403 404 405 411 412 413 http://err.tmall.com/error2.html

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值