what happen to nginx when both gzip and gzip_static are enable - nginx

Consider there are 3 files named a.html, a.html.gz, b.html and they are served by nginx with the configuration gzip on and gzip_static on, and we also have sendfile on.
What make me feel confused are:
When I access localhost/a.html in browser, I know it will read content from a.html.gz, but will sendfile still work in this case?
What happen when I access localhost/b.html? Will it just send the uncompressed file, or will nginx will gzip this file dynamically?
By the way, is there any configuration in nginx can show the detail log about how a request are handled by different modules?

Related

Does nginx automatically served gzipped location matches when gzip_static is on?

I have a question about serving gzipped static files from nginx. I did gzip -k style.min.css to produce style.min.css.gz and I uploaded it to the server in the static-root directory. My location block is an exact match and looks like this:
location =/style.min.css {
root /home/ubuntu/.../static-root/;
gzip_static on;
expires 100d;
add_header Cache-Control "public";
access_log off;
}
Will nginx just serve up the style.min.css.gz in place of the style.min.css automagically, or do I have to tweak that location block so that the gzipped version is served?
Given the exact match at that location block this test does 404
$ curl -I https://example.com/style.css.gz -H "Accept-Encoding: gzip"
HTTP/1.1 404 Not Found
Is the compressed version still getting served up or do I need to tweak the location block to something like this so that the .gz file gets served?
location ~ /(style.min.css.*) {...
Update ... I can confirm that a gzipped version of a static css file is returned. So it seems to happen automatically. I don't know if the gzip on; in the server section or the gzip_static on; takes care of it but it is working.
$ curl -H "Accept-Encoding: gzip" -I https://example.com/bsmin.css
HTTP/1.1 200 OK
Cache-Control: max-age=7673705, public
Cache-control: no-cache="set-cookie"
Content-Encoding: gzip
From the official documentation https://docs.nginx.com/nginx/admin-guide/web-server/compression/ under Sending Compressed Files
to service a request for /path/to/file, NGINX tries to find and send the file /path/to/file.gz. If the file doesn’t exist, or the client does not support gzip, NGINX sends the uncompressed version of the file.
Note that the gzip_static directive does not enable on-the-fly compression. It merely uses a file compressed beforehand by any compression tool. To compress content (and not only static content) at runtime, use the gzip directive.

Can nginx do transparent compression of websocket connections?

I have nginx acting as reverse-proxy for an ASP.net / Kestrel back-end server.
I'd like nginx to do gzip compression (ie, permessage-deflate / rfc7692) for my Websocket connections, but I can't find any config options for that.
Can nginx do that? Are there any plugins to make it work? If no, is there something else I can use?
just edit your nginx config. It is the same for static sites, proxies or websockets
gunzip on; // be sure to include this. This enables runtime decompression for clients that do not accept gzip
gzip on; //enables gzip for request
gzip_proxied any; //enables compression on proxies
gzip_types *; //compress everything there is
if this does not work, try compressing the messages befor sending them through the socket.
You can read more about it here: https://docs.nginx.com/nginx/admin-guide/web-server/compression/

Disable unzipping the zipped response in Ngnix

I have 5 micro-services that are up and running. One of them is an nginx server that acts as a gateway( reverse proxy for other services). There is another service called 'web' that is an nginx server that serves all the client side static bundles. I have enabled gzipping in the web nginx server. But when the compressed response comes through the gateway nginx server, it decompresses the files and sends them back to the client. I tried setting gzip off and gunzip off in gateway nginx server but it is not working.
Here is the configuration of the web-nginx server:
gzip on;
gzip_comp_level 3;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
gzip_min_length 100;
gzip_buffers 4 32k;
Here is the configuration for the gateway ngnix server:
gzip off;
gunzip off;
Any kind of help is appreciated.
You need to add gzip_proxied any; to the backend nginx servers (serving static files)
Compress data even for clients that are connecting to us via proxies,
identified by the "Via" header (required for CloudFront/Cloudflare).
The default value is off which disables compression for all proxied requests, ignoring other parameters; For more info checkout the nginx docs
I found the mistake that, i failed to forward the header using proxy_pass from proxy server to actual server. With help of above answer. it worked.

How to disable/deactivate nginx file compression/merge?

We use PLESK and pagespeed with nginx. Now all CSS-files are compressed and merged into one single file. Like test.com/A.style.css+style2.css+plugin.css+test.css etc.
How can i disable that file compression for development in PLESK with additional nginx commands or something? I want to use nginx but without merging of CSS-files.
Thanks!
Pagespeed uses gzip compression, so you can just disable it completely using directive:
gzip off;
Or specify the gzip_types values without text/css files.
Check the module description here.
In case you manage website configuration in Plesk, you can add additional nginx directives in Plesk > Subscriptions > example.com > Apache & Nginx Settings > Additional nginx directives.

nginx upload client_max_body_size issue

I'm running nginx/ruby-on-rails and I have a simple multipart form to upload files.
Everything works fine until I decide to restrict the maximum size of files I want uploaded.
To do that, I set the nginx client_max_body_size to 1m (1MB) and expect a HTTP 413 (Request Entity Too Large) status in response when that rule breaks.
The problem is that when I upload a 1.2 MB file, instead of displaying the HTTP 413 error page, the browser hangs a bit and then dies with a "Connection was reset while the page was loading" message.
I've tried just about every option there is that nginx offers, nothing seems to work. Does anyone have any ideas about this?
Here's my nginx.conf:
worker_processes 1;
timer_resolution 1000ms;
events {
worker_connections 1024;
}
http {
passenger_root /the_passenger_root;
passenger_ruby /the_ruby;
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.x.com;
client_max_body_size 1M;
passenger_use_global_queue on;
root /the_root;
passenger_enabled on;
error_page 404 /404.html;
error_page 413 /413.html;
}
}
Thanks.
**Edit**
Environment/UA: Windows XP/Firefox 3.6.13
nginx "fails fast" when the client informs it that it's going to send a body larger than the client_max_body_size by sending a 413 response and closing the connection.
Most clients don't read responses until the entire request body is sent. Because nginx closes the connection, the client sends data to the closed socket, causing a TCP RST.
If your HTTP client supports it, the best way to handle this is to send an Expect: 100-Continue header. Nginx supports this correctly as of 1.2.7, and will reply with a 413 Request Entity Too Large response rather than 100 Continue if Content-Length exceeds the maximum body size.
Does your upload die at the very end? 99% before crashing? Client body and buffers are key because nginx must buffer incoming data. The body configs (data of the request body) specify how nginx handles the bulk flow of binary data from multi-part-form clients into your app's logic.
The clean setting frees up memory and consumption limits by instructing nginx to store incoming buffer in a file and then clean this file later from disk by deleting it.
Set body_in_file_only to clean and adjust buffers for the client_max_body_size. The original question's config already had sendfile on, increase timeouts too. I use the settings below to fix this, appropriate across your local config, server, & http contexts.
client_body_in_file_only clean;
client_body_buffer_size 32K;
client_max_body_size 300M;
sendfile on;
send_timeout 300s;
From the documentation:
It is necessary to keep in mind that the browsers do not know how to correctly show this error.
I suspect this is what's happening, if you inspect the HTTP to-and-fro using tools such as Firebug or Live HTTP Headers (both Firefox extensions) you'll be able to see what's really going on.

Resources