nginx的官方網(wǎng)站是:http://www.nginx.org
Nginx ("engine x") 是一個(gè)高性能的 HTTP 和 反向代理 服務(wù)器,也是一個(gè) IMAP/POP3/SMTP 代理服務(wù)器。 Nginx 是由 Igor Sysoev 為俄羅斯訪問量第二的 Rambler.ru 站點(diǎn)開發(fā)的,它已經(jīng)在該站點(diǎn)運(yùn)行超過兩年半了。Igor 將源代碼以類BSD許可證的形式發(fā)布。盡管還是測試版,但是,Nginx 已經(jīng)因?yàn)樗姆€(wěn)定性、豐富的功能集、示例配置文件和低系統(tǒng)資源的消耗而聞名了。
nginx的特性
1.目前官方 Nginx 并不支持Windows,您只能在包括Linux,UNIX,BSD系統(tǒng)下安裝和使用
2.Nginx 本身只是一個(gè)HTTP和反向代理服務(wù)器,它無法像Apache一樣通過安裝各種模塊來支持不同的頁面腳本,例如PHP、CGI等
3.Nginx 支持簡單的負(fù)載均衡和容錯(cuò)
4.支持作為基本 HTTP 服務(wù)器的功能,例如日志、壓縮、Byte ranges、Chunked responses、SSL、虛擬主機(jī)等等,應(yīng)有盡有
安裝
安裝nginx前需要確保系統(tǒng)中已經(jīng)安裝PCRE包,PCRE library這個(gè)是HTTP Rewrite模塊,也即是url靜態(tài)化的包,到http://www.pcre.org下載最新的PCRE源碼包,
或直接使用yum install pcre,apt-get instll pcre進(jìn)行安裝
登錄 http://www.nginx.org/en/download.html 下載最新的版本
wget http://www.nginx.org/download/nginx-0.8.53.tar.gz
tar zxvf nginx-0.8.53.tar.gz
cd nginx-0.8.53
./configure --prefix=/usr/local/nginx --with-pcre=/home/download/pcre-8.01 --with-http_ssl_module --with-openssl=/home/download/openssl-1.0.0a --with-http_stub_status_module
##指定pcre目錄和啟動(dòng)ssl,https模塊
make
make install
##為什么Nginx的性能要比Apache高得多?這得益于Nginx使用了最新的epoll(Linux 2.6內(nèi)核)和kqueue(freebsd)網(wǎng)絡(luò)I/O模型,而Apache則使用的是傳統(tǒng)的select模型。目前Linux下能夠承受高并發(fā)訪問的 Squid、Memcached都采用的是epoll網(wǎng)絡(luò)I/O模型,處理大量的連接的讀寫,Apache所采用的select網(wǎng)絡(luò)I/O模型非常低效
配置
編輯vi /usr/local/nginx/conf/nginx.conf,其中/usr/local/nginx為安裝路徑
##指定nginx的用戶名和用戶組
user nobody;
##啟動(dòng)進(jìn)程數(shù)
worker_processes 8;
worker_rlimit_nofile 10240;
##指定PID文件
pid logs/nginx.pid;
##指定工作模式和鏈接上限
events {
use epoll;
worker_connections 10240;
}
##http服務(wù)器
http {
include mime.types;
default_type text/html;
##指定日志格式
log_format main '$http_x_forwarded_for - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
##指定accesslog
access_log logs/nginx.log main;
##指定超時(shí)
keepalive_timeout 300;
##開啟gzip模塊
gzip on;
gzip_min_length 1000;
gzip_buffers 4 8k;
gzip_types text/*;
gzip_http_version 1.0;
output_buffers 1 32k;
postpone_output 1460;
gzip_proxied any;
gzip_vary on;
##指定請求的緩沖
client_header_timeout 5m;
client_body_timeout 5m;
send_timeout 5m;
sendfile on;
tcp_nopush on;
tcp_nodelay off;
client_header_buffer_size 16k;
large_client_header_buffers 4 64k;
server_names_hash_bucket_size 128;
ssi on;
ssi_silent_errors on;
ssi_types text/shtml;
##指定虛擬主機(jī)
server {
listen 80;
server_name _;
server_name_in_redirect off;
location / {
root /dev/null;
}
}
##指定include文件
include servers/*.com;
}
新建proxy.conf
proxy_set_header X-Forwarded-For $remote_addr; ##設(shè)定header
proxy_set_header RealIP $remote_addr;
proxy_set_header Accept-Encoding ' ';
proxy_hide_header Vary; ##隱藏header
add_header via_up $upstream_addr;
proxy_connect_timeout 2m; ##代理連接超時(shí)
proxy_send_timeout 2m; ##代理發(fā)送超時(shí)
proxy_read_timeout 2m; ##代理發(fā)送超時(shí)
proxy_temp_file_write_size 2048m;##設(shè)定緩存文件夾大小
proxy_buffer_size 256k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 512k;
proxy_ignore_client_abort off;
proxy_next_upstream error timeout invalid_header;
新建目錄/conf/servers,并新建配置文件test.com
server {
listen 80;
server_name 245.test.com;
root /home/htmlfile/test;
location = / {
proxy_temp_path /var/www/cache;
index index.html index.htm;
}
}
修改配置文件后,通過以下命令檢查配置是否正確
/usr/local/nginx/sbin/nginx -t
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
configuration file /usr/local/nginx/conf/nginx.conf test is successful
啟動(dòng)命令
/usr/local/nginx/sbin/nginx
停止命令
/usr/local/nginx/sbin/nginx -s stop
平滑重新加載配置文件
kill -HUP `cat /usr/local/nginx/logs/nginx.pid
添加到自啟動(dòng)
echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local