Sometimes, Nginx users encounter broken assets or links or uploads when visiting tenants address. In such cases, it’s important to ensure that all requests, especially static assets, are directed to the application (CRM) before showing a 404 error or apply rewrite rules.
We can achieve this by excluding requests for assets with the tenant path ID from being served as static files. Instead, they should be passed to the application (CRM), where the SaaS handles dynamic content or shows a 404 error when necessary.
For example, consider the highlighted path below:
location / {
try_files $uri $uri/ /index.php?/$request_uri;
}
If you are using proxy pass, ensure fastcgi_intercept_errors is turned off:
location ~ \.php$ {
...
fastcgi_intercept_errors off;
...
}
On some setup, the below block or similar block might still be preventing the request of asset file to reach the server. In such case please remove the block and consider alternative fast caching like varnish proxy:
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
expires max;
log_not_found off;
}
Ensuring your / block contain above highlighted part is essential. We want to ensure all request reaches the app ( Perfex CRM).
This is already in place in the advance sample config provided by the Perfex CRM team here: https://help.perfexcrm.com/nginx-config/
You should consult with your server admin for further assistance.
Using rewrite rule to fix the tenant file uploads (optional) #
As a contribution from the community, the file upload for tenants can be directly served using rewrite rules to forward tenant files upload request to the appropriate hard location.
Only use this option if you have the necessary skill to manage and validate Nginx configuration rules. It has the additional benefit of determining file location before reaching app and serving directly.
Here is the snippet to be added to your Nginx configuration (might works only when tenant is using subdomain):
location /uploads/ {
set $subdomain "";
if ($host ~* ^([^.]+)\.(.*)$) {
set $subdomain $1;
}
set $path_after_uploads $uri;
if ($uri ~ "^/uploads/(.*)") {
set $path_after_uploads /$1;
}
set $new_path /uploads/tenants/$subdomain$path_after_uploads;
# If there's a subdomain, use the new path; otherwise, use the original URI
if ($subdomain != "") {
rewrite ^/uploads/(.*)$ $new_path break;
}
try_files $new_path $uri =404;
}
Disclamer: we are not server administrator expert. Kindly consult with your server administrator to review your Nginx configuration or settings before applying them.