trouble with static content (Revised)

Francis Daly francis at daoine.org
Sun Nov 7 17:28:35 UTC 2021


On Sun, Nov 07, 2021 at 11:00:39AM -0500, deeztek wrote:

Hi there,

> >The config above says that nginx should serve something like the file
> >/usr/local/nginx/html/static/js/vendor.d0bc79df.js in response to the
> >first request. The actual response is a 404. Does that file exist?
> 
> The specific /static/js/vendor.d0bc79df.js files does not exist in
> /usr/local/nginx/html directory. It's served by the service
> http://127.0.0.1:9091 and I don't know exactly the path in that service. All
> I can see is the /static/js/vendor.d0bc79df.js request.

Ok, that makes sense. You do not want nginx to serve this request from
the filesystem; you want nginx to proxy_pass the request to port 9091.

> To expand further, the /static/js/vendor.d0bc79df.js works when I remove the
> following from the config:
> 
> location ~* .(?:ico|css|js|gif|jpe?g|png|woff2|map)$ {
> expires max;
> }

Yes, that makes sense too. Without that location{} block, the request
is handled in "location /", which does the right thing.

> To give you a bit of background. The /admin location is served by Lucee/CFML
> server running on Tomcat on port 8888. Everything I read says to include the
> following location in order for Nginx to serve .ico, .css. js etc files:

Everything you read has misled you.

What everything you read should have said, is something like "in order to get
nginx to serve all matching requests from the filesystem, include this location".

> location ~* .(?:ico|css|js|gif|jpe?g|png|woff2|map)$ {
> expires max;
> }

Right now, I'm not sure if you want nginx to serve some matching requests
from the filesystem, or no matching requests from the filesystem -- you
seem to have confirmed that you do not want nginx to serve all matching
requests from the filesystem.

> Please see here:
> http://dnando.github.io/blog/2015/01/05/advantages-of-nginx/

That seems to describe a reasonable way to have exactly one proxy_pass
within a server{}, and have all "static" requests served from the filesystem.

You are trying to have two proxy_pass:es within a server{}, which is
not the same thing.

That can still work; but you need to decide how you want nginx to handle
each request, and then you need to tell nginx how you want nginx to
handle each request.

> So, when I include the location ~* the application that lives on port 8888
> works fine, HOWEVER, the application that lives on 9091 breaks. If I
> remove:
> 
> location ~* .(?:ico|css|js|gif|jpe?g|png|woff2|map)$ {
> expires max;
> }
> 
> Then, the application that lives on port 9091 works but the application on
> port 8888 breaks.

Correct. With that "location ~*", you are telling nginx that every
request that ends in js should be served from the filesytem.

But that is not what you want nginx to do.

> So, the way I'm figuring, I need a way to specify a /admin specific location
> for static files (.ico, .css, .js etc) since they are not being served by
> Nginx unless I have a specific directive for those type of files for the
> application in the /admin location.
> 
> Hope that makes more sense.

One way could be to have

    location ~* ^/admin/.*(?:ico|css|js|gif|jpe?g|png|woff2|map)$ {

to regex-match all requests that start with "/admin/" and which end with
things like "js" (case-insensitively).

Another way could be to move the current 

    location ~* .(?:ico|css|js|gif|jpe?g|png|woff2|map)$ {

to be inside the "location /admin".

(I prefer the second, because I dislike regex locations at the top level.)

See https://nginx.org/r/location, for example. You have configurations B,
C, and E; but the E that you have is not the E that you want.

That page mentions, but does not describe in detail nested locations; they
generally Do The Right Thing, which is probably why they are not expanded there.

Good luck with it,

	f
-- 
Francis Daly        francis at daoine.org


More information about the nginx mailing list