trying to cache js and css file with nginx - nginx

I'm trying to cache .css e .js files.
At the moment in this way is not working:
location /static {
alias /var/www/ttch/assets/;
}
location ~* ^.+\.(css|js)$ {
access_log off;
expires max;
}
With only this config, nginx serve correctly static file without cache:
location /static {
alias /var/www/ttch/assets/;
}
Any hints about how can I merge this two directives?
Thanks.

Alias or root directive helps you to identify location on the file system, but location is self-contained block you can extend easily, so this will work properly:
location /static {
alias /var/www/ttch/assets/;
access_log off;
expires max;
}
If you want to serve files with particular extensions, try this one:
location ~ ^/static/(.+\.(?:css|js))$ {
alias /var/www/ttch/assets/;
access_log off;
expires max;
}

Related

How exclude .json extension from nginx logs

I wan to exclude logs for all these extensions woff, jpg, jpeg, png, gif, ico, css, js, json
in default.conf I added these location rule
location ~* \.(woff|jpg|jpeg|png|gif|ico|css|js|json)$ {
access_log off;
}
all extensions was exluded from logs, except json
I tried separate rule like this
location ~* \.(json)$ {
access_log off;
}
or
location ~ \.json$ {
access_log off;
}
any way I still can see json files in logs
in the same default.conf I have another rule for json extension
location ~* \.(json)$ {
add_header Cache-Control "must-revalidate";
}
can this be the problem?
any idea how to solve?
Fixed by adding access_log off; rule in first location for json extension
location ~* \.(json)$ {
add_header Cache-Control "must-revalidate";
access_log off;
}

NGINX - invalid number of arguments in "location" directive

I am creating an NGINX global config file to be added to a website vhost file.
The code I put in the file /etc/nginx/global.d/wordpresscache.conf is:
set $cache_uri $request_uri;
# POST requests and URLs with a query string should always go to PHP
if ($request_method = POST) {
set $cache_uri 'null cache';
}
if ($query_string != "") {
set $cache_uri 'null cache';
}
# Don't cache URIs containing the following segments
if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php
|wp-.*.php|/feed/|index.php|wp-comments-popup.php
|wp-links-opml.php|wp-locations.php |sitemap(_index)?.xml
|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {
set $cache_uri 'null cache';
}
# Don't use the cache for logged-in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+
|wp-postpass|wordpress_logged_in") {
set $cache_uri 'null cache';
}
# Use cached or actual file if it exists, otherwise pass request to WordPress
location / {
try_files /wp-content/cache/supercache/$http_host/$cache_uri/index.html
$uri $uri/ /index.php;
}
# Cache static files for as long as possible
location ~*.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg
|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid
|midi|wav|bmp|rtf)$ {
expires max;
log_not_found off;
access_log off;
}
The problem is that when I reload the configuration I get this error message:
nginx: [emerg] invalid number of arguments in "location" directive in
/etc/nginx/global.d/wordpresscache.conf:35
However the structure of that location directive seem to be good to me. Here is where I got the code from (Tip 7)
Can anyone point me out to what I am doing wrong?
Edit:
I have tried the following:
location ~*. (ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg
|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid
|midi|wav|bmp|rtf)$ {
expires max;
log_not_found off;
access_log off;
}
location ~*. (ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg
|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid
|midi|wav|bmp|rtf)$ {
expires max;
log_not_found off;
access_log off;
}
location ~* \.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg
|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid
|midi|wav|bmp|rtf)$ {
expires max;
log_not_found off;
access_log off;
}
location ~*\. (ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg
|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid
|midi|wav|bmp|rtf)$ {
expires max;
log_not_found off;
access_log off;
}
But they all give me the same error :(
New Edit. I tried to remove some extensions... and it works. Now I have for testing purposes only:
location ~* \.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf)$ {
expires max;
log_not_found off;
access_log off;
}
and it works. Does anybody know what is the maximum number of arguments in parentheses?
location ~*.(ogg
Missed a space right there, should be:
location ~*. (ogg
The regex in question is a bit off also, because it would match URI /frogg or /blahwav, etc.
The correct is escaping the dot with \:
location ~* \.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg
|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid
|midi|wav|bmp|rtf)$ {
expires max;
log_not_found off;
access_log off;
}
Same thing applies to
location ~ .php$ {
Should be:
location ~ \.php$ {
All in all, that linked article is not escaping much of regexes, for whatever reason.
I suspect it's due to bad formatting/content sanitizing in whatever custom CMS they use.
Example:
location ~* wp-config.php {
deny all;
}
Would unnecessarily match /wp-configaphp or /wp-configuphp, and even /whateveryouwantmetobewp-configaphp, etc. There is no performance benefit in not escaping stuff in those regex, so yeah - "bad blogging" :-)
Should be:
location ~* /wp-config\.php$ {
deny all;
}
And even better, if you know you're using a single site in webroot directory, use exact matching:
location = /wp-config.php {
deny all;
}
You can also look at secure NGINX WordPress configuration which deals with security-related part of NGINX-Wordpress configuration.

Nginx custom uploads rewrite for WordPress Multisite

I have a WordPress multisite setup with subdomains on Nginx.
I'd like upload urls such as this to work...
http://mysub.mydomain.com/files/2017/07/myfile.png
where the location of the file is...
<web root>/wp-content/uploads/sites/xx/2017/07/myfile.png
This is the location rule in the Nginx config:
location ~ ^/files/(.*)$ {
try_files /wp-content/uploads/sites/$blogid/$1;
access_log off; log_not_found off; expires max;
}
It doesn't work and I'm unable to see why.
I have also tried (and numerous variations):
location ^~ /files {
internal;
alias /www/wp-content/uploads/sites/$blogid;
access_log off; log_not_found off; expires max;
}
If either of these can be confirmed as 'should work' then I'll start looking at rules higher up my code that may be taking precedence (although nothing's jumped out at me yet)...
This is what did work:
if (!-e $request_filename) {
rewrite ^/files/(.*) /wp-content/uploads/sites/$blogid/$1 last;
}

Setting cache-control on all folders' assets except one

I need to apply the following location rule for every single folder of my app with the exception of /forum and its children:
location ~* \.(?:jpg|jpeg|gif)$ {
expires 1M;
access_log off;
add_header Cache-Control "public";
}
I tried out setting it to location ~* ^/forum/.*\.(?:jpg|jpeg)$ but it doesn't look like it's working the way I want it to.
This is the solver I came up with:
location /forum {
...
}
location / {
location ~* \.(?:jpg|jpeg|gif)$ {
...
}
...
}
Simply separate the location directives and adjust commands accordingly.

Nginx conf exclude directory from location ~* \.(?:js)$

How can I exclude all URLs with a directory called dynamic in the following location block:
location ~* \.(?:js)$ {
expires 1y;
access_log off;
add_header Cache-Control "public";
}
Here's the entire config, most of this comes from herokus php nginx buildpack
http://pastebin.com/xQ4BDtwr
( stackex won't let me post "mostly code" )
I would add the following location:
location /path/to/dynamic/ {
location ~* \.js$ {internal;}
}
The key is to override the ~* \.(?:js)$ regex location with a prefix location. Then you don't have to worry about where it appears in your config.
It could be solved with another regex location ~ /dynamic/.*\.js$ {internal;}, but then you would need to be sure it always comes before the ~* \.(?:js)$ location; another problem waiting to happen when your config grows.

Resources