NGINX as Webserver
New to NGINX? Start by reading NGINX introduction.
Notes
webserver is something that serves static contents (HTML, media, etc.)
NGINX was primarily introduced as a webserver
NGINX can server from different locations and in different ports at the same time
default site is at
/etc/nginx/site-available/defaultbut inferring from the previous point, we can add multiple site configuration files at/etc/nginx/sites-availableeach serving on different ports or even different domains such as:/etc/nginx/sites-available/ ├── default ├── example.com └── mysite.com/etc/nginx/sites-availablejust makes site available but not ready to serve, for the site be ready to serve, there should be symlink at/etc/nginx/sites-enabledof each site which is ready to serve content:sudo ln -s /etc/nginx/sites-available/mysite.com /etc/nginx/sites-enabled/which will create a symblink as such:
lrwxrwxrwx 1 root root 37 Oct 7 11:52 /etc/nginx/sites-enabled/mysite.com -> /etc/nginx/sites-available/mysite.comWhy Symlink?
- changes made to
/etc/nginx/sites-available/mysite.comare directly reflected on/etc/nginx/sites-enabled/mysite.com - symlink is just a pointer which means it saves storage
- changes made to
generally in production, there will mostly be only one webserver with a single domain but multi server architecture helps easy develop and test sites/apps
Serving Static Content
The general flow is by start adding config file at
/etc/nginx/sites-available/and enabling by symlink at/etc/nginx/sites-enabled/when ready. So, first:sudo mkdir /etc/nginx/sites-available/mysite.comThe most basic skeleton to write configuration for NGINX webserver is:
http { server { location / { } } }However
httpblock can be omitted and justserver {}is fine too.Add simple
rootdirective tolocationblock directive:location / { root /data/www; }When requests comes at
mysite.com/ormysite.com, serve from/data/www.This means
data/wwwshould exist and the default HTML file i.e.index.htmlfile should exist there to start serving at/:sudo mkdir -p /data/www/ echo "HI from mysite.com" | sudo tee /data/www/mysite.com sudo chown -R www-data:www-data /data/www/Changing owner is not mandatory (because NGINX is started as
sudo nginx) but a good pratice because generally NGINX user and group iswww-dataand notroot.Add more
locationdirective if necessary, for example to serve/imagesfrom another location such as/media/images/:location /images/ { root /media/images/; }When requests comes at
mysite.com/images/picture.png, return the image from/media/images/picture.pngif available.Specify listening
address:portinside the server block (generally the first line). This is necessary because default site is already using default port80:http { server { listen 8090; ... } }Here
addresspart is omitted so universal address0.0.0.0is applied.[Optional] Seperating
access.loganderror.logfor this site only is also a good idea:http { server { ... access_log /var/log/nginx/mysite.com/access.log; error_log /var/log/nginx/mysite.com/error.log; } }These file should exist before
nginxis started, so the same idea goes here:sudo mkdir /var/log/nginx/mysite.com/ sudo touch /var/log/nginx/mysite.com/{access,error}.log sudo chown -R www-data:adm /var/log/nginx/mysite.com/Why
www-data:adm? Because it is common practice for log files.Create symlink of
mysite.comin/etc/nginx/sites-enabled:sudo ln -s /etc/nginx/sites-available/mysite.com /etc/nginx/sites-enabled/Validate and start serving:
sudo nginx -t && sudo nginxAccess your site:
curl localhost:8090should return:
HI from mysite.comBecause the site is accessed, now
/var/log/nginx/mysite.com/access.logdoes have some access logs, see it yourself.
Important Notes
- naming
mysite.comdoesn’t mean NGINX server is allowed to serve asmysite.com - if you want to serve as
mysite.compublicly, you should own the domain in order to serve asmysite.combecuase you need to set A or CNAME DNS records (from the domain name provider console such as: cloudflare or godaddy) to mapmysite.comto your public IP address - continuing to previous point, you should also set
server_namedirective asmysite.cominsideserver {}block -> this only solves serving ashttp://mysite.com - to serve HTTPS, you need to have TLS certificate which is signed by trusted CA and mention it in NGINX configuration (
ssldirective)
Important
Everything mentioned on important notes is not necessary for private site (not served publicly) because:
- You can easily change A record of DNS server you own so that
mysite.compoint to the webserver IP address. - Even if HTTPS required, you can self signed TLS cert with
openssl-> game over.