Common configuration instances for Nginx

Source: Internet
Author: User

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;  
 }  
  }  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.