1. Nginx介紹
Nginx ("engine x") 是一個高性能的 HTTP 和 反向代理 服務器,也是一個 IMAP/POP3/SMTP 代理服務器,其兼具apache和squid代理服務器的一款高性能、低消耗的開源服務,具有很好的研究價值。Nginx 是由 Igor Sysoev 爲俄羅斯訪問量第二的 Rambler.ru 站點開發的,第一個公開版本0.1.0發佈於2004年10月4日。其將源代碼以類BSD許可證的形式發佈,因它的穩定性、豐富的功能集、示例配置文件和低系統資源的消耗而聞名。
注:本文只對安裝和反向代理做簡單介紹便於相關人員對Nginx有一個簡單的認識,更詳細的配置及優化請查閱相關資料,參考文獻http://www.nginx.cn等
2. 安裝環境準備1) 兩臺設備分別用作Nginx服務器和被代理服務器(比如啓動一個apache服務)
2) Linux RedHat 5.4(2.6內核均可)
3) 安裝包nginx-1.2.3.tar.gz
4) 環境庫文件zlib-1.2.7.tar.gz, pcre-8.21.tar.gz(可選,視環境情況選擇安裝,高版本的pcre對正則表達能更好的支持)
注:安裝包可到http://www.nginx.cn/nginx-download進行選擇下載;
3. 安裝步驟1) 上傳安裝包、庫文件等至Nginx服務器;
2) tar xzvf nginx-1.2.3.tar.gz;cd nginx-1.2.3
3) ./configure --prefix=/usr/local/nginx --with-http_ssl_module
#默認安裝的路徑是/usr/local/nginx
更多的安裝配置
./configure --prefix=/usr/local/nginx
--with-openssl=/usr/include (啓用ssl)
--with-pcre=/usr/include/pcre/ (啓用正規表達式)
--with-http_stub_status_module (安裝可以查看nginx狀態的程序)
--with-http_memcached_module (啓用memcache緩存)
--with-http_rewrite_module (啓用支持url重寫)
4) make&&make install
4. 安裝目錄介紹安裝目錄PREFIX=/usr/local/nginx
配置文件 PREFIX/conf/nginx.conf
執行文件 PREFIX/sbin/nginx
日誌文件 PREFIX/logs/
文檔目錄 PREFIX/html/
5. 啓動/停止Nginx服務
Ø 啓動:/usr/local/nginx/sbin/nginx &
Ø 關閉:killall -9 nginx
注:可將nginx添加至系統服務採用serivce nginx start/restart/stop來啓動/重啓/停止nginx服務,可查閱相關/init.d文獻;
6. Nginx基本配置配置項介紹如下,具體配置可參考實例附件nginx.conf;
#運行用戶
user nobody nobody;
#啓動進程
worker_processes 5;
#全局錯誤日誌及PID文件
error_log logs/error.log notice;
pid logs/nginx.pid;
#工作模式及連接數上限
events {
#工作模式有:select(標準模式),poll(標準模式),kqueue(高效模式,適用FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 and MacOS X),
#epoll(高效模式,本例用的。適用Linux 2.6+,SuSE 8.2,),/dev/poll(高效模式,適用Solaris 7 11/99+, HP/UX 11.22+ (eventport), IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+)
use epoll;
worker_connections 1024;
}
#設定http服務器,利用它的反向代理功能提供負載均衡支持
http {
#設定mime類型
include conf/mime.types;
default_type application/octet-stream;
#設定日誌格式
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$gzip_ratio"';
log_format download '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$http_range" "$sent_http_content_range"';
#設定請求緩衝
client_header_buffer_size 10k;
large_client_header_buffers 4 4k;
#開啓gzip模塊,要求安裝gzip 在運行./config時要指定
gzip on;
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_types text/plain;
output_buffers 1 32k;
postpone_output 1460;
#設定訪問日誌
access_log logs/access.log main;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
#設定負載均衡的服務器列表
upstream backserver {
#weigth參數表示權值,權值越高被分配到的機率越大
#本例是指在同一臺服務器,多臺服務器改變ip即可
server 127.0.0.1:8081 weight=5;
server 127.0.0.1:8082;
server 127.0.0.1:8083;
}
#設定虛擬主機,默認爲監聽80端口,改成其他端口會出現問題
server {
listen 80;
server_name test.com www.test.com;
charset utf8;
#設定本虛擬主機的訪問日誌
access_log logs/test.com.log main;
#如果訪問 /images/*, /js/*, /css/* 資源,則直接取本地文件,不用轉發。但如果文件較多效果不是太好。
location ~ ^/(images|js|css)/ {
root /usr/local/testweb;
expires 30m;
}
#對 "/" 啓用負載均衡
location / {
proxy_pass http://backserver;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
#設定查看Nginx狀態的地址,在運行./config 要指定,默認是不安裝的。
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
#是否要通過用戶名和密碼訪問,測試時可以不加上。conf/htpasswd 文件的內容用 apache 提供的 htpasswd 工具來產生即可
#auth_basic_user_file conf/htpasswd;
}
}
7. 附件nginx.conf紅色需重點關注;該配置文件分別對原始服務器http://122.193.14.88和http://218.108.85.62:8808的服務進行反向代理同時識別文件的後綴名稱.ts的文件進行代理並緩存其他只代理不緩存,緩存文件在/usr/local/nginx/proxy_temp/;
#user nobody;
worker_processes 1;
#error_log logs/error.log;
error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
## Proxy options
proxy_buffering on;
proxy_cache_min_uses 1;
proxy_cache_path /usr/local/nginx/proxy_temp/ levels=1:2 keys_zone=cache:10m inactive=10m max_size=1000M;
proxy_cache_valid any 10m;
proxy_ignore_client_abort off;
proxy_intercept_errors on;
proxy_next_upstream error timeout invalid_header;
proxy_redirect off;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_connect_timeout 60;
proxy_send_timeout 60;
proxy_read_timeout 60;
## Backend servers (web1 is the primary and web2 will come up if web1 is down)
upstream webbackend {
server web1.domain.lan weight=10 max_fails=3 fail_timeout=30s;
server web2.domain.lan weight=1 backup;
}
server {
listen 8011;
server_name localhost;
#charset koi8-r;
#access_log logs/access.log main;
access_log logs/access.log;
#location / {
# root html;
# index index.html index.htm;
#}
location / {
#proxy_pass http://122.193.14.88;
proxy_pass http://xx:8808;
#proxy_cache cache;
#proxy_cache_valid 200 24h;
#proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
#proxy_ignore_headers Expires Cache-Control;
}
location ~ \.ts$ {
#proxy_pass http://122.193.xx.xx;
proxy_pass http://ip:8808;
proxy_cache cache;
proxy_cache_valid 200 24h;
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
proxy_ignore_headers Expires Cache-Control;
}
## All other errors get the generic error page
error_page 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 500 501 502 503 504 505 506 507 /50x.html;
location = /50x.html {
root html;
}
server {
listen 8012;
server_name localhost;
#charset koi8-r;
#access_log logs/access.log main;
access_log logs/access2.log;
#location / {
# root html;
# index index.html index.htm;
#}
location / {
proxy_pass http://122.193.xx.xx;
#proxy_cache cache;
#proxy_cache_valid 200 24h;
#proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
#proxy_ignore_headers Expires Cache-Control;
}
location ~ \.ts$ {
proxy_pass http://122.193.xx;
#proxy_pass http://ip2:8808;
proxy_cache cache;
proxy_cache_valid 200 24h;
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
proxy_ignore_headers Expires Cache-Control;
}
## All other errors get the generic error page
error_page 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 500 501 502 503 504 505 506 507 /50x.html;
location = /50x.html {
root html;
}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
8. 測試驗證以一個服務爲例,以下情況說明配置成功:
a) 正常訪問http://122.193.14.xx/1.ts 可以正常讀取文件,配置代理後通過http://nginx-IP:8012/1.ts可以正常讀取數據並且在PREFIX/proxy_temp/有相應的緩存文件;
b) 正常訪問http://122.193.14.xx/1.MP3等其他非ts文件可以正常讀取文件,配置代理後通過http://nginx-IP:8012/1.mp3可以正常讀取數據並且在PREFIX/proxy_temp/沒有有相應的緩存文件;