W
W
Warfare Noise2016-03-01 23:54:07
Nginx
Warfare Noise, 2016-03-01 23:54:07

How to redirect to nginx using try_files?

Greetings!
I have a site running on a bunch of LAMP + nginx as a proxy. How to make a redirect using try_files so that when accessing a non-existent address, a redirect to index.php occurs? For example www.example.com/unknown should instead of 404 redirect to the main page of the site.
nginx config:

server {
listen 80;
server_name example.com www.example.com;
access_logoff;
#access_log /srv/www/example.com/logs/example.com-acc main;
error_log /srv/www/example.com/logs/example.com-err;
client_max_body_size 256m;
location / {
try_files $uri /index.php$is_args$args; # redirect to index
proxy_pass http://127.0.0.1:8080;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_read_timeout 300;
proxy_buffer_size 16k;
proxy_buffers 32 16k;
}
location ~* \.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp |rtf|js|swf|flv|avi|djvu|mp3|mp4|ogv|3gp|otf)$ {
root /srv/www/example.com/htdocs/public;
}
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Voronkov, 2016-03-02
@DmitryVoronkov

If you have an error handler on your site that returns an error page like site.com/page404
Then do it, redirect from this page:

location /page404 {
try_files $uri /index.php
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question