Setting nginx expire headers only for fonts - caching

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!

Related

WordPress and YOURLS on one domain (Nginx)

I want to run both WordPress and YOURLS on one domain.
Since both need to handle URLs differently, they need different try_files directives. WordPress sits on the root of the domain (domain.tld), while YOURLS is being installed to the /go/ directory.
Despite the two location rules, I get 404s on any links generated by YOURLS (e.g. domain.tld/g/linkname, all are redirects to external URLs), though I can access the YOURLS admin backend.
As far as I read, declaring to location rules (one for /go/, and one for /) should suffice in order to let Nginx handle the direct and the /go/ URLS differently - is there something in wrong in my thinking?
I have added the following to the Nginx config:
location / {
try_files $uri $uri/ /index.php?$args;
}
location /go/ {
try_files $uri $uri/ /go/yourls-loader.php$is_args$args;
}
What am I missing?
Not sure if it will be of direct help to you or not.
I was trying to do a similar configuration using a subdirectory pointing to yourls docker
After many hits and trials, the following settings have worked for me. Maybe some part of it might be useful for someone.
location ~ (\/shorturl\/.*) {
if ( $uri ~ ^\/shorturl\/((admin|images|js|css)(\/?)(.*)|.*.(\.html|\.php)) ) {
rewrite /shorturl(/?)(.*) $1$2 break;
}
rewrite (\/shorturl\/.*) $1 break;
proxy_pass http://localhost:9091;
proxy_redirect / /shorturl/;
}
PS: I know if is evil and please add proxy headers as per your need!

Session is not working if php.ini file is present other than root

I am implementing the video uploading in my project.if i include php.ini in any folder other than root, session is not working. php.ini file is
default_charset = UTF-8;
memory_limit = 64M;
max_execution_time = 30;
upload_max_filesize = 512M;
safe_mode = Off;
mysql.connect_timeout = 20;
session.use_cookies = On;
session.use_trans_sid = Off;
session.gc_maxlifetime = 12000000;
This file is in users folder where a user can log in to upload a video.If i remove this file,session working properly but video uploading failed.so what is my mistake, please guide me.I have tried for many solution regarding the sessions suggested in stackoverflow,but failed. Thanks in advance
php.ini file should be renamed as .user.ini,then it will work.This file should be placed in folder where uploading code is written.Session is also working now.

Nginx configuration to deny access to private Laravel folders

Every answer or tutorial about nginx configuration on Laravel has something like that:
location ~ \.php {
# fastcgi stuff ...
}
If the Laravel framework has a unique entry point (the public index.php), wouldn't it be this definition more accurate?
location ~ index\.php {
# fastcgi stuff ...
}
In a context where you have your app deployed on a subfolder, the first configuration it's allowing the access on every php private file, even if I have already defined a deny all rule over the private folder.
I'm wrong with anything?
There is another way to define a fully deny access over a folder?
If you want deny access to files in your private folder (e.g. /private) add this to your config for virtual host:
Case sensitive
location ~ ^/private/(.*)\.php$ {
deny all;
}
Case insensitive:
location ~* ^/private/(.*)\.php$ {
deny all;
}
This returns 403 all requests to /private/something.php, /private/something/something.php and so on. But requests to /private/something will work.

proper way to manage images and static files nginx

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;
}

Nginx caching headers by file type

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'

Resources