summary refs log tree commit diff
path: root/pkgs/servers/http/nginx
diff options
context:
space:
mode:
authorYegor Timoshenko <yegortimoshenko@riseup.net>2018-10-13 21:45:25 +0000
committeraszlig <aszlig@nix.build>2019-04-18 09:40:06 +0200
commit135d54f5358aaf2c71bea0a1ce3cdaaadb7a5222 (patch)
tree4fa854c9176a5ab3cdf08bebe15a9ac49c1367d7 /pkgs/servers/http/nginx
parentb050097edfc54cf7c2b5bd655b4edfdfb2e98757 (diff)
downloadnixpkgs-135d54f5358aaf2c71bea0a1ce3cdaaadb7a5222.tar
nixpkgs-135d54f5358aaf2c71bea0a1ce3cdaaadb7a5222.tar.gz
nixpkgs-135d54f5358aaf2c71bea0a1ce3cdaaadb7a5222.tar.bz2
nixpkgs-135d54f5358aaf2c71bea0a1ce3cdaaadb7a5222.tar.lz
nixpkgs-135d54f5358aaf2c71bea0a1ce3cdaaadb7a5222.tar.xz
nixpkgs-135d54f5358aaf2c71bea0a1ce3cdaaadb7a5222.tar.zst
nixpkgs-135d54f5358aaf2c71bea0a1ce3cdaaadb7a5222.zip
nginx: if root is in Nix store, use path's hash as ETag
Resolves #25485. Usage example:

$ realpath /var/www
/nix/store/wnrhnnpdj3x50j5xz38zp1qxs1ygwccw-site
$ curl --head localhost
HTTP/1.1 200 OK
Server: nginx
Date: Fri, 28 Sep 2018 06:09:25 GMT
Content-Type: text/html
Content-Length: 50
Last-Modified: Thu, 01 Jan 1970 00:00:01 GMT
Connection: keep-alive
ETag: "wnrhnnpdj3x50j5xz38zp1qxs1ygwccw"
Accept-Ranges: bytes
Diffstat (limited to 'pkgs/servers/http/nginx')
-rw-r--r--pkgs/servers/http/nginx/generic.nix4
-rw-r--r--pkgs/servers/http/nginx/nix-etag-1.15.4.patch87
2 files changed, 90 insertions, 1 deletions
diff --git a/pkgs/servers/http/nginx/generic.nix b/pkgs/servers/http/nginx/generic.nix
index 02bef43c184..fa7864886dc 100644
--- a/pkgs/servers/http/nginx/generic.nix
+++ b/pkgs/servers/http/nginx/generic.nix
@@ -75,7 +75,9 @@ stdenv.mkDerivation {
 
   preConfigure = (concatMapStringsSep "\n" (mod: mod.preConfigure or "") modules);
 
-  patches = stdenv.lib.optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
+  patches = [
+    ./nix-etag-1.15.4.patch
+  ] ++ stdenv.lib.optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
     (fetchpatch {
       url = "https://raw.githubusercontent.com/openwrt/packages/master/net/nginx/patches/102-sizeof_test_fix.patch";
       sha256 = "0i2k30ac8d7inj9l6bl0684kjglam2f68z8lf3xggcc2i5wzhh8a";
diff --git a/pkgs/servers/http/nginx/nix-etag-1.15.4.patch b/pkgs/servers/http/nginx/nix-etag-1.15.4.patch
new file mode 100644
index 00000000000..7415b4e15d0
--- /dev/null
+++ b/pkgs/servers/http/nginx/nix-etag-1.15.4.patch
@@ -0,0 +1,87 @@
+From 3ed81ef9e973247d0c95bf240b3bd2e640f2605a Mon Sep 17 00:00:00 2001
+From: Yegor Timoshenko <yegortimoshenko@riseup.net>
+Date: Fri, 28 Sep 2018 03:27:04 +0000
+Subject: [PATCH] If root is in Nix store, set ETag to its path hash
+
+---
+ src/http/ngx_http_core_module.c | 56 +++++++++++++++++++++++++++------
+ 1 file changed, 47 insertions(+), 9 deletions(-)
+
+diff --git a/src/http/ngx_http_core_module.c b/src/http/ngx_http_core_module.c
+index c57ec00c..d996d7e3 100644
+--- a/src/http/ngx_http_core_module.c
++++ b/src/http/ngx_http_core_module.c
+@@ -1583,6 +1583,7 @@ ngx_http_set_etag(ngx_http_request_t *r)
+ {
+     ngx_table_elt_t           *etag;
+     ngx_http_core_loc_conf_t  *clcf;
++    u_char                    *real, *ptr1, *ptr2;
+ 
+     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
+ 
+@@ -1598,16 +1599,53 @@ ngx_http_set_etag(ngx_http_request_t *r)
+     etag->hash = 1;
+     ngx_str_set(&etag->key, "ETag");
+ 
+-    etag->value.data = ngx_pnalloc(r->pool, NGX_OFF_T_LEN + NGX_TIME_T_LEN + 3);
+-    if (etag->value.data == NULL) {
+-        etag->hash = 0;
+-        return NGX_ERROR;
+-    }
++    real = ngx_realpath(clcf->root.data, NULL);
++
++    #define NIX_STORE_DIR "/nix/store"
++    #define NIX_STORE_LEN ngx_strlen(NIX_STORE_DIR)
++
++    if (!ngx_strncmp(real, NIX_STORE_DIR, NIX_STORE_LEN)
++        && real[NIX_STORE_LEN] == '/'
++        && real[NIX_STORE_LEN + 1] != '\0')
++    {
++         ptr1 = real + NIX_STORE_LEN;
++        *ptr1 = '"';
++
++        ptr2 = (u_char *) ngx_strchr(ptr1, '-');
++
++        if (ptr2 == NULL) {
++            ngx_free(real);
++            etag->hash = 0;
++            return NGX_ERROR;
++        }
++
++        *ptr2++ = '"';
++        *ptr2 = '\0';
+ 
+-    etag->value.len = ngx_sprintf(etag->value.data, "\"%xT-%xO\"",
+-                                  r->headers_out.last_modified_time,
+-                                  r->headers_out.content_length_n)
+-                      - etag->value.data;
++        etag->value.len = ngx_strlen(ptr1);
++        etag->value.data = ngx_pnalloc(r->pool, etag->value.len);
++
++        if (etag->value.data == NULL) {
++            ngx_free(real);
++            etag->hash = 0;
++            return NGX_ERROR;
++        }
++
++        ngx_memcpy(etag->value.data, ptr1, etag->value.len);
++    } else {
++        etag->value.data = ngx_pnalloc(r->pool, NGX_OFF_T_LEN + NGX_TIME_T_LEN + 3);
++
++        if (etag->value.data == NULL) {
++            ngx_free(real);
++            etag->hash = 0;
++            return NGX_ERROR;
++        }
++
++        etag->value.len = ngx_sprintf(etag->value.data, "\"%xT-%xO\"",
++                                      r->headers_out.last_modified_time,
++                                      r->headers_out.content_length_n)
++                          - etag->value.data;
++    }
+ 
+     r->headers_out.etag = etag;
+ 
+-- 
+2.18.0
+