4

My aim is to have any error in nginx (i.e. 404, 405 etc...) be redirected to my php script located at index.php. `

server {

    root /usr/share/nginx/TradeLog/www/;
    index index.php index.html index.htm default.html default.htm;

    # Make site accessible from http://localhost/
    server_name localhost;
    server_name_in_redirect off;

    fastcgi_intercept_errors on;

    error_page 403 404 /index.php/;


    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~* \.php$
    {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            include fastcgi_params;
            fastcgi_read_timeout 600; # Set fairly high for debugging
            fastcgi_index index.php;
    }

    location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
        expires max;
    }
}

`

However, when I attempt this, I am greeted with an nginx 404 error. What am I doing wrong here?

EDIT

I have found that if I turn fastcgi_intercept_errors on to off, then nginx deals with the errors. My next step is to then set a new request for the php script which will display errors. This is at the location /errors/code/<code_number>.

Say I had the following location block:

location = /composer.phar { return 403; }

I want to then redirect to the error page at /error/code/403

Therefore, I was thinking

error_page 403 = /error/code/403;

would do the trick for me, but my php script is still picking up the original request - I always show a 404 page because my PHP script is getting a request for a location that it doesn't know. I want nginx to tell it which page to display externally and then just pass the scrip the error number.

2
  • Try something like - error_page 403 404 /40x.html; location = /40x.html { index index.php; } Commented Dec 8, 2013 at 7:42
  • this gives me a 404 still. I'm not too sure how this is supposed to work anyway. Why would i navigate to the index if I have already specified the location? Commented Dec 8, 2013 at 7:55

2 Answers 2

5

I think you should replace:

error_page 403 404 /index.php/;

with:

error_page 403 404 /index.php;

The extra / make the redirection to a directory, not to a file. The rest of your configuration seems to be ok.

Sign up to request clarification or add additional context in comments.

Comments

0

This is working error_page 404 = /index.php;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.