404 on try_files

Nathan Zabaldo nate at usamm.com
Thu Sep 21 16:18:45 UTC 2017


I'm going nuts on this. Any help would be much appreciated.

The $request_uri

/h_32/w_36/test.jpg

needs to be routed to

/index.php/img/i/h_32/w_36/test.jpg

index.php will route the request to the "img" controller and "i" method,
then process the image and return it. However, my MVC works off of the
REQUEST_URI. So simply rewriting the url will not work. The REQUEST_URI
needs to modified.

You can see in the last location block that I'm passing in the modified
REQUEST_URI, but Nginx is trying to open /var/www/vhosts/
ezrshop.com/htdocs/h_32/w_36/test.jpg (see **Error Logs** below) and
throwing a 404.

Shouldn't Nginx be trying to send it for processing to index.php?? Why the
404?

In a browser, if I go directly to
https://www.example.com/img/i/h_32/w_36/test.jpg the page comes up just
fine. If I try to go to https://www.example.com/h_32/w_36/test.jpg in my
browser I get 404 and the **Error Logs** you can see below.

    root /var/www/vhosts/example.com/htdocs;
    index index.php index.html;

    set $request_url $request_uri;

    location ~ (h|w|fm|trim|fit|pad|border|or|bg)_.*\.(jpg|png)$ {
    if ($request_uri !~ "/img/i/") {
    set $request_url /index.php/img/i$1.$2;
    }

    try_files $uri $uri/ /index.php/img/i$1.$2;
    }

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

    location ~ ^(.+\.php)(.*)$ {
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_param  CI_ENV production; #CI environment constant
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include fastcgi_params;
    fastcgi_param  REQUEST_URI $request_url;
    }

**Error Logs:**

**Log 1:** "/img/i/" does not match "/h_32/w_36/test.jpg", request: "GET
/h_32/w_36/test.jpg HTTP/1.1"

**Log 2:** open() "/var/www/vhosts/ezrshop.com/htdocs/h_32/w_36/test.jpg"
failed (2: No such file or directory), request: "GET /h_32/w_36/test.jpg
HTTP/1.1"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx/attachments/20170921/496fe66e/attachment.html>


More information about the nginx mailing list