Nginx config with some conditionals
root

The root directive is inherited. You have the same statement in the server block, and repeated in a number of location blocks. Only the first statement is necessary. See this document for details.

location ~* ^/static/.*$

This can be replaced by the prefix location:

location /static/ { ... }


The prefix location is more efficient than the regular expression location. The precedence order is different, but that does not affect your current configuration. See this document for details.

rewrite ^(.*)$ https://$http_host$1 permanent;

You can replace this with a return statement, thus eliminating a regular expression.

return 301 https://$http_host$request_uri;


Nested location blocks

A number of statements are common to three location blocks, all of which represent URIs that begin with /static/. The expires, access_log and add_header directives are inherited.

You could restructure your locations as follows:

location /static/ {
    root  /var/www/html/src;
    expires 1y;
    access_log off;
    add_header Cache-Control "public";

    location = /static/site.js {
        try_files /static/$script_file =404;
    }
    location ~* ^/static/img/.+\.(png|jpg)$ {
        add_header Vary Accept;
        add_header Cache-Control "public";
        try_files $uri$webp_suffix $uri =404;
    }
}
location / {
    ...
}


This would improve efficiency as URIs which do not begin with /static/ will not need to be checked against the regular expressions.

The add_header directive has an additional inheritance rule. See this document for details.