Nginx Installation notes (including PHP support, virtual host, Reverse Proxy Server Load balancer)
2008-10-11 tsengyia # 126.com
Nginx is said to be superior to Apache in terms of high performance and stability, strong concurrent connection processing capability, and low system resource consumption. Currently, more than 2.5 million of web sites are in use
Http://survey.netcraft.com/Reports/200809/
).
######################################## ########################
System Environment: rhel5 [2.6.18-8. el5xen]
Software environment:
Nginx-0.7.17
Lighttpd-1.4.20.tar.gz
Pcre-6.6-1.1
Pcre-devel-6.6-1.1
Php-5.1.6-5.el5
Refer:
Http://sysoev.ru/nginx/nginx-0.7.17.tar.gz
(The latest stable version is 0.6.32)
Http://www.lighttpd.net/download/lighttpd-1.4.20.tar.gz
######################################## ##################################
1. Install the supporting software
1. Install Lighttpd to extract spawn-fcgi (if the site does not contain PHP pages, you can skip SPAW-fcgi and PHP installation)
Shell> tar zxvf lighttpd-1.4.20.tar.gz
Shell> Cd lighttpd-1.4.20/
Shell>./configure & make
Shell> CP-P src/spawn-fcgi/usr/sbin/spawn-fcgi
2. Install PCRE and PHP (the following software)
You can use the RPM package that comes with rhel5 for installation. The installation process is omitted.
Ii. Install nginx
Shell> tar zxvf nginx-0.7.17.tar.gz
Shell> Cd nginx-0.7.17/
Shell>./configure -- prefix =/opt/nginx -- with-http_stub_status_module -- with-http_ssl_module
Shell> make & make install
Shell> ln-SF/opt/nginx/sbin/nginx/usr/sbin/
Iii. nginx Operation Control
1. Check the configuration file for syntax errors.
Shell> nginx-T
2. Start (directly run without any parameters)
Shell> nginx
3. Reload nginx Configuration
Shell> killall-s hup nginx # // or killall-1 nginx
4. Exit nginx after processing the current request
Shell> killall-s quit nginx # // or killall-3 nginx
Iv. nginx configuration Cases
1. General Configuration
Shell> VI/opt/nginx/CONF/nginx. conf
Worker_processes 1; # // Number of worker Processes
Events {
Use epoll; # // Add this event to improve I/O performance
Work_connections 4096;
}
HTTP {
Include mime. types;
Default_types application/octet-stream;
Sendfile on;
Tcp_nodelay on
Keepalive_timeout 60;
Server {
Listen 80; # // set the listening port. do not conflict with other web programs such as Apache.
SERVER_NAME
Www.linux.org
; # // Specify the host name used
Charset UTF-8; # // specify the default encoding of the site file
Location /{
Root HTML; # // set the website root directory
Index index.html;
}
Error_page 500 502 503 x.html
Location =/50x.html {
Root HTML;
}
}
}
2. Add Status Monitoring
Shell> VI/opt/nginx/CONF/nginx. conf # // Add the following content
Location ~ ^/Nginxstatus /{
Stub_status on;
Access_log off;
}
Shell> killall-1 nginx
# // Access through a browser
Http: // nginx_server_ip/nginxstatus/
The status statistics page is displayed. (The three numbers indicate the total number of processed connections, the number of successfully created handshakes, and the total number of processed requests)
3. FastCGI support for PHP
1) Start the FastCGI Service (Use PHP-CGI to process PHP pages, with spawn-fcgi, it is convenient to enable multiple PHP-CGI processes at the same time -- "-c" option to control the number of sub-processes)
Shell & gt;/usr/sbin/spawn-fcgi-A 127.0.0.1-P 9000-F/usr/bin/PHP-CGI-C 10
2) modify the/opt/nginx/CONF/nginx. conf configuration file and add the following content:
Location ~ /. Php $ {
Root HTML;
Fastcgi_pass 127.0.0.1: 9000;
Fastcgi_index index. php;
Fastcgi_param script_filename $ document_root $ fastcgi_script_name;
Include fastcgi_params;
}
3) Reload the configuration
Shell> killall-1 nginx
4. VM settings
Modify the nginx. conf file and add a server {......} You can configure the parameters of each VM.
HTTP {
Server {
Listen 80;
SERVER_NAME
Www.vhost1.com
;
Access_log logs/vhost1.access. Log main;
Location /{
Index index.html;
Root/var/www/vhost1; # // root directory of the web page of 1st Virtual Hosts
}
}
Server {
Listen 80;
SERVER_NAME
Www.vhost2.com
;
Access_log logs/vhost2.access. Log main;
Location /{
Index index.html;
Root/var/www/vhost2; # // webpage root directory of 2nd Virtual Hosts
}
}
}
5. Reverse Proxy-Based Load Balancing
Modify the nginx. conf file, add upstream configuration, specify the IP address and weight of the corresponding server group, and adjust the webpage root directory configuration in the server segment. Distribute HTTP requests to nginx servers to servers in the Web Cluster for processing.
HTTP {
Upstream my_web_cluster {
Servers 192.168.2.11: 8000 Weight = 3;
Servers 192.168.2.12: 8000 Weight = 3;
Server 192.168.2.13: 8000 Weight = 3;
Server 192.168.2.14: 8000 Weight = 3;
Server 192.168.2.15: 8000 Weight = 3;
}
Server {
Listen 80;
SERVER_NAME
Www.domain.com
;
Location /{
Proxy_pass
Http: // my_web_cluster
;
Proxy_set_header X-real-IP $ remote_addr;
}
# // Note: other location configuration segments (for example, for the. php file) need to be commented out, otherwise the redirection of such files may be affected.
}
}
---------------------------------------------------------------------
Reference URL:
Http://wiki.codemongers.com/NginxChsVirtualHostExample
Http://www.ibm.com/developerworks/cn/web/wa-lo-nginx/