NGinX’s main configuration file is usually located in /etc/nginx/nginx.conf
. You can configure server blocks (similar to virtual hosts in Apache) to host multiple domains:
server {
listen 80;
server_name linux-server-admin.com www.linux-server-admin.com;
root /var/www/linux-server-admin;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
To secure your website with SSL, you need to configure NGinX to use SSL certificates. Here is an example configuration:
server {
listen 443 ssl;
server_name linux-server-admin.com www.linux-server-admin.com;
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
root /var/www/linux-server-admin;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
NGinX can also be used as a load balancer to distribute traffic across multiple servers:
upstream backend {
server backend1.example.com;
server backend2.example.com;
}
server {
listen 80;
server_name linux-server-admin.com;
location / {
proxy_pass http://backend;
}
}
To improve performance, you can enable caching in NGinX:
http {
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off;
server {
location / {
proxy_cache my_cache;
proxy_pass http://backend;
}
}
}