nginx配置提供动态错误页面 [英] nginx configuration to provide dynamic error pages
问题描述
我有一个用于代理和提供静态内容的Nginx服务器设置. 静态内容基于多个React应用. 我要做的是替换nginx中的默认错误页面,并从React App中提供错误页面.
I have an nginx server setup that serves as is used to for proxy and serving static content. The static contents are based on multiple react apps. What I want to do is replace the default error pages from nginx and serve the error pages from a React App.
React应用基于URL处理错误页面. http://localhost/401.html
将为应用程序提供标准的401 HTTP错误页面,依此类推.问题是如何在nginx中正确配置它,因为实际上没有401.html
页面.只有一个html文件,即"index.html". React应用使用React Router根据URL处理所有属于自己的事件.
The React app handles error pages based on the url. http://localhost/401.html
will provide the standard 401 HTTP error page for the app, and so on. The problem is how to correctly configure this in nginx, because in reality there is no 401.html
page. There is just one html file and that is `index.html'. React app handles that all own its own based on the URL using React Router.
我尝试使用nginx进行一些error_page
配置,但是没有运气.
I have tried with some error_page
configuration with nginx with no luck.
以下是错误页面配置的样子: ```
Here is how the error page configuration looks like: ```
error_page 400 /error/400.html;
error_page 401 /error/401.html;
error_page 402 /error/402.html;
error_page 403 /error/403.html;
error_page 404 /error/404.html;
error_page 405 /error/405.html;
error_page 406 /error/406.html;
error_page 407 /error/407.html;
error_page 408 /error/408.html;
error_page 409 /error/409.html;
error_page 410 /error/410.html;
error_page 411 /error/411.html;
error_page 412 /error/412.html;
error_page 413 /error/413.html;
error_page 414 /error/414.html;
error_page 415 /error/415.html;
error_page 416 /error/416.html;
error_page 417 /error/417.html;
error_page 418 /error/418.html;
error_page 422 /error/422.html;
error_page 423 /error/423.html;
error_page 424 /error/424.html;
error_page 426 /error/426.html;
error_page 428 /error/428.html;
error_page 429 /error/429.html;
error_page 431 /error/431.html;
error_page 451 /error/451.html;
# 50x errors
error_page 500 /error/500.html;
error_page 501 /error/501.html;
error_page 502 /error/502.html;
error_page 503 /error/503.html;
error_page 504 /error/504.html;
error_page 505 /error/505.html;
error_page 506 /error/506.html;
error_page 507 /error/507.html;
error_page 511 /error/511.html;
location ^~ /error/ {
root /usr/local/var/www/html/;
allow all;
try_files $uri $uri/ /index.html;
}
```
这就是我将其包含在nginx.conf
中的方式:
And this is how I include it in my nginx.conf
:
server {
listen 8443 ssl;
server_name localhost;
ssl_certificate domain.crt;
ssl_certificate_key domain.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
if ( $http_user_agent ~* (nmap|nikto|wikto|sf|sqlmap|bsqlbf|w3af|acunetix|havij|appscan) ) {
return 444;
}
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $server_name:$server_port;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
include error-pages/error_pages.conf;
location /backend/ {
proxy_pass https://localhost:8181/backend/;
proxy_intercept_errors on;
}
}
是否可以在nginx中提供这样的错误页面?因为它仍然为我提供了来自nginx的标准404找不到错误页面,因为我认为它正在尝试在该文件夹中找到401.html
页面.
Is it possible to serve error pages like this in nginx? Because it still gives me the standard 404 not found error page from nginx, because i think it is trying to find the 401.html
page in that folder.
推荐答案
为什么不做这样的事情?假设您有一个错误目录.
Why not do something like this ? Assuming you have an error directory.
error_page 403 /error/403.html;
error_page 404 /error/404.html;
error_page 405 /error/405.html;
error_page 500 501 502 503 504 /error/5xx.html;
location ^~ /error/ {
internal;
root /var/www/default;
}
您可以将其用作参考
这篇关于nginx配置提供动态错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!