Locating Configuration File
Usually located in
/etc/nginx/nginx.conf
A bare configuration file looks like this
events {
}
http {
}
Adding a virtual host
http {
# To include correct mime types to resolve different assets. The `mime.types` file usually exists in the same directory
include mime.types;
server {
# port, can be left out if port 80
listen 80;
# server location
server_name 142.47.92.9;
# directory by which the files will be served
root /sites/demo;
}
}
Routing
Routing can be done via location blocks
server {
listen 80;
server_name 142.47.92.9;
root /sites/demo;
# location matchers can exact `=`
# or regex `~`
# or a prefix ``
# and are prioritized in that order
location = /hello {
return 200 "Hello";
}
}
Simple usage of logic (conditionals and variables)
NGINX provides some variables out of the box, and we can se our own variables and conditionals
server {
listen 80;
server_name 142.47.92.9;
root /sites/demo;
set $weekend 'No';
if ( $date_local ~ 'Saturday|Sunday' ) {
set $weekend 'Yes';
}
location = /is_weekend {
return 200 "Is it a weekend? $weekend\n$date_local";
}
}
Rewrites / Return Directives
The return directive allows to redirect a customer to another URL when given a
3**
status codelocation /logo {
return 307 /blog.css;
}
The rewrite directive allows for manipulating the page URL so it stays the same
server {
listen 80;
server_name 142.47.92.9;
root /sites/demo;
rewrite ^/user/\w+ /greet;
location /greet {
return 200 'Hello User';
}
}
Rewrites also support regular expression with matching Regex groups
server {
listen 80;
server_name 142.47.92.9;
root /sites/demo;
rewrite ^/user/(\w+) /greet/$1;
location /greet {
return 200 'Hello User';
}
location ~ /greet/(\w+) {
return 200 'Hello $1';
}
}
Having more than one rewrite will have the last one override the first
rewrite ^/user/(\w+) /greet/$1;
rewrite ^/user/yamen /blog.css;
In order to only resolve a preceding rewrite, the
last
flag will signal to NGINX to not resolve any follow-up rewrite for the same matcherrewrite ^/user/(\w+) /greet/$1 last;
rewrite ^/user/yamen /blog.css;
Β
Try-Files Directive
The
try_files
directive tells NGINX to try serving files in the provided order until one is found and is servedserver {
listen 80;
server_name 142.47.92.9;
root /sites/demo;
try_files /blog.css /greet;
location /greet {
return 200 'Hello User';
}
location ~ /greet/(\w+) {
return 200 'Hello $1';
}
}
To try matching the original request first, a variable
$uri
can be provided as a first argumenttry_files $uri /blog.css /greet;
This makes the
try_files
directive ideal for serving 404 pagesserver {
listen 80;
server_name 142.47.92.9;
root /sites/demo;
try_files $uri /friendly_404;
location /greet {
return 200 'Hello User';
}
location /friendly_404 {
return 404 "Sorry, this file does not exist";
}
}
Named Locations
Locations can be named to be referenced later when they don't need to have an actual URL, building on the previous example:
server {
listen 80;
server_name 142.47.92.9;
root /sites/demo;
try_files $uri @friendly_404;
location /greet {
return 200 'Hello User';
}
location @friendly_404 {
return 404 "Sorry, this file does not exist";
}
}
Β
To validate configuration file syntax, run the command:
nginx -t
The NGINX server needs to be reloaded for changes to the configuration file to take effect
systemctl reload nginx
Or
nginx -s reload
Loading Comments...