简体   繁体   中英

can i just serve all static files except html with ngnix and express

Usually i see people serve html with ngnix with configuration like this

  location / {
       root /usr/share/nginx/html;
       index index.html index.htm;
    }

in my case I use a template engine with express that i don't want ngnix to serve html but instead i want ngnix to serve all static files in the public folder css, js, svgs etc.. public folder

css
 main.css
js
 main.js
svgs
 arrow.svg
 logo.svg
 building.svg

is there is a way to configure ngnix to serve those files and then i can mention them in my pug template like plublic/css/main.css?

full nginx config file

upstream client {
    server client:3000;
}

upstream api {
    server api:5000;
}

server {
    listen 80;

    location / {
        proxy_pass  http://client;
    }
    location /sockjs-node {
        proxy_pass http://client;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
    }

    location /api {
        rewrite /api/(.*) /$1 break;
        proxy_pass  http://api;
    }

}

You can add location block under server context for your static files (with a regex for your static files)

location ~* \.(css|js|svg)$ {
   root /usr/share/nginx/html;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM