Nginx as an HTTP server, in terms of functional implementation and performance are very outstanding, can be comparable to Apache, can almost achieve all the functions of Apache, the following describes some nginx commonly used configuration examples, including virtual host configuration, load balance configuration, Anti-Theft chain configuration and log management.
Example of a virtual host configuration
The following is the creation of three virtual hosts in Nginx, which is illustrated by the listing of the virtual Host configuration section only.
HTTP {
server {
listen ;
server_name www.domain1.com;
Access_log Logs/domain1.access.log main;
Location/{
index index.html;
Root /web/www/domain1.com/htdocs;
}
}
server {
listen ;
server_name www.domain2.com;
Access_log Logs/domain2.access.log main;
Location/{
index index.html;
Root /web/www/domain2.com/htdocs;
}
}
Include /opt/nginx/conf/vhosts/www.domain2.com.conf;
}
The include directive is used here, where the contents of the/opt/nginx/conf/vhosts/www.domain2.com.conf are:
server {
listen ;
server_name www.domain3.com;
Access_log Logs/domain3.access.log main;
Location/{
index index.html;
Root /web/www/domain3.com/htdocs;
}
}
Second, load balance configuration example
The following is configured with a Nginx load balancing server via the Nginx reverse proxy feature. The back end has three service nodes to provide Web services and achieve load balancing of three nodes through Nginx scheduling.
HTTP
{
upstream myserver {
server 192.168.12.181:80 weight=3 max_fails=3 fail_timeout=20s ;
Server 192.168.12.182:80 weight=1 max_fails=3 fail_timeout=20s;
Server 192.168.12.183:80 weight=4 max_fails=3 fail_timeout=20s;
}
Server
{
listen ;
server_name www.domain.com 192.168.12.189;
Index index.htm index.html;
Root /ixdba/web/wwwroot;
Location/{
Proxy_pass http://myserver;
Proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;
Include /opt/nginx/conf/proxy.conf;
}
}