Nginx錯誤頁面重定向入門(yum安裝的Nginx)
阿新 • • 發佈:2019-02-19
1、環境:yum安裝的Nginx。
2、場景:重定向Nginx自帶的錯誤提示頁面。
3、配置檔案:/etc/nginx/conf.d/default.conf
tip:為什麼是這個配置檔案呢,因為在nginx.conf檔案中載入了include /etc/nginx/conf.d/*.conf;
4、default.conf原始碼:
server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
5、Nginx訪問頁面目錄:usr/share/nginx/html/
自定義加添了404.html檔案、403.html檔案、200目錄、403目錄、502目錄。
1、這樣訪問localhost
正常頁面
2、訪問localhost/200
、localhost/403
、localhost/502
拒絕訪問目錄,提示Nginx自帶提示錯誤
3、訪問localhost/404
未找到,提示Nginx自帶提示錯誤
然後我們修改default.conf
1、去掉error_page 404 /404.html;
的註釋,重定向404錯誤
2、新增#error_page 403 /403.html;
,重定向403錯誤
3、新增
location /502 { return 502; }
,訪問502目錄返回502狀態碼
熱重啟nginx
# service nginx reload
1、現在訪問localhost
正常頁面
2、訪問localhost/200
、localhost/403
拒絕訪問目錄,提示自定義頁面
3、訪問localhost/404
未找到,提示自定義頁面
4、訪問localhost/502
502狀態,提示自定義頁面
由於定義了
location /502 {
return 502;
}
所以訪問 localhost/502
返回502狀態,
由定義了
error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; }
所以502狀態提示頁面為50x.html
最後放上修改後的default.conf檔案
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 404 /404.html;
error_page 403 /403.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
location /502 {
return 502;
}
}