404 on try_files

Francis Daly francis at daoine.org
Tue Oct 3 00:06:51 UTC 2017

On Thu, Sep 21, 2017 at 09:18:45AM -0700, Nathan Zabaldo wrote:

Hi there,

> The $request_uri
> /h_32/w_36/test.jpg
> needs to be routed to
> /index.php/img/i/h_32/w_36/test.jpg

What does "routed to" mean, specifically? nginx can proxy_pass to a http
server or fastcgi_pass to a fastcgi server or do an internal rewrite to
another url. It might be useful for you to think in nginx terms when
you are using nginx.

> 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.

When your fastcgi server gets multiple fastcgi_param entries with the same
"key", does it use the first, the last, or a random one?

What fastcgi_param values are you sending?

(The debug log, or tcpdump of the network traffic, should show you.)

> 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?

Can you show the complete nginx.conf that leads to this response? Perhaps
there is some other configuration that is being used here.

> 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)$ {

What values do you want $1 and $2 to have here? What values do they
actually have? (Hint: $2 is either "jpg" or "png".)

>     if ($request_uri !~ "/img/i/") {
>     set $request_url /index.php/img/i$1.$2;
>     }

You've used "if" inside "location" without using something like
"return". That may not do what you hope it will do.

>     try_files $uri $uri/ /index.php/img/i$1.$2;
>     }
>     location / {
>     try_files $uri $uri/ /index.php$uri?$args;
>     }
>     location ~ ^(.+\.php)(.*)$ {
>     fastcgi_pass;
>     fastcgi_param  CI_ENV production; #CI environment constant
>     fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
>     include fastcgi_params;

Does that "include" include a value for REQUEST_URI?

>     fastcgi_param  REQUEST_URI $request_url;
>     }

Francis Daly        francis at daoine.org

More information about the nginx mailing list