Nginx is a versatile web server that offers various capabilities beyond being a reverse proxy, such as load balancing, SSL/TLS termination, and caching. It can serve as an API gateway, providing advanced routing, filtering, and security features. Nginx supports content compression, media streaming, and WebSockets proxying, making it suitable for a wide range of applications. Additionally, Nginx can act as a content delivery network (CDN) and a web application firewall (WAF), helping optimize performance and enhance security for web applications and services. Its flexibility and performance make it a popular choice for many use cases.
Nginx Conf
http { # this is called as context
include mime.types; # this is directive
server { # child context under parent http context
listen 80; # need to mention on which port you need to listen
server_name ip/domain; # specify server where application is running
root /path/to/project/folder; # specify project folder in server
}
}
here, whatever under /var/www/demo will be served, by default it picks only index.html
http {
include mime.types;
server {
listen 80;
server_name ip/domain;
root /var/www/demo;
location /new { #prefix match
return 200 "hi i am from new"; # 200 is response code
}
location = /new { #exact match
return 200 "hi i am from new exact match";
}
location ~* /new[0-9] { #REGX match
return 200 "hi i am from new REGX match";
}
location ^~ /New { #prefential prefix match
return 200 "hi i am from new prefential prefix match";
}
location /fruits { #here it will look for fruits folder under /var/www/demo/ and will serve index.html
root /var/www/demo/
}
location /veg { #here it will server index.html of fruits folder if path is veg
alias /var/www/demo/fruits
}
location /crops {
root /var/www/demo;
try_files /crops/crops.html /index/html =404; #by default it will server index.html to server other, use try_files
#if crops.html not present it will server /var/www/demo/index.html
# if index.html not present it will give 404 error
}
}
}
Prefix match will load its content anything that starts with "new"
here, the url is http://54.226.209.32/new1234/random but it stll provides response as it matches "new"
here we get error, when the path is apart from "new" unlike in prefix match
here the only condition is "new" followed by any number.
Exact match > Prefential prefix > REGX match > Prefix match is the order of priority
here, Prefential prefix is same as Prefix match but it has got higher priority over REGX match.
simillar to alias there is rewrite, but they have diffrent purpose and operate at diffrent levels within nginx conf.
Load Balancer
events {
# Event settings can go here if needed
}
http {
# Include mime types for content type determination
include mime.types;
# Define the upstream group (backend servers) for load balancing
upstream backendserver {
server 54.226.209.32:1111; # Docker container running on port 1111
server 54.226.209.32:4444; # Docker container running on port 4444
}
# Define the server block
server {
listen 80; # Listen on port 80
server_name 54.226.209.32; # Replace with your server IP or domain
location / {
# Handle requests for load balancing
proxy_pass http://backendserver;
proxy_set_header Host $host; # Set the host header
proxy_set_header X-Real-IP $remote_addr; # Forward the client's IP address
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
Thank you