Express js app with nginx - conflict with static files while serving subfolder

upstream app {
    server localhost:3000;
}

server {
    ...
    # If I comment this location out, images are displayed on the website
    location ~* \.(?:jpg|jpeg|png|gif|swf|xml|txt|css|js)$ {

        expires 6004800;
        add_header Pragma public;
        add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    }
    ...
    location /app {
        alias /path/to/app/public/; 
        try_files $uri $uri @app;
    }

    location @app {
        rewrite /app(.*) $1 break;

        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $proxy_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://app;
        proxy_redirect http://app/ /app/;
    }
    ...
}

      

I have been struggling with this for some time now. I have an express application in a subfolder under nginx. Above is the code in the nginx file at / sites -available /. When I remove the location for static files, the images and css of the application are proxied, but if the cache of the static files is in the nginx file, the images and css of the express application are not displayed on the website.

Can anyone help please?

+3


source to share


1 answer


Regex placement takes precedence over location prefix blocks in handling nginx requests. Below are relevant excerpts from nigx documentation.

I highly recommend you read them carefully as many people don't and don't know the basics.

A few examples before understanding the keywords:

  • prefix location: location /toto { [...] }

  • regex location: location ~ /toto { [...] }

[...]

To find a location that matches a given request, nginx first checks the locations specified with prefix strings (prefix locations). Among them, the location with the longest matching prefix is โ€‹โ€‹selected and stored. The regular expressions are then checked in the order they appear in the configuration file. The regular expression search ends at the first match, and the matching configuration is used. If no match is found with the regular expression, the previously remembered prefix location configuration is used.

[...]

If the longest matching prefix location has the "^ ~" modifier, the regular expressions are not checked.

[...]

Also, using the "=" modifier, you can determine the exact match between URI and location. If an exact match is found, the search stops. [...]

Several other examples illustrating two operators that change the order of finding a location:



  • location ^~ /toto { [...] }

    : prefix location with higher priority than regex location.
  • location = /toto { [...] }

    : exact prefix location (exact match, highest priority)

To summarize, a list of priorities during location selection for the URI of the incoming request:

  • location = /too

  • location ^~ /toto

  • location ~ /toto

  • location /toto

Thus, a cleaner way to solve your problem:

location ^~ /app {
    alias /path/to/app/public/; 
    try_files $uri $uri @app;
}

      

+3


source







All Articles