webui: serve via nginx

Serve the mostly pre-compressed static webui content via nginx. If nginx
can't find a static file, proxy the URI back to the rest server.

A follow-up commit will remove code that serves webui from the
phosphor-rest-server.

Tested:
- The webui, rest server and bmcweb seem to be working as before.
- curl -H "Accept-Encoding: gzip" https://<witherspoon>/foo.html
  is served compressed content.
- curl https://<witherspoon>/foo.html is served uncompressed
  content.

Change-Id: Ice443f00a467f3558f28d697261df4f23278f420
Signed-off-by: Deepak Kodihalli <dkodihal@in.ibm.com>
diff --git a/meta-openbmc-machines/meta-openpower/common/recipes-phosphor/webui/phosphor-webui.bbappend b/meta-openbmc-machines/meta-openpower/common/recipes-phosphor/webui/phosphor-webui.bbappend
new file mode 100644
index 0000000..ea81894
--- /dev/null
+++ b/meta-openbmc-machines/meta-openpower/common/recipes-phosphor/webui/phosphor-webui.bbappend
@@ -0,0 +1,16 @@
+do_install_append () {
+
+# The webui content is served as pre-compressed gzip content. While nginx can
+# handle this via the gzip_static directive (and we use that), the nginx
+# try_files directive (which we also use) searches the URI as-is. The suggested
+# workaround is to have an empty uncompressed file with the same name as the
+# compressed file. This does not impact the functionality of gzip_static.
+# So for eg if there's an index.html.gz, create an empty index.html. Same goes
+# for all the webui content.
+# https://serverfault.com/questions/571733/nginx-gzip-static-why-are-the-non-compressed-files-required
+# https://www.ruby-forum.com/topic/4402481
+# https://trac.nginx.org/nginx/ticket/1367
+
+find ${D}${datadir}/www -type f -name '*.gz' -exec sh -c 'touch `dirname "$0"`/`basename "$0" .gz`' '{}' \;
+
+}
diff --git a/meta-openbmc-machines/meta-openpower/meta-ibm/recipes-httpd/nginx/files/nginx.conf b/meta-openbmc-machines/meta-openpower/meta-ibm/recipes-httpd/nginx/files/nginx.conf
index 89e575a..7d65183 100644
--- a/meta-openbmc-machines/meta-openpower/meta-ibm/recipes-httpd/nginx/files/nginx.conf
+++ b/meta-openbmc-machines/meta-openpower/meta-ibm/recipes-httpd/nginx/files/nginx.conf
@@ -65,12 +65,29 @@
         ssl_prefer_server_ciphers   on;
 
         location / {
+                # This location lets us serve the static pre-compressed webui
+                # content (rooted at /usr/share/www). Also if the URI points to
+                # something else (that is unmatched by other locations), we
+                # fallback to the rest server. This approach is based on the
+                # guide at https://docs.nginx.com/nginx/admin-guide/web-server/serving-static-content.
+                root /usr/share/www;
+                # For clients that support gzip encoding, serve them
+                # pre-compressed gzip content. For clients that don't,
+                # uncompress on the BMC. The module gunzip requires
+                # gzip_static to be set to 'always'; gzip_static is the
+                # module that serves compressed content for clients that
+                # support gzip.
+                gunzip on;
+                gzip_static always;
+                try_files $uri $uri/ @rest_server;
+        }
+        location @rest_server {
                 # Use 127.0.0.1 instead of localhost since nginx will
                 # first use ipv6 address of ::1 which the upstream server
                 # is not listening on. This generates an error msg to
                 # the journal. Nginx then uses the 127.0.0.1 and everything
                 # works fine but want to avoid the error msg to the log.
-                proxy_pass http://127.0.0.1:8081/;
+                proxy_pass http://127.0.0.1:8081;
 
                 # WebSocket support
                 proxy_http_version 1.1;
diff --git a/meta-openbmc-machines/meta-openpower/meta-ibm/recipes-httpd/nginx/nginx_%.bbappend b/meta-openbmc-machines/meta-openpower/meta-ibm/recipes-httpd/nginx/nginx_%.bbappend
index 8a58341..8eb7e40 100644
--- a/meta-openbmc-machines/meta-openpower/meta-ibm/recipes-httpd/nginx/nginx_%.bbappend
+++ b/meta-openbmc-machines/meta-openpower/meta-ibm/recipes-httpd/nginx/nginx_%.bbappend
@@ -9,7 +9,7 @@
     file://gen-cert.sh \
     "
 
-EXTRA_OECONF =+ " --without-select_module"
+EXTRA_OECONF =+ " --without-select_module --with-http_gunzip_module"
 
 SSLCERTPATH = "/etc/ssl/certs/nginx/"