Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx Error Pages - One Location Rule to Fit Them All?

Having the following nginx vhost config:

server {     listen 80;     listen 443 ssl;     server_name default;     root /var/www/default/html;     error_log /var/www/default/log/error.log;     access_log /var/www/default/log/access.log;     ssl_certificate /etc/ssl/certs/ssl-cert-snakeoil.pem;     ssl_certificate_key /etc/ssl/private/ssl-cert-snakeoil.key;      autoindex on;     index index.html index.php;      location / {         try_files $uri $uri/ @php;     }      location @php {         rewrite ^/(.*)/?$ /index.php/$1 last;     }      location ~* /(?:[.]|.*[.](?:bak|fla|inc|ini|log|psd|sh|sql|swp)|(?:file|upload)s?/.*[.](?:php)) {         deny all;     }      location ~* [.](?:php) {         fastcgi_buffer_size             128k;         fastcgi_buffers                 4 256k;         fastcgi_busy_buffers_size       256k;         fastcgi_connect_timeout         30;         fastcgi_ignore_client_abort     off;         fastcgi_index                   index.php;         fastcgi_intercept_errors        on;         fastcgi_pass                    unix:/var/run/php5-fpm.sock;         fastcgi_read_timeout            60;         fastcgi_send_timeout            60;         fastcgi_split_path_info         ^(.+[.]php)(/.*)$;         fastcgi_temp_file_write_size    256k;          include /etc/nginx/fastcgi_params;     }      error_page 403 /403.html; location = /403.html {         root /var/www/default/error;     }      error_page 404 /404.html; location = /404.html {         root /var/www/default/error;     }      error_page 405 /405.html; location = /405.html {         root /var/www/default/error;     }      error_page 500 501 502 503 504 /5xx.html; location = /5xx.html {         root /var/www/default/error;     } } 

Is it possible to have the 40x and 50x errors served by a single location rule? Something like:

error_page 403 /403.html; error_page 404 /404.html; error_page 405 /405.html; error_page 500 501 502 503 504 /5xx.html;  location ~ /(?:40[345]|5xx)[.]html$ {     root /var/www/default/error; } 

If the I the above, I always get the nginx default 404 errors. String matches (no operator) and exact matches (= operator) work, but with the the case-[in]sensitive regex operator (~[*]) it doesn't.

I guess the problem is the order in which the location blocks are processed.

Is there anyway to overcome that to reduce the unneeded root redundancy?

like image 814
Alix Axel Avatar asked Nov 29 '12 08:11

Alix Axel


People also ask

Where are nginx error pages?

Creating Your Custom Error Pages Put your custom error pages in the /usr/share/nginx/html directory where Nginx sets its default document root. You'll make a page for 404 errors called custom_404. html and one for general 500-level errors called custom_50x.

How do I change the default 404 page in nginx?

Create a configuration file called custom-error-page. conf under /etc/nginx/snippets/ as shown. This configuration causes an internal redirect to the URI/error-page. html every time NGINX encounters any of the specified HTTP errors 404, 403, 500, and 503.

What is 404 not found nginx?

Essentially, the “404 error” indicates that your or your visitor's web browser was connected successfully to the website server or the host. However, it was unable to locate the requested resource, such as filename or any specific URL.

What is nginx location?

Every NGINX configuration file will be found in the /etc/nginx/ directory, with the main configuration file located in /etc/nginx/nginx. conf . NGINX configuration options are known as “directives”: these are arranged into groups, known interchangeably as blocks or contexts .


1 Answers

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; } 
  • http://nginx.org/r/location
  • http://nginx.org/r/error_page
  • http://nginx.org/r/root
like image 160
VBart Avatar answered Sep 21 '22 20:09

VBart