Nginx installation notes (including PHP support, virtual host, reverse proxy load Balancing) _nginx

Source: Internet
Author: User
Tags php language nginx server

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
Reference Download Address:
Http://sysoev.ru/nginx/nginx-0.7.17.tar.gz (latest stable version for 0.6.32)
Http://www.lighttpd.net/download/lighttpd-1.4.20.tar.gz
##########################################################################
First, install the support software
1, install lighttpd to extract spawn-fcgi (if the site does not contain PHP page, you can not install spaw-fcgi, PHP)
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 RHEL5 with the RPM package installed, the process is slightly.

Second, 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/

Three, nginx operation control
1. Check the configuration file for grammatical errors
Shell> nginx-t
2, start (without any parameters to run directly)
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 Use Cases
1. General Configuration
Shell> vi/opt/nginx/conf/nginx.conf
Worker_processes 1; #//number of worker processes
Events {
Use Epoll; #//increase 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, be careful not to conflict with other web programs such as Apache
server_name www.linux.org; #//Specifies the host name to use
CharSet Utf-8; #//Specifies the default encoding for a site file
Location/{
root HTML; #//set Web site root directory
Index index.html index.html;
}
Error_page 502 503 504/50x.html
Location =/50x.html {
root HTML;
}
}
}
2. Add Status Monitor
Shell> vi/opt/nginx/conf/nginx.conf #//Add the following content
Location ~ ^/nginxstatus/{
Stub_status on;
Access_log off;
}
Shell> killall-1 Nginx
#//use the browser to access the http://nginx_server_ip/NginxStatus/to see the Status statistics page. (Three digits: Total number of connections processed, number of handshakes successfully created, total number of requests processed)
3, through the fastcgi way to support the PHP language
1 Start fastcgi Service (use php-cgi to do the actual processing of PHP page program, with SPAWN-FCGI is easy to open multiple php-cgi process simultaneously-"-C" option to control the number of child processes)
Shell>/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:
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 Configuration
Shell> killall-1 Nginx
4, the Virtual host settings
Modify nginx.conf file, add one server {...} Configuration, the parameters for each virtual host can be configured independently.
HTTP {
server {
Listen 80;
server_name www.vhost1.com;
Access_log Logs/vhost1.access.log Main;
Location/{
Index index.html;
Root/var/www/vhost1; #//the Web page root of the 1th virtual host
}
}
server {
Listen 80;
server_name www.vhost2.com;
Access_log Logs/vhost2.access.log Main;
Location/{
Index index.html;
Root/var/www/vhost2; #//the Web page root of the 2nd virtual host
}
}
}
5. Load balancing based on reverse proxy
Modify the nginx.conf file, increase the upstream configuration, specify the IP and weight of the corresponding server farm, and adjust the page root configuration in the server segment. The HTTP requests that access the Nginx server are dispersed to the servers in the Web cluster to handle.
HTTP {
Upstream My_web_cluster {
Server 192.168.2.11:8000 weight=3;
Server 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 (such as those about. php files) need to be commented out, or they may affect redirection of such files.
}
}

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.