I have successfully setup an nginx server which uses both alias + rewrite as in the below configuration. Everything works except that javascript files are not enforced browser caching.
- Server url: http://localhost/
- Server root: /space1/www
- An alias named 'builder', with its root directory: /space1/builder
- There's a web app called 'luoicongtrinh' in: /space1/builder/apps/luoicongtrinh/admin
- Path of static resources of this app are rewritten, like:
http://localhost/builder/apps/luoicongtrinh/admin/js/main.js
will be served from the location: /space1/builder/apps/luoicongtrinh/public/js/main.js - I would like to setup Cache-control for this file but still unsuccessful
Current working configuration (with no cache-control)
server {
listen 80;
server_name localhost;
root /space1/www;
index index.html index.htm index.php;
access_log /var/log/nginx/access.log debug;
location / {
server_tokens off;
client_max_body_size 20m;
client_body_buffer_size 128k;
}
location ~ /\. {
access_log off;
log_not_found off;
deny all;
}
# builder
location /builder {
alias /space1/builder;
# luoicongtrinh
rewrite ^/builder/apps/luoicongtrinh/(css|images|js|fonts|lib|uploads)/(.+)$ /builder/apps/luoicongtrinh/public/$1/$2 last;
rewrite ^/builder/apps/luoicongtrinh/admin/(css|images|js|fonts|lib|uploads)/(.+)$ /builder/apps/luoicongtrinh/public/$1/$2 last;
rewrite ^/builder/apps/luoicongtrinh/admin/?(.*)$ /builder/apps/luoicongtrinh/admin/index.php?p=$1&$args last;
fastcgi_split_path_info ^/builder/(.+\.php)(.*)$;
include /etc/nginx/php_fastcgi;
}
# Default PHP support
include /etc/nginx/php_fastcgi;
}
I try to add the following block to enforce browser caching for static files (for example: http://localhost/builder/apps/luoicongtrinh/admin/js/main.js) but this does not work.
location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
expires 30d;
add_header Pragma public;
add_header Cache-Control "public";
try_files $uri =404;
}
I have also try various configurations but still unsuccessful.
Please guide me how to set this up correctly. Many thanks!!!
UPDATE:
I have tried using "if" block and this works.
# builder
location /builder {
alias /space1/builder;
# cache control for static resources
if ($uri ~* "/(css|images|js|fonts|lib|uploads)/") {
expires 30d;
add_header Pragma public;
add_header Cache-Control "public";
}
# luoicongtrinh
rewrite ^/builder/apps/luoicongtrinh/(css|images|js|fonts|lib|uploads)/(.+)$ /builder/apps/luoicongtrinh/public/$1/$2 last;
rewrite ^/builder/apps/luoicongtrinh/admin/(css|images|js|fonts|lib|uploads)/(.+)$ /builder/apps/luoicongtrinh/public/$1/$2 last;
rewrite ^/builder/apps/luoicongtrinh/admin/?(.*)$ /builder/apps/luoicongtrinh/admin/index.php?p=$1&$args last;
fastcgi_split_path_info ^/builder/(.+\.php)(.*)$;
include /etc/nginx/php_fastcgi;
}
I've read that using "if" is "evil". Do you know how to formulate the above "if" block another way?
Best Answer
add_header is part of a third party module called "headers more", you either need to build nginx from source or make sure your repository has already done it for you.
To build nginx from source read this tutorial I wrote - it's pretty quick and easy.
The way to check if the module is working is to run
Add see if this is in the output
Another way is to add a simple add_header to the main location block. add_header's a good debugging tool as well.
For reference, here's how I do the Nginx build. Note at the tutorial above there's a more optimized version of the configure command, but this one is more compatible and probably safer
Then try this kind of block for your static resource caching. Note there is no try_files in there.