Q
Q
QQ2016-03-13 19:55:40
Nginx
QQ, 2016-03-13 19:55:40

Who is better to entrust the processing of statics, expressJS or Nginx?

Hello.
Actually this is the question.
Statics in expressJS:

var theme = 'MyTheme';
app.use(express.static(path.join(__dirname, 'themes', theme, 'public')));

Statics in Nginx:
location ~* \.(jpg|jpeg|gif|ico|png|css|js|xml|rss|txt|svg|eot|ttf|woff|woff2)$ {
            root /home/example.com/themes/??????????/public;
            access_log off;
            autoindex off;
            expires 30d;
    }

Actually, how to handle statics in Nginx, since it does not know what topic is currently active on the site? Or let expressJS handle everything?
And yet, such a nuisance in Nginx that when you open app.js (example.com/app.js) via the Web, it opens it as a regular file, how to hide it?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
E
Eugene, 2016-03-13
@Nc_Soft

Yes, no one ever leaves express hanging around on the web. They always proxy through nginx, and giving statics as a backend is generally nonsense.

T
Timur Shemsedinov, 2016-03-13
@MarcusAurelius

The static is given through the CDN, and express, which is sticking out with a bare ass, which is hiding behind a sticking out nginx, is only suitable for prototyping.

S
sim3x, 2016-03-13
@sim3x

Making a symlink to an active topic
How to install nodejs
https://nodejs.org/en/download/package-manager/#de...
How to set up nginx <-> nodejs
https://www.digitalocean.com/community/tutorials/h...

V
Valery Ryaboshapko, 2016-03-14
@valerium

White people solve this problem in this way. The backend (in your case, expressJS) generates links to statics in such a way that they contain an indication of the topic, for example /static/my_theme/css/style.css. In the nginx config it is written something like this:

location /static/ {
            root /home/example.com/static/;
            access_log off;
            autoindex off;
            expires 30d;
    }

Obviously, /home/example.com/static/only public files are in the folder, all JavaScript should be in other folders.
Thus, the backend does not stick out, nginx successfully performs the work for which it is intended, GET requests remain idempotent, and you still have the opportunity to move the statics to the CDN without too much pain if necessary.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question