@Paulo_o
The Nginx code is required for specific permalink settings of WordPress to work - that is the brief and simple explanation : this code actually prevents 404's by instructing Nginx to forward "pretty permalink" requests - which are not actual URI - as URI to Apache that can be dealt with by WordPress.
It is hence not really recommended to remove the error_page directives from Nginx config - you applied a "very dirty workaround", which is not good.
That was question 1.
Question 2 can be answered by a more general solution (and not a dirty workaround) that applies to your general issue of non-existing files.
It does not matter whether you have an URI pointing to a non-existing product or a non-existing file (such as an image).
The non-existing product page or image result in a 404 generated by WordPress, due to the fact that the URI is not existing.
Thanks to the non-existing URI, one gets "redirected" to the Nginx location block with the name @wpt_permalinks_fallback .......
............ and there is the issue : in that location block, it is specified that Nginx should attempt to resolve the request with (in chronological order)
1 - $uri : the URI, if it can be matched by Nginx,
2 - $sef_entry_point : a value equal to "/" by default and to "/index.php?$args" if the URI starts with / but cannot be matched by Nginx,
and, as a result, pretty permalink requests are "redirected" to "/index.php?$args".
In your case, it is not about pretty permalink requests, but all about requests that cannot be matched by Nginx thanks to non-existing files or images.
Stated differently, you are redirected to "/index.php?$args" ........ and that creates 404s.
For that reason, the removal of the 404 error_page directive did provide you with some advantages - but it is not a solid solution.
In essence, you could try to alter the Nginx code in such a way that you do not end up at "/index.php?$args".
There are two methods that you can test :
a - remove all code and replace by one simple line containing :
error_page 404 /
and this will redirect to the home page, (OR)
b - only remove the code
if ($uri ~* "^/") {
set $sef_entry_point "/index.php?$args";
}
and this will also redirect to the home page, but Nginx still tries to match $uri, (OR)
c - change the line
try_files $uri $sef_entry_point;
to
try_files $uri / $sef_entry_point;
and hence force Nginx to try $uri first and, if not matched, to try the home page on "/" second and, if not matched, to try $sef_entry_point as the last resort.
Please note that I did not do any testing, but it could work.
As a final remark, it should be duly noted that any update would overwrite your customizations to the Nginx config.
That is not really a problem, first try out whether solution a or b works for you!
I hope the above helps a bit.....
Kind regards.....
PS If you want to create a redirect with a 301 status code, then you could try the following solution too :
location @alt_fallback {
return 301 /;
}
error_page 404 = @alt_fallback;
error_page 405 = @alt_fallback;