Remove trailing slash from $uri in nginx? - nginx

I am caching my websites pages into a static flat file cache and am trying to use a try_files directive to load the file first from cache if it exists and then go to web application.
I'm basically trying to do something like this:
try_files $uri $uri/ /var/www/vhosts/example.com/httpdocs/staticfilecache/$uri/index.html
In this example if the user requested example.com/products/ it would try staticfilecache/products/index.html The problem I have is that $uri already contains a trailing slash. Trying to use this with $uriindex.html creates an error.
Is there any performant/easy way to always strip the / but not rewrite the address in the browser?

What you are trying to do has many problems on many levels.
"/var/www/vhosts/$uri" is invalid. Except if you have duplicated
your whole document root under the staticfilecache folder.
Nginx has it's own caching system and will check it first by itself
without any intervention from you as long as it has been setup.
Nginx will check for index files by itself as long as the index directive has been set.
Your code is actually attempting to check the staticcache last and
not first as you said you wanted.
There is no point in creating a static file cache on disk. Nginx can
just as well read the static files from their original locations.
In any case, Nginx will create the cache for items that need cache,
usually dynamic files, by itself. It is not a manual job.
Basically, it looks like you are a bit on the wrong track on some webserver fundamentals.

The answer(strictly) to your question is,
rewrite ^(.*)/$ $1 break;
But I would recommend you don't do this and go through #Dayo's answer.
For some interesting stuff for caching guidance, you can check this out.

Related

Nginx Config - Case Insensitive = 404

Newby at Nginx on Ubuntu. Main site is a WordPress site "example.com". No issue with this site. However, I need (cause someone did things in IIS and we need to move it to Nginx) to have a another site at "example.com/testsite" (not Wordpress, just static HTML).
Since this "testsite" was originally in IIS, sloppy coding was done and things are a complete mess of upper and lowercase.
So, I added a location block to my /ect/nginx/sites-enabled/example.com.conf
location /testsite {
alias /home/myusername/public_html/testsite;
index index.html;
}
This works! However as soon as a try to make this location case insensitve, I get a 404.
location ~* /testsite {
alias /home/myusername/public_html/testsite;
index index.html;
}
Ideas? Also, this site has tons of links that users can click on that are a mixture of upper/lower/proper and in no relation to the actual files that are on the server. Is there a way to make anything under that location block case insensitive (aka IIS way of doing it).
Thanks everyone.

Drupal doesn't generate thumbnails

I don't know why Drupal stopped generating thumbnails. So, I get error on files like this:
/sites/default/files/styles/choices/public/a1531172504.jpg?itok=Wn-VWDKd
Although the full image is found in:
/sites/default/files/a1531172504.jpg
I'm working on Nginx, Drupal 7
Last time I've answers question related to image derivatives I've got minuses so let be thorough here.
1.Drupal core will NOT create image derivative upon image upload - fact.
2.Thumbnail image (derivative from original image) is physically created upon HTTP request eg. if you visit the listing where image is used (when you visit the listing with thumbnail image the derivative is created on the fly via HTTP request)
3.To alter this behavior and to get a derivative image instantly upon image upload via CCK field you must use module Imageinfo Cache https://www.drupal.org/project/imageinfo_cache
With this info form above please recheck your site (go to listing where the thumbnail should appear and than check your thumbnail folder) If the image is still missing please provide more details like: Are you using CCK field or is it a custom field, are you creating derivative programmatically? Some code snippets are required here to solve you problem.
Also please check your .htaccess located in sites/default/files/.htaccess (not the general .htaccess)
and while you there check the permissions on files folder.
The solution for me was to add this lines to the nginx.conf files:
location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
expires max;
log_not_found off;
try_files $uri $uri/ #rewrite;
}
location #rewrite {
# not working with Drupal rewrite module: rewrite ^/?(.*?)/?$ /index.php?q=$1&$args;
rewrite ^/?(.*?)/?$ /index.php/$1&$args;
}
My environment is:
- docker-compose with nginx (nginx:1.17.4-alpine), drupal
(drupal:9.0.6-fpm-alpine) and mysql (mysql:8.0)
- drupal (9.0.6), nginx/1.17.4, PHP 7.4.10, MySql 8.0.21
The server responded with a 404 error because:
The image styles are generated when drupal receives a request.
Nginx receives a request for an image that is not in the filesystem; then returns a 404 instead redirect the request to drupal.
The rule I propose for the nginx.conf redirect the image request to drupal and solve the problem after restart the web server:
docker-compose restart webserver
My fix was to disable and re-enable the clean-url. .../admin/config/search/clean-urls".

Nginx Rewrite Rules - All Sub Folders

I am migrating site and bringing all my product urls to top level.
Example:
Old URL: http://www.example.com/sub/folder/product.asp
Old URL: http://www.example.com/sub1/folder1/product.asp
New URL: http://www.example.com/product.asp
I am struggling to get the nginx rewrite rule to work how i need it. Basically anything that ends in *.asp need to strip out all the folder paths.
You need to identify URIs ending with .asp that include more than one /. One possible solution might be:
rewrite ^/.+(/[^/]+\.asp)$ $1 permanent;
See this document for details.

Set nginx root based on existance of another directory

I'm building a system which uses dynamic DNS for user accounts, so you register for a sub domain.
I have a few server directives in place to catch things like api. and www. and other special cases, and I have a directive which reads the wildcard domain name and uses it to set a domain specific assets location.
server {
server_name "~^(?<domain>[a-z0-9]+)\.example\.com$";
root /sites/core;
location /assets {
alias /site-assets/$domain;
}
}
What I want to do is check for the existence of the assets directory and present, on the same domain, a different site.
To clarify, if the assets directory in the example above exists, serve one set of files, otherwise, serve another. Since the site I want to serve won't be IN the directory in question, I can't use try_files or anything like that.
I read all the horror stories about using the if directive but I think I need something like
if (-d /site-assets/$domain) {
... Do something
}
Then change the root, but that doesn't work.
Any ideas how I can achieve this?
OK. I figured this out. If there's a problem with it, let me know.
I have the following in a server directive
server {
server_name "~^(?<domain>[a-z0-9]+)\.example\.com$";
set $site core;
if (!-d /path/to/assets/$domain) {
set $site join;
}
root /path/to/sites/$site;
}
If the assets directory for the given domain name does NOT exist, then I can serve up a joining page for my site, which can pick up the domain name and allow the user to register. If it does exist, then serve up the main app.
This seems to me like a simple use of the if directive so shouldn't get me in too much trouble.

Changing sorting on Nginx autoindex

I am using autoindex with nginx for a specific endpoint that lists a bunch of folders. Is it possible to change the sorting direction from alphabetical to by modified date?
Not without rewriting the module.
You can use a PHP script to display whatever you like, or run a task which creates a html page.
Or even use Lua to create this for you and check if something has changed, then either display from cache or re-generate.

Resources