Nginx指南和配置詳解
nginx指南
Nginx ("engine x") 是一個高性能的 HTTP 和 反向代理 服務器,也是一個 IMAP/POP3/SMTP 代理服務器。 Nginx 因它的穩定性、豐富的功能集、示例配置文件和低系統資源的消耗而聞名了。更多的請見官方wiki:?http://wiki.nginx.org/Main 。
其優點:
Nginx做為HTTP服務器,有以下幾項基本特性:
處理靜態文件,索引文件以及自動索引;打開文件描述符緩沖.
無緩存的反向代理加速,簡單的負載均衡和容錯.
一、依賴的程序
gzip module requires zlib library
rewrite module requires pcre library
ssl support requires openssl library
二、安裝
$./configure
$make
$make install
默認安裝的路徑是/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重寫)
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /usr/local/nginx/)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file
啟動:nginx
重啟:nginx -s reload
退出:nginx -s quit
測試配置文件:nginx -t
Nginx配置詳解
在進行Nginx配置的時候會出現很多不明白的地方,其實有些時候只要換一個思維的方式就能找多你要解決問題的方法。官方配置樣例:http://wiki.nginx.org/NginxFullExample
#運行用戶
user nobody nobody;
#啟動進程
worker_processes 4;
#全局錯誤日志及PID文檔 [ debug | info | notice | warn | error | crit ]
error_log logs/error.log notice;
pid logs/Nginx.pid;
#工作模式及連接數上限
events {
#工作模式有:# use [ kqueue | rtsig | epoll | /dev/poll | select | poll ] ;
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 1k;
large_client_header_buffers 4 4k;
#開啟gzip模塊
gzip on;
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_types text/plain;
output_buffers 1 32k;
postpone_output 1460;
#設定access log
access_log logs/access.log main;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
sendfile on; #指令激活或停用的sendfile()的用法。
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
#設定負載均衡的服務器列表
upstream mysvr {
#weigth參數表示權值,權值越高被分配到的幾率越大
#本機上的Squid開啟3128端口
server 192.168.8.1:3128 weight=5;
server 192.168.8.2:80 weight=1;
server 192.168.8.3:80 weight=6;
}
#設定虛擬主機
server {
listen 80;
server_name 192.168.8.1 www.yejr.com;
charset utf8;
#設定本虛擬主機的訪問日志
access_log logs/www.yejr.com.access.log main;
#假如訪問 /img/*, /js/*, /css/* 資源,則直接取本地文檔,不通過squid
#假如這些文檔較多,不推薦這種方式,因為通過squid的緩存效果更好
location ~ ^/(img|js|css)/ {
root /data3/Html;
expires 24h;
}
#對 "/" 啟用負載均衡
location / {
proxy_pass http://mysvr;
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狀態的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file conf/htpasswd;
}
# error_page 404 /404.html;
# location /404.html {
# root /spool/www;
# charset on;
# source_charset koi8-r;
# }
# location /old_stuff/ {
# rewrite ^/old_stuff/(.*)$ /new_stuff/$1 permanent;
# }
#location /download/ {
# valid_referers none blocked server_names *.example.com;
# if ($invalid_referer) {
# #rewrite ^/ http://www.example.com/;
# return 403;
# }
# rewrite_log on;
# # rewrite /download/*/mp3/*.any_ext to /download/*/mp3/*.mp3
# rewrite ^/(download/.*)/mp3/(.*)\..*$ /$1/mp3/$2.mp3 break;
# root /spool/www;
# # autoindex on;
# access_log /var/log/nginx-download.access_log download;
# }
# location ~* ^.+\.(jpg|jpeg|gif)$ {
# root /spool/www;
# access_log off;
# expires 30d;
#}
}
}
Active connections: 328
server accepts handled requests
9309 8982 28890
Reading: 1 Writing: 3 Waiting: 324
Nginx 負載均衡緩存
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。