I want to set caching headers for files delivered by nginx. Is there a way to set caching headers for certain files based by their type? E.g. I want to set caching headers to all js files but not other files. I don't want to set headers based on the location where the files are.
I know this is old but a map and expires would automatically do that with Cache-Control Header without any extra modules, and you can still add public or private via add_header
map $sent_http_content_type $expires {
application/pdf 42d;
application/zip 42d;
application/x-7z-compressed 42d;
application/x-rar-compressed 42d;
application/octet-stream 1M;
application/javascript 3M;
text/css 3M;
~image/ 3M;
~video/ 3M;
~audio/ 3M;
~text/ 1d; # html/php
default off;
}
expires $expires;
You could use Headers More module. It has option to set headers based on response content type.
more_set_headers -t application/x-javascript 'Cache-Control: max-age=31536000'
Related
I am running a https server (Apache?) on my Mac. Done by this hints:
https://medium.com/#jonsamp/how-to-set-up-https-on-localhost-for-macos-b597bcf935ee
It serves my wasm-files with charset and I get: TypeError: WebAssembly: Response has unsupported MIME type 'application/wasm; charset=utf-8' expected 'application/wasm'
Changing .htaccess by adding
AddType 'application/wasm' .wasm
does not help. It feels like the server ignores .htaccess
In my nginx configuration, I have this location directive:
location /static/ {
alias /var/www/myproject/myproject/static/;
access_log off;
expires 30m;
break;
}
Now, I want to set the 'expires' to 1y only for files (within /static/) with extension .woff, woff2.
The problem is that the fonts files can be in many different subfolders of /var/www/myproject/myproject/static/
I have tried appending this other directive (after the other one)
location ~* \.(?:ttf|ttc|otf|eot|woff|woff2)$ {
access_log off;
expires 1d;
break;
}
But the fonts files are throwing a 404 error.
Any help?
Many thanks!
I'm new to CORS and have learnt that the OPTIONS preflight request sent by the browser excludes user credentials.
How do I get the filter (in httpd.conf) to respond to OPTIONS requests differently, i.e bypassing the authentication ?
This is my current configuration :
<LocationMatch /api>
SetEnvIfNoCase Origin "https://(www\.)?(domain1\.com|domain2\.com)(:\d+)?$" AccessControlAllowOrigin=$0
Header set Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin
Header set Access-Control-Allow-Credentials "true"
Header set Access-Control-Allow-Methods "GET,POST,DELETE,OPTIONS"
Header set Access-Control-Allow-Headers "Accept, Authorization, Origin, Content-Type"
AuthFormProvider ldap
AuthLDAPURL "ldap://localhost:10889/ou=Users,dc=work,dc=com?uid"
AuthLDAPGroupAttribute member
AuthLDAPGroupAttributeIsDN on
Require valid-user
ErrorDocument 401 /login.html
ErrorDocument 500 /error.html
AuthType form
AuthName realm
Session On
SessionMaxAge 1800
SessionDBDCookieName session path=/
ProxyPass http://localhost:8080 timeout=31536000
AuthFormFakeBasicAuth On
</LocationMatch>
And the javascript which makes the request :
$.ajax({
type : "DELETE",
url : "https://www.domain1.com/api",
xhrFields: {
withCredentials: true,
},
success : function(data){
},
});
I've tried the follwoing but with no luck :
(a)
RewriteEngine On
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [R=200,L,E=HTTP_ORIGIN:%{HTTP:ORIGIN}]
(b)
<Limit OPTIONS>
Header always set Access-Control-Allow-Origin "*"
Header always set Access-Control-Allow-Credentials "false"
Header always set Access-Control-Allow-Headers "Accept, Authorization, Origin, Content-Type"
Header always set Access-Control-Allow-Methods "GET,POST,DELETE,OPTIONS,PUT"
</Limit>
(c)
<Limit OPTIONS>
Allow for all
</Limit>
(d)
SetEnvIfNoCase Request_Method OPTIONS allowed
Any idea ? Please help !
I had the same issue which I solved today with the help of this question. Basically your option c.
My conf structure is:
conf/httpd.conf <- normal stuff
conf.d/ssl.conf <- set up ssl stuff
conf.d/api.conf <- set specific stuff to api like Auth
/var/www/.htaccess <- set specific stuff to api again
This allows for limiting everything except for OPTIONS
/conf.d/api.conf file:
<Directory "/var/www/api">
AllowOverride All
Options FollowSymLinks
<LimitExcept OPTIONS>
Auth stuff here
Mainly your Require statements
</LimitExcept>
</Directory>
Then in my .htaccess file I set the headers.
The Apache manual in the require directive states "Access controls which are applied in this way are effective for all methods. This is what is normally desired. If you wish to apply access controls only to specific methods, while leaving other methods unprotected, then place the Require statement into a <Limit> [or <LimitExcept>] section."
I had to make sure my application could handle OPTIONS as this setup is not doing an automatic return. Here or here one can see how to redirect which may work instead of having something in the application handle it.
I'm a new coming to nginx service, I misunderstand how nginx manage the files to do show.
Let's say an example. In my web application I have statics file with this path:
---public
| -- js
| -- img
| -- css
For invoke this kind of static files it just need to digit the current url: www.mysite.com/js/some.js or www.mysite.com/img/myimg.jpg that's fine it will show up.
But now let's say I have a script on my php files that use a different url to get a image from public/img folder using example www.mywebsite.com/20/10/29/myimg.jpg
if I add this code to my configuration in nginx, it will get correctly all files to serve to my application giving of course a real path of the file
location ~* \.(jpe?g|gif|png|ico|css|js|html|htm|woff|svg|ttf)$ {
access_log off;
expires max;
}
Getting the previous example with the url that generate the image, if I use that block of code in nginx and i try to go:
www.mywebsite.com/20/10/29/myimg.jpg
The image will displayed correctly because i show it via php, but on the console log the image get a 404 because nginx go to search for the file with extension jpg on public/20/10/29/myimg.jpg even if the file is on the public/img folder it make sense.
My question is, some one can explain to me how nginx logic has been created for serve static files and files via dynamic url?
Proper way is to get rid of awful block location ~* (...) and use simple prefix locations.
location /img/ {
access_log off;
expires max;
}
location /js/ {
access_log off;
expires max;
}
location /css/ {
access_log off;
expires max;
}
I am trying to configure my .htaccess file to set the cache time. Tryied every possible configuration but nothing works!
This is what is written in my HTML:
<meta http-equiv="Cache-Control" content="max-age=2592000, public" />
<!--
<meta http-equiv="expires" content="mon, 24 sep 2012 14:30:00 GMT">
-->
and this is what written in my .htaccess file:
ExpiresActive On
ExpiresDefault A3600
However, when I refresh inclusind cache clear (ctrl+F5) in firefox, my firebug NET panel says that the cache expires at the same second I have accessed the file ( and not in the future, as I want it to be).
What am I doing wrong??
Thanks
I advise you to use headers mod. You can activate it (if disabled) with this command :
a2enmod headers
Here is a simple code example that works:
<IfModule mod_headers.c>
# WEEK
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
# WEEK
<FilesMatch "\.(js|css|swf)$">
Header set Cache-Control "max-age=604800"
</FilesMatch>
</IfModule>
max-age is cached time in seconds.