LINUX.ORG.RU
решено ФорумAdmin

Переадресация 404 на существующую страницу с возвратом статуса 303

 


1

1

Мне нужно все несуществующие пути сайта обработать средствами nginx след. образом: показать посетителю страницу-заглушку /content/katalog и возвратить код ответа 303 (ну или хотябы 200)

Применил директиву error_page:

error_page 404 =303 /index.php?q=content/katalog;
в результате посетитель, перешедший по несуществующему адресу видит нужную мне страницу /content/katalog, но вот с HTTP-кодом ответа проблема - он вместо ожидаемого 303 отдаёт 404:
$ echo -en "GET /foo-bar HTTP/1.1\r\nHost: example-site.ru\r\n\r\n" | nc example-site.ru 80

HTTP/1.1 404 Not Found
Server: nginx
Date: Sun, 16 Aug 2015 23:36:01 GMT
Content-Type: text/html
Content-Length: 162
Connection: keep-alive
Vary: Accept-Encoding

<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx</center>
</body>
</html>

Конфиг nginx для сайта на друпале использую следующий

server {
        listen 80 default;
        server_name example-site.ru;

        root /ssd/www/example-site; ## <-- Your only path reference.
 
        # Enable compression, this will help if you have for instance advagg module
        # by serving Gzip versions of the files.
        gzip_static on;
 
#       access_log /var/log/nginx/example-site-access.log;
        error_log /var/log/nginx/example-site-error.log;

        error_page 404 =303 /index.php?q=content/katalog;

        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }
 
        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }
 
        # Very rarely should these ever be accessed outside of your lan
        location ~* \.(txt|log)$ {
#               allow 192.168.0.0/16;
                deny all;
        }
 
        location ~ \..*/.*\.php$ {
                return 403;
        }
 
        # No no for private
        location ~ ^/sites/.*/private-files/ {
                return 403;
        }
 
        # Block access to "hidden" files and directories whose names begin with a
        # period. This includes directories used by version control systems such
        # as Subversion or Git to store control files.
        location ~ (^|/)\. {
                return 403;
        }
 
        location / {
                # This is cool because no php is touched for static content
                try_files $uri @rewrite;
        }
 
        location @rewrite {
                # Clean URLs are handled in drupal_environment_initialize().
                rewrite ^ /index.php;
        }

 
        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $request_filename;
                fastcgi_intercept_errors on;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
        }
 
        # Fighting with Styles? This little gem is amazing.
        location ~ ^/sites/.*/files/styles/ {
                try_files $uri @rewrite;
        }
 
        location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
                expires max;
#                log_not_found off;
        }

}

Помогите исправить возникшую проблему, товарищи!..

Нашёл ответ в списке рассылки nginx

помогла решить проблему следующая конструкция:

        error_page 403 404 = @katalog;

        location @katalog {
                return 303 /index.php?q=content/katalog;
        }

Ubiquitous_Nothing
() автор топика
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.