NGINX Quickstart
■ Install
■ Basic stuff
■ Configuration
■ Static file server
■ Proxy
■ Load balancing
■ Troubleshooting
In my opinion Nginx is the most useful and configurable server engine. It feels very light but you can make it work like different server components - web server, proxy, load balancer etc. This is a quick start to setup Nginx in a linux machine.
Install
Just run:
sudo apt-get install nginx
And verify:
sudo nginx -v
Basic stuff
Starting:
sudo service nginx start
Restarting:
sudo service nginx reload
Checking status:
sudo service nginx status
Configuration
After installation you can find the server configuration in /etc/nginx/nginx.conf
.
A few important keys are:
worker_processes - No of Nginx workers. Usually this will be set as no of cores of CPU available.
worker_connections - No of connections a single worker should handle at a time.
server - Configuration for one host and port. It has following sub keys:
- listen - port number
- server_name - list of hostnames to listen to
- root - root folder of the content
- index - list of index files
- location - configuration for handling a specific path in the server
upstream - details about the remote servers Nginx might proxy to
Static file server
server {
listen 80;
server_name www.ajinasokan.com ajinasokan.com
root /var/www/html/ajinasokan.com;
index index.html
location / {
alias /var/www/html/ajinasokan.com;
try_files $uri $uri/ =404;
}
}
Proxy
upstream myremoteserver {
server 10.10.20.130
}
server {
listen 80;
server_name www.ajinasokan.com ajinasokan.com
root /var/www/html/ajinasokan.com;
index index.html
location / {
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://myremoteserver/;
}
}
Load balancing
upstream myremoteserver {
server 10.10.20.130
server 10.10.20.131
server 10.10.20.132
server 10.10.20.133
}
server {
listen 80;
server_name www.ajinasokan.com ajinasokan.com
root /var/www/html/ajinasokan.com;
index index.html
location / {
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://myremoteserver/;
}
}
Troubleshooting
Nginx error logs can be found in: /var/log/nginx/nginx_error.log
And access logs can be found in: /var/log/nginx/nginx_access.log
If you are on Mac then logs are in: /usr/local/var/log/nginx