Nginx
Configurations
Nginx uses a modular setup. Configs are typically stored in /etc/nginx/*.
Main Config
The main configuration file is at /etc/nginx/nginx.conf . It contains some primary configurations and then the most important is include lines at the bottom.
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
Under static site deployment, Nginx is responsible for finding files on the disk. The root directive (base path) tells it where all the website files live. So for example.com/about.html, it starts at the root directory and then proceeds from there. So the path and document root make up the root parameter.
server {
root /var/www/example/public
# /var/www/example + /public
}
Typically, only the static assets such as images/fonts are held in /public. The actual pages are in the folders src/app outside of /public. Nginx checks the /public folder first and then checks proxy_pass.
Security Config
Additional security configurations are typically included at /etc/nginx/(snippet/security).
CDN setup
Setting up a cdn.example.com, a subdomain, is for offloading. The static assets are moved to this dedicated subdomain thus you can reduce the number of worker threads for file delivery. The CDN caches the assets and distributes them. It is a transparent cache.
The CDN setup is as follows:
- User browser sees a tag like <img src=“https://cdn.example.com”>
- The request goes to the nearest CDN edge server.
- If the edge server doesn’t have the image, the edge server will ask (pull) the webserver for it. You are the origin.
- The CDN then caches the copy and hands it off to the user.
- Every request afterwards uses the CDN copy.
This is origin-pull system.
CDN Setup
For the setup to work, the CDN must be configured. So then
-
User browser sees the url www.example.com or http://www.example.com. Both of these will be routed by the DNS records. So you will need multiple CNAME and an A record to make sure it gets routed correctly. Where
-
The user