Closed umlumpa closed 9 months ago
Add a routes section matching static files you want and add respective headers in the action for this section as well as specify the directory with share. For example:
{
"routes": [
{
"match": {
"uri": "~\\.(jpg|jpeg|gif|png|svg|ico|pdf|ppt|txt|bmp|rtf|woff|woff2|ttf|eot)$"
},
"action": {
"share": "/var/www/html/src/frontend/web",
"response_headers": {
"Cache-Control": "max-age=1209600"
}
}
}
]
}
can you help me to convert it to nginx unit conf?
server {
listen 80;
error_log /dev/stdout error;
access_log off;
client_max_body_size 100M;
location / {
root /var/www/html/src/frontend/web;
try_files $uri /index.php$is_args$args;
}
location ~ ^/.+\.php(/|$) {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' $http_origin always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'Accept,Accept-Encoding,Authorization,Cache-Control,Connection,Content-Length,Content-Type,DNT,Host,Pragma,If-Modified-Since,Keep-Alive,Origin,User-Agent,Referer,X-Requested-With' always;
add_header 'Access-Control-Max-Age' 1728000 always;
add_header 'Content-Type' 'text/plain charset=UTF-8' always;
add_header 'Content-Length' 0 always;
add_header "Vary" "Origin";
return 200;
}
if ($request_method ~ '(GET|POST|PATCH|PUT|DELETE)') {
add_header 'Access-Control-Allow-Origin' $http_origin always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'Accept,Accept-Encoding,Authorization,Cache-Control,Connection,Content-Length,Content-Type,DNT,Host,Pragma,If-Modified-Since,Keep-Alive,Origin,User-Agent,Referer,X-Requested-With' always;
}
fastcgi_pass localhost:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/html/src/frontend/web$fastcgi_script_name;
fastcgi_param HTTPS off;
}
}```
Sure. Can you tell the reason behind allowing CORS for all origins? Which framework your application is using?
I am using php yii2 framework my actual nginx is working with this nginx.conf! and also i want to add gzip compression but dont find anything in documentation!
You can significantly simplify your configuration if you deal with CORS and gzip compression with the means of your framework.
I am using php yii2 framework my actual nginx is working with this nginx.conf! and also i want to add gzip compression but dont find anything in documentation!
Currently, NGINX Unit does not support GZIP in production. We are having a beta implementation ready but this didn't find it way into main, yet.
For CORS and friends you can use the response_headers
feature in a router action
.
https://unit.nginx.org/configuration/#handling-actions
Matching for HTTP-Methods is supported.
@rcknr , great answer to the original question!
Here's an alternative approach that matches faster (without regex) and simulates NGINX's expires
using JavaScript to do the +14d
calc.
[
{
"match": {
"uri": ["*.jpg", "*.jpeg", "*.gif", "*.png", "*.svg", "*.ico", "*.pdf"]
},
"action": {
"share": "/var/www/html/src/frontend/web",
"response_headers": {
"Expires": "`${(() => {var d = new Date(); d.setDate(d.getDate() + 14); return d.toUTCString();} )()}`"
}
}
}
]
@lcrilly Would it work using MIME filtering as well?
{
"share": "/var/www/html/src/frontend/web$uri",
"types": [
"application/pdf",
"font/*",
"image/*",
"text/*"
],
"response_headers": {
"Cache-Control": "max-age=1209600"
}
}
{
"listeners":{
"*:80":{
"pass":"routes"
}
},
"routes":[
{
"match": {
"uri": "~\\.(jpg|jpeg|gif|png|svg|ico|pdf|ppt|txt|bmp|rtf|woff|woff2|ttf|eot)$"
},
"action": {
"share": "/var/www/html/src/frontend/web",
"response_headers": {
"Cache-Control": "max-age=1209600"
}
}
},
{
"match":{
"uri":[
"!/assets/*",
"*.php",
"*.php/*"
]
},
"action":{
"pass":"applications/php/direct"
}
},
{
"action":{
"share":"/var/www/html/src/frontend/web$uri",
"fallback":{
"pass":"applications/php/index"
}
}
}
],
"applications":{
"php":{
"type":"php",
"limits": {
"timeout": 60,
"requests": 10000
},
"options": {
"file": "/etc/php.ini",
"admin": {
"memory_limit": "700M",
"variables_order": "EGPCS"
},
"user": {
"display_errors": "0"
}
},
"processes":{
"max":3,
"spare":1,
"idle_timeout":5
},
"targets":{
"direct":{
"root":"/var/www/html/src/frontend/web"
},
"index":{
"root":"/var/www/html/src/frontend/web",
"script":"index.php"
}
}
}
}
}
not working blocking just blocking jpg|jpeg|gif|png|svg|ico|pdf|ppt|txt|bmp|rtf|woff|woff2|ttf|eot 404 status not found
not working blocking
Suggest adding router diagnostic logging to your configuration:
echo '{"http":{"log_route":true}}' | unitc /config/settings
@chopanovv Make it "share": "/var/www/html/src/frontend/web$uri"
like in your last action.
In nginx i know! i didnot find from documentation!![cache](https://github.com/nginx/unit/assets/96824080/b67e241a-5728-48d1-8139-a76b7c428a31)