I want enable the gzip compression on my nginx server. The nginx.conf file is here:
http {
# Enable Gzip
server {
location ~* \.(?:ico|woff|css|js|gif|jpe?g|png)$ {
expires 30d;
add_header Pragma public;
add_header Cache-Control "public";
}
location /api {
try_files $uri $uri/ /api/index.php;
}
location / { ##merge
gzip on;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_proxied any;
gzip_types
# text/html is always compressed by HttpGzipModule
text/css
text/javascript
text/xml
text/plain
text/x-component
application/javascript
application/json
application/xml
application/rss+xml
font/truetype
font/opentype
application/vnd.ms-fontobject
image/svg+xml;
gzip_static on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";
gzip_vary on;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" { add_header "" ""; }
location ~ "^/ngx_pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon" { }
}
}
Unfortunately the gzip compression not working, Google Pagespeed and Gtmetrix not detect this.
Where can I place the gzip conf?
In The http{} server{} or location{} tag?
I already tried in the http and in the location tags too
You can put the gzip configuration anywhere, but if you want to apply it to all websites / files it is best to put it in the http section - this will then be the default for all server and location blocks. I would also "shorten" / change your config to the following:
http {
gzip on;
gzip_min_length 500;
gzip_proxied any;
gzip_comp_level 4;
gzip_types text/css text/javascript text/xml text/plain text/x-component application/javascript application/json application/xml application/rss+xml font/truetype font/opentype application/vnd.ms-fontobject image/svg+xml;
gzip_vary on;
gzip_disable "msie6";
... here come your server blocks / rest of your config
}
I use that configuration and it works fine for me - you can also test it in your browser first (for example with Firebug) before testing it with external services.
Using gzip_static only makes sense if you actually generate gzipped files for Nginx (as filename + .gz), so this has nothing to do with enabling gzip and should only be a possible second step.
Related
I have a Nginx server with follow parametrs `
server {
listen 443 ssl;
listen [::]:443 ssl;
ssl_certificate /home/dev/main.crt;
ssl_certificate_key /home/dev/crt.private;
server_name dev;
root /home/dev/ui;
index index.html;
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_types
application/atom+xml
application/geo+json
application/javascript
application/x-javascript
application/json
application/ld+json
application/manifest+json
application/rdf+xml
application/rss+xml
application/xhtml+xml
application/xml
font/eot
font/otf
font/ttf
image/svg+xml
text/css
text/javascript
text/plain
text/xml;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:4000$request_uri;
}
location /images {
alias /home/dev/img;
expires 20d;
try_files $uri $uri/ =404;
}
}
`
And when I add
`
location ~* .(?:ico|css|js|gif|jpe?g|png|svg|woff)$ {
expires 30d;
add_header Pragma public;
add_header Cache-Control "public";
}
`
the server starts returning errors for all images inside /home/dev/img. Have you ever faced such problem? If Have no clue what is going on, because when I remove the code, everything works fine, and images, js files load with out any issue
I' ve got simple setup to deploy angular, springboot, mysql stack. I'am serving this through nginx, with this configuration
server {
listen 8081;
server_name _;
root /usr/share/nginx/html;
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_min_length 32;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript font/opentype image/svg+xml image/x-icon application/javascript application/x-font-ttf application/x-font-otf application/font-woff application/font-woff2 application/vnd.ms-fontobject application/octet-stream;
error_log stderr info;
access_log /dev/stdout main;
location /api {
proxy_pass http://backend:8080/;
}
location /app {
root /usr/share/nginx/html;
try_files $uri$args $uri$args/ $uri/ /index.html;
#index index.html index.htm;
}
}
app is served well, but when i try to get localhost:8081/api it ends with 404, and in logs i've see that nginx end with 404, without passing to backend. Am i doing something wrong?
I use DDEV v1.15.3 with Typo3 v9 environment. I created the Typo3 environment according to the Quick Guide with ddev config --project-type=typo3 --php-version 7.3 --docroot=public --create-docroot=true. So far everything works fine, but no SVGZ files are displayed.
A svgz file uploaded in the backend in the fileadmin is not displayed in the browser - like https://ddev-typo3v9.ddev.site:444/fileadmin/user_upload/meinen_jmd_vor_ort_finden.svgz. I get the following error message:
This page contains the following errors:
error on line 1 at column 1: Encoding error
Below is a rendering of the page up to the first error.
To enable compression with gzip in the nginx-container, I added the following lines to the file .ddev/nginx_full/nginx-site.conf
# Compression
gzip on;
gzip_proxied any;
gzip_comp_level 6; # Level of compression
gzip_http_version 1.1;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_types
text/plain
text/css
text/xml
application/x-javascript
application/atom+xml
text/mathml
text/vnd.sun.j2me.app-descriptor
text/vnd.wap.wml
text/x-component
text/javascript
application/javascript
application/json
application/xml
application/rss+xml
font/truetype
font/opentype
application/vnd.ms-fontobject
image/svg+xml svg svgz;
gzip_static on;
gunzip on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6] \."; # Disable for IE < 6
# Add a vary header for downstream proxies to avoid sending
# cached gzipped files to IE6
gzip_vary on;
# ### Compression ###
# For CSS with compression
location ~* "\.css(\.|\.\d{10}\.)gzip$" {
rewrite ^(.+css)\.(\d+\.)gzip$ /$1.gzip;
add_header Content-Encoding gzip;
add_header Vary Accept-Encoding;
add_header Access-Control-Allow-Origin *;
gzip off;
types { text/css gzip; }
expires max;
log_not_found off;
}
# For JavaScript with compression
location ~* "\.js(\.|\.\d{10}\.)gzip$" {
rewrite ^(.+js)\.(\d+\.)gzip$ /$1.gzip;
add_header Content-Encoding gzip;
add_header Vary Accept-Encoding;
gzip off;
default_type application/javascript;
expires max;
log_not_found off;
}
# Compression for SVGZ
location ~* \.svgz$ {
add_header Content-Encoding gzip;
}
When I use ddev ssh to connect to the docker container, the nginx configuration also contains the above lines.
Thanks for your help.
Best greetings
-- Gerald
What you need in .ddev/nginx_full/nginx-site.conf is adding
location ~ \.svgz$ { add_header Content-Encoding gzip; }
I put my nginx-site.conf file at https://gist.github.com/rfay/5071ff19237ee7b83442cd8ad0311459
I used the file example.svgz from https://www.online-convert.com/file-format/svgz
This nginx issue explained what to do.
Here's what I see after 1) Uploading example.svgz with the file manager and then 2) Clicking the "show" icon.
If you think this is important to add to the default nginx configurations, please open an issue or a PR at https://github.com/drud/ddev/issues
Since I added the gzip block to my NGINX conf, the page returns a 404 for most static files (.js, .css, .png etc.) and was blocked because of a disallowed MIME type (“text/html”) for the vue build files while in server:80 block.
The files do have content-encoding: gzip response header
nginx.conf
server {
listen 80;
server_name *.domain.dev;
rewrite ^ https://$host$request_uri permanent;
}
server {
listen [::]:443 ssl http2;
listen 443 ssl http2;
server_name *.domain.dev;
location /static {
gzip on;
gzip_min_length 256;
gzip_buffers 4 32k;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype image/svg+xml image/x-icon;
gzip_vary on;
gzip_comp_level 6;
alias /home/ubuntu/code/domain_test/static;
expires 1y;
}
//... rest of config
}
If I move the GZIP block to the server:443 config, it works. Is the gzip block supposed to be in the ssl block, because of the redirect?
I'm trying to figure out why my custom CDN on nginx, doesn't appear to be working. Here is what I have in my site configuration:
server {
listen 80;
listen [::]:80;
server_name cdn.site.co.uk;
root /srv/www/site.co.uk/bob_user;
if ($uri !~ "\.(gif|jpe?g|png|js|css|eot|woff|ttf|svg)$") {
rewrite ^/(.*)$ https://site.co.uk/ permanent;
}
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
}
The file itself works - but using this tool, it tells me that it does:
https://varvy.com/tools/gzip/
This is the URL I tested:
http://cdn.businessofbrands.co.uk/wp-includes/js/jquery/jquery.js
I'm a bit confused as to why this is. Could anyone shed some light?
It looks like application/javascript is missing from gzip_types.
You'll want to add it to the following line:
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;
Actually, text/javascript is obsolete so you could just replace it :)