通过url参数根据locale在nginx上设置自定义404错误页面 [英] Set custom 404 error page on nginx based on locale through url parameter

查看:90
本文介绍了通过url参数根据locale在nginx上设置自定义404错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 GNU/Linux 操作系统上运行最新稳定版本的 Nginx 并拥有以下虚拟主机,我正在尝试 设置自定义本地化 404错误页面避免 if 但我总是以重定向循环结束.

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.

到目前为止,我只考虑以下语言环境 esenca 作为 URL 参数,总是下一个到域.预期的网址是,类似于:

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

这是我的 nginx 服务器块:

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

推荐答案

您不能使用这样的位置块,否则会破坏所有其他位置块.有关详细信息,请参阅nginx 如何处理请求.

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

有一个更简单的方法.使用命名位置处理 404 响应,只需将原始 URI 重写为所需的错误页面.

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.

例如:

error_page 404 @error404;

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

有关更多信息,请参阅本文档.

See this document for more.

这篇关于通过url参数根据locale在nginx上设置自定义404错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆