0

Bear with me as I'm new to this.

I've put together an nginx server configuration that was working great until I noticed it wasn't throwing 404 errors when applicable. So I added to my try_files conditions the =404 fall back. However now it doesn't serve the @rewrites conditions and shows all the pages but the homepage as a 404.

Here is the applicable portion of the config

        server {
        error_page  404              /404.html;


    location / {

          index index.php;
          try_files $uri $uri/ @rewrite;

        }


        location @rewrite {


            #rewrites
            rewrite ^/listings/(\d+)/(.*)$ /index.php?op=default&_a=listing&id=$1 last;
            rewrite ^/subcategories/(\d+)/(.*)$ /index.php?op=default&_a=subcategory&id=$1 last;
            rewrite ^/categories/(\d+)/(.*)$ /index.php?op=default&_a=category&id=$1 last;
            rewrite ^/companies/(\d+)/(.*)/(.*)$ /index.php?op=default&_a=vendor&id=$1&_b=$3 last;
            rewrite ^/companies/(\d+)/(.*)$ /index.php?op=default&_a=vendor&id=$1 last;
            rewrite ^/classifieds/(.*)/(.*)$ /index.php?op=default&_a=classified&cf_type=$1&url=$2 last;
            rewrite ^/classifieds/(.*)$ /index.php?op=default&_a=classified&cf_type=$1 last;
            rewrite ^/classifieds$ /index.php?op=default&_a=classified last;
            rewrite ^/companies/(.*)$ /index.php?op=default&_a=vendor_index&ind=$1 last;
            rewrite ^/companies$ /index.php?op=default&_a=vendor_index last;
            rewrite ^/(.*)$ /index.php?op=default&_a=static&_b=$1 last;

        }

        }   

I tried [try_files $uri $uri/ @rewrite @error_page;] with a location @error_page block but that didn't work either. The rewrites all work perfectly if I take out the 404..

Any help would be greatly appreciated

  • The default URI, the named location or the return code is the last parameter of the try_files statement. There can only be one. See this document for more. – Richard Smith Jun 29 '17 at 7:57
  • Thanks for the clarification on syntax @RichardSmith. Any suggestions on how to get both rewrite and 404 error in the config? – winabun Jun 29 '17 at 17:15
  • I guess you have a bunch of rewrite statements inside the @rewrite block. But it's not clear under what conditions you would like a 404 response and how it was originally behaving instead. – Richard Smith Jun 29 '17 at 17:29
  • I want the 404 response to show when uri uri/ and @rewrite doesn't match. Right now when a page that doesn't exist is called for it serves a blank page and the nginx access log shows a normal page request. With the try_files 404 fall back it was serving the proper error page for a url that doesn't go anywhere. Unfortunately it was doing that for all the rewrite pages too. – winabun Jun 29 '17 at 17:41
  • Without seeing the @rewrite block and an example of a URI that should cause a 404 response, it is difficult to take this question further. – Richard Smith Jun 29 '17 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.