【问题标题】:nginx configuration to provide dynamic error pagesnginx配置提供动态错误页面
【发布时间】:2019-02-12 14:06:35
【问题描述】:

我有一个 nginx 服务器设置,用于代理和提供静态内容。 静态内容基于多个反应应用程序。 我想要做的是替换来自 nginx 的默认错误页面并从 React 应用程序提供错误页面。

React 应用根据 url 处理错误页面。 http://localhost/401.html 将为应用程序提供标准的 401 HTTP 错误页面,依此类推。问题是如何在nginx中正确配置这个,因为实际上没有401.html页面。只有一个 html 文件,即 `index.html'。 React 应用程序根据使用 React Router 的 URL 处理所有自己的。

我尝试了一些 error_page 配置与 nginx 没有运气。

错误页面配置如下所示: ```

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 中的方式:

    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 not found 错误页面,因为我认为它试图在该文件夹中找到 401.html 页面。

【问题讨论】:

  • I have tried with some error_page,发生了什么?
  • @KedarnagMukanahallipatna 上面的配置用于error_page。它从 nginx 返回默认的 404 错误。即使我想通过在浏览器中指定路径来访问这些页面

标签: reactjs nginx custom-error-pages nginx-config


【解决方案1】:

为什么不这样做呢?假设你有一个错误目录。

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;
}

您可以将其用作reference

【讨论】:

  • 不起作用。我已经尝试过了,它仍然向我显示 nginx 错误页面
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-07
  • 2021-01-03
  • 1970-01-01
  • 2015-08-12
  • 1970-01-01
  • 2018-12-08
相关资源
最近更新 更多