简体   繁体   English

nginx 上的配置错误页面不起作用

[英]Configuration error pages on nginx don't work

I can't run error pages on nginx if my settings are like this, where's the error?如果我的设置是这样的,我无法在 nginx 上运行错误页面,错误在哪里?

#ssl redirect www and hidden php and error
server {
listen :192.168.0.2:443 ssl;

root /usr/share/nginx/webserver/example/;
index index.html index.htm index.nginx-debian.html index.php;

server_name www.example.com;

# ssl on;
ssl_certificate /etc/ssl/example.pem;
ssl_certificate_key /etc/ssl/example.key;

location / {
try_files $uri $uri.html $uri/ @extensionless-php;
    index index.html index.htm index.php;
}

location /usr/share/nginx/webserver/example/id {
error_page 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 421 422 423 424 426 428 429 431 451 500 501 502 503 504 505 506 507 508 510 511 @error;
}

location @error {
     return 301  https://www.example.com/id/error;
}


location ~ \.php$ {
        try_files $uri = 404;
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
                fastcgi_cache phpcache;
                fastcgi_cache_valid 200 301 302 15m;
                add_header X-FastCGI-Cache $upstream_cache_status;

        fastcgi_hide_header X-Powered-By;

        }

location ~ /\.ht {
deny all;
}                                                                    

location @extensionless-php {
rewrite ^(.*)$ $1.php last;
}

location ~ \.css {
    add_header Content-Type text/css;
}

location ~ \.js {
    add_header Content-Type application/x-javascript;
}

location ~ \.(jpg|jpeg|gif|png|ico|html|xml|txt)$ {

}
}
server {
    listen 192.168.0.2:443 ssl;
    server_name example.com;
    return 301 $scheme://www.example.com$request_uri;
}

I want the error to start from example.com/id/ or if the path goes like this /usr/share/nginx/webserver/example/id/.我希望错误从 example.com/id/ 开始,或者如果路径像这样 /usr/share/nginx/webserver/example/id/。 How to set the right?如何设置正确?

The above is the code that I found on the internet以上是我在网上找到的代码

Most likely the problem is here:最有可能的问题在这里:

location /usr/share/nginx/webserver/example/id {
  error_page 300 301 302 303 304 305 306 307 308 309 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 421 422 423 424 426 428 429 431 451 500 501 502 503 504 505 506 507 508 510 511 @error;
}

This is telling nginx that if someone requests http://myserver/usr/share/nginx/webserver/example/id , then redirect to @error when there is one of the specified error codes.这告诉 nginx 如果有人请求http://myserver/usr/share/nginx/webserver/example/id ,则在出现指定错误代码之一时重定向到@error The redirect happens only for that path, which is not what you want.重定向针对该路径发生,这不是您想要的。

Instead you probably want to move the error_page directive outside of the location directive.相反,您可能希望将error_page指令移到 location 指令之外。 Also, you probably want to restrict to actual errors (>= 400).此外,您可能希望限制为实际错误 (>= 400)。 Note that these status codes are ones produced by nginx itself, not your application.请注意,这些状态码是由 nginx 本身产生的,而不是您的应用程序。 The following should work fine inside of your server block:以下应该在您的服务器块内正常工作:

error_page 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 421 422 423 424 426 428 429 431 451 500 501 502 503 504 505 506 507 508 510 511 @error;

(Just unwrapping and removing 3XX statuses.) (只是展开和删除 3XX 状态。)


EDIT how to handle multiple languages.编辑如何处理多种语言。

I'd recommend using variables.我建议使用变量。 Presumably, the requested language is coming in through the ACCEPT_LANGUAGE header, and if it's not, you'll need to find some other way of getting the language.据推测,请求的语言是通过ACCEPT_LANGUAGE header 输入的,如果不是,则需要找到其他获取该语言的方法。

Get the variable like this:像这样获取变量:

set $lang $http_accept_language;

And then modify your @erro location like this:然后像这样修改您的@erro位置:

location @error {
  return 301  https://www.example.com/$lang/error;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM