4

i'm running latest stable version of Nginx on GNU/Linux OS and having the following virtual host, i'm trying to setup custom localized 404 error pages avoiding if but i always end up in redirect loops.

By now, i'm only contemplating the following locales es, en and ca which i get as a URL parameter, always next to the domain. The expected URLs are something like:

http://www.example.com/ca
http://www.example.com/ca/home
http://www.example.com/en
http://www.example.com/en/contact

This is my nginx server block:

server {
        listen          80;
        listen          [::]:80;
        server_name     example.com;
        root            /var/www/html/example_com;
        # By default, show Castilian index
        index           es/index.html;

        access_log      /var/log/nginx/example_com.access.log main;
        error_log       /var/log/nginx/example_com.error.log error;

        #Remove HTML Extension
        rewrite ^(/.*)\.html(\?.*)?$ $1$2 permanent;

        # Remove trailing slash
        rewrite ^/(.*)/$ /$1 permanent;

        # Make sure Nginx knows what files to look for, and for that we use the try_files directive.
        # Look for a file with the current $uri and an .html extension, and if no file exists, 
        # check for a directory with that name and serve the index. Otherwise, render a 404 error.
        try_files $uri/index.html $uri.html $uri/ $uri =404;

        location = /form/contact.php {
                deny all;
                return 404;
        }

        # Custom error pages
        set $error404 /en/404.html;

        location /ca {
            set $error404 /ca/404.html;
        }

        error_page 404 =404 $error404;


        location = /form/contact {
                try_files $uri.php =404;

                include /etc/nginx/fastcgi_params;
                fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
                send_timeout 1800;
                fastcgi_read_timeout 1800;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
        }

        location ~ /\.ht {
            deny  all;
        }

        location ~ /(config) {
                deny all;
                return 404;
        }
    }
peris
  • 943
  • 3
  • 20
  • 33

2 Answers2

6

You cannot use location blocks like that, you will break all of your other location blocks. See how nginx processes a request for details.

There is an easier way. Use a named location to process the 404 response, and simply rewrite the original URI into the required error page.

For example:

error_page 404 @error404;

location @error404 {
    rewrite ^/(en|es|ca) /$1/404.html last;
    rewrite ^ /en/404.html last;
}

See this document for more.

Richard Smith
  • 45,711
  • 6
  • 82
  • 81
  • Thanks a lot for helping and providing an efficient solution. Also took note of the links, i already read many of them but first time are hard to find out a working solution :) – peris Apr 13 '17 at 14:27
  • I've coded a PHP script accessible from the following URLs[1][2][3][4] so i added the following location block to my nginx config file. By now it works fine except by mistake it allows URLs like the following ones where language locale is repeated[5][6]. Could you please help me to fix it?I'm pasting here the location block i came out with. [1]http://www.example.com/en/oil [2]http://www.example.com/en/oil-paint [3]http://www.example.com/es/oleo [4]http://www.example.com/es/pintura-al-oleo [5]http://www.example.com/es/es/es/es/es/pintura-al-oleo [6]http://www.example.com/en/en/oil – peris Apr 13 '17 at 14:30
  • This is the location block i'm using https goo.gl/rd221F – peris Apr 13 '17 at 14:37
  • You should start a new question. – Richard Smith Apr 13 '17 at 16:15
0

Just add this below line for error page redirection in your config fine inside the server.

error_page 404 https://www.example.com/404-page.html;

This solved my problem