Serve pre-compressed static files via NGINX Reverse Proxy
Hello,
I'm using NGINX as a Reverse Proxy with Caching enabled in front of Apache by following the default setup offered by NGINX Manager of cPanel.
I would like to server pre-compressed files found at a specific directory under the document root (pub/static). I have already configured NGINX with the property "gzip_static on;". However, NGINX does not access the document root of Apache. Instead, it forwards the requests to Apache. So, when NGINX requests the file "my-file.css", Apache responds with that very file, and not "my-file.css.gz".
-
Hey there! Is this a specific document root under a user, such as /home/username/public_html/static or is this outside of the typical user directory structure?
We have some details at https://docs.cpanel.net/knowledge-base/nginx/customize-reverse-proxy-nginx-configurations/#server-blocks which also references https://nginx.org/en/docs/beginners_guide.html#static - is that what you're looking for?
0 -
Hello,
It is a specific document root under a user "/home/myuser/public_html/dev245/pub", and the directory with the static files is "/home/myuser/public_html/dev245/pub/static".
The automatically generated configuration file is located at "/etc/nginx/conf.d/users/myuser.conf" and contains a SERVER block per domain such as the below. The question is how I can customize the configuration in a way that the files under the location "/home/myuser/public_html/dev245/pub/static" are served by NGINX directly.
server {
server_name dev2.mydomain.com www.dev2.mydomain.com;
listen 80;
listen [::]:80;
include conf.d/includes-optional/cloudflare.conf;
set $CPANEL_APACHE_PROXY_PASS $scheme://apache_backend_${scheme}_88_288_288_78;
# For includes:
set $CPANEL_APACHE_PROXY_IP 88.288.288.78;
set $CPANEL_APACHE_PROXY_SSL_IP 88.288.288.78;
set $CPANEL_SERVICE_SUBDOMAIN 0;
set $CPANEL_PROXY_CACHE myuser;
set $CPANEL_SKIP_PROXY_CACHING 0;
listen 443 ssl;
listen [::]:443 ssl;
http2 on;
ssl_certificate /var/cpanel/ssl/apache_tls/dev2.mydomain.com/combined;
ssl_certificate_key /var/cpanel/ssl/apache_tls/dev2.mydomain.com/combined;
ssl_protocols TLSv1.2 TLSv1.3;
proxy_ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers :ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-XXXXXXXXXXXXXXXXXXXXXXXXXXXX_SHA256:TLS_AES_128_GCM_SHA256;
proxy_ssl_ciphers :ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-YYYYYYYYYYYYYYYYYYYYYYYYY_POLY1305_SHA256:TLS_AES_128_GCM_SHA256;
root "/home/myuser/public_html/dev245/pub";
location /cpanelwebcall {
include conf.d/includes-optional/cpanel-proxy.conf;
proxy_pass http://127.0.0.1:2082/cpanelwebcall;
}
location /Microsoft-Server-ActiveSync {
include conf.d/includes-optional/cpanel-proxy.conf;
proxy_pass http://127.0.0.1:2090/Microsoft-Server-ActiveSync;
}
location = /favicon.ico {
allow all;
log_not_found off;
access_log off;
include conf.d/includes-optional/cpanel-proxy.conf;
proxy_pass $CPANEL_APACHE_PROXY_PASS;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
include conf.d/includes-optional/cpanel-proxy.conf;
proxy_pass $CPANEL_APACHE_PROXY_PASS;
}
location / {
# has basic auth, so disable cache:
proxy_cache off;
proxy_no_cache 1;
proxy_cache_bypass 1;
proxy_cache_valid 200 12d;
proxy_cache_valid 301 12d;
proxy_cache_valid 302 12d;
proxy_cache_use_stale error timeout http_429 http_500 http_502 http_503 http_504;
proxy_cache_background_update on;
proxy_cache_revalidate on;
proxy_cache_min_uses 1;
proxy_cache_lock off;
include conf.d/includes-optional/cpanel-proxy.conf;
proxy_pass $CPANEL_APACHE_PROXY_PASS;
}
location "/media/myfeed" {
# has basic auth, so disable cache:
proxy_cache off;
proxy_no_cache 1;
proxy_cache_bypass 1;
include conf.d/includes-optional/cpanel-proxy.conf;
proxy_pass $CPANEL_APACHE_PROXY_PASS;
}
include conf.d/server-includes/*.conf;
include conf.d/users/myuser/*.conf;
include conf.d/users/myuser/dev2.mydomain.com/*.conf;
}0 -
Thanks for the clarification. You can do the following in the custom configuration file to add your block of code:
/etc/nginx/conf.d/users/username/domainname/*.confYou'll note that path is included in all the vhost entries on the system, so this can apply to any user.0 -
Hello,
I would like to clarify whether it eventually makes sense to serve static files directly by NGINX Reverse Proxy given that I have enabled NGINX caching, as well, via NGINX Manager. I mean that upon the initial request, NGINX requests a CSS file from Apache. Afterwards, it is supposed that NGINX caches the CSS file. So, NGINX will normally serve subsequent requests for the same CSS file from its cache and not by requesting that from Apache. Is this the case?
If this is not the case, can you provide the code I should place in the file "/etc/nginx/conf.d/users/username/domainname/*.conf" so that NGINX Reverse Proxy serves files located under the directory "/home/myuser/public_html/dev245/pub/static" directly, and not via Apache?
0 -
That's kind of the beautiful/automated part - the files that are getting used will be cached anyway after their first access, and the files that aren't used as much won't, so I'm not sure this is worth worrying about, ultimately, since they'll get cached anyway if requested.
0 -
OK. That's good. Can you provide the configuration code I should place in the file "/etc/nginx/conf.d/users/username/domainname/*.conf" so that NGINX Reverse Proxy serves the files located under the directory "/home/myuser/public_html/dev245/pub/static" directly, and not via Apache?
I checked the cPanel documentation and there are not any examples so that I understand how NGINX works on the cPanel server. On the other hand, changing the configuration of NGINX is a global change, and I prefer not to experiment.
0 -
Since this will just affect one user you don't need to worry about this being a "global" change. You'd want to add the following block:
location /static { root /home/username/public_html/dev245/pub; }so that user's custom config you're creating in /etc/nginx/conf.d/users/username/domainname/*.conf
0 -
Thanks for the hint. I made the configuration you proposed with some modifications associated with Magento website I'm running, and it worked fine.
It is said that NGINX is pretty fast for serving static content (CSS, JS, JPG, PNG etc). So, for a setup in which NGINX works as a Reverse Proxy with Caching enabled in front of Apache, it is recommended that NGINX serves the static content directly; i.e. without requesting that from Apache, as it happens with the dynamic content. Indeed for CSS, JS files specifically, I have also generated the pre-compressed files with both GZIP and BROTLI algorithms. So, NGINX will directly serve these pre-compressed files depending on the accepted encoding of the visitor's browser.
Now the question is this: Will the above setup lower the TTFB?
I would like to avoid making my Magento implementation more complicated without a noticeable benefit.
0 -
TTFB is always a tricky thing to improve on - the only real way to answer that is to try it and find out!
0
Please sign in to leave a comment.
Comments
9 comments