Serving Domain Static Files via Nginx on a VPS Server
To serve static files on NodeJS or on Express, a static server is set up. In fact, this is not recommended, as it performs slowly.
It is more optimal to serve static files
via Nginx. For this, a special configuration
is made for the domain,
containing a regex and the path to
the static folder. For example,
let the static folder in our domain
be the public folder.
In this case, we will write
the following settings:
server_name test1.com;
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|js|swf|txt|ico)$ {
root /var/www/test1.com/public;
}
It is somewhat inconvenient that the domain name must be written twice, if this name matches the site folder (and for convenience, it matches for us). The problem can be solved as follows:
server_name test1.com;
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|js|swf|txt|ico)$ {
root /var/www/$server_name/public;
}
Let's gather all our settings together and get the following:
server {
listen 80;
server_name test1.com;
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|js|swf|txt|ico)$ {
root /var/www/$server_name/public;
}
location / {
proxy_pass http://localhost:3001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
Configure static file serving for both test sites. Test it via your browser.