Nginx的總結
Nginx是常用的web服務的軟件,比Apache性能和擴展性上要好,所以現在對Nginx的功能上做以下總結:
Nginx的功能:
1、Web服務-à設置多虛擬主機的服務並配合fast-cgi或tomcat支持動態網頁;
Nginx是近年來比較火的一個www服務的軟件,與Apache和lighttpd以及tomcat等功能類似,但是nginx要比前者有着卓越的性能,比如:採用了epoll模型,內存消耗小等優點;
2、反向代理 -à多虛擬主機的代理;
指以代理服務器來接受Internet上的連接請求,然後將請求轉發給內部網絡上的服務器,並將從服務器上得到的結果返回給Internet上請求連接的客戶端;
3、七層的負載均衡—>單多虛擬主機不同服務器之間的訪問;
負載均衡是由多臺服務器以對稱的方式組成一個服務器集合,每臺都是等價地位,通過某種負載分擔技術,將外部發送來的請求均勻分配到對稱結構中某一臺服務器上,來接收到請求的服務器獨立地迴應客戶的請求;
4、正向代理 –>代理上網;
代理內部網絡對Internet的鏈接請求,客戶機必須指定代理服務器,並將本來要直接發送到web服務器上的http請求發送到代理服務器中,由代理服務器請求並返回響應內容;
5、緩存服務
爲proxy和fastcgi做緩存服務,提高訪問速度,相當於squid功能;
Nginx的優點:
1、高併發連接(採用epoll模型);2、內存消耗小;3、成本低(免費、開源);4、其他(配置簡單、支持rewrite重寫、內置健康檢查、節省帶寬gzip、穩定性高、支持熱部署等)
Nginx的安裝和配置:
1、查看系統版本和內核:
cat/etc/redhat-release
CentOS release6.7 (Final)
[root@LNMP-02 ~]#uname -r
2.6.32-573.el6.x86_64
2、安裝nginx的依賴包:
rpm -aq pcrepcre-devel openssl openssl-devel
yum install pcrepcre-devel openssl openssl-devel -y
3、創建安裝包下載路徑(這個要養成習慣,也是作爲好運維的標準)
mkdir –p /server/xuesong/tools
cd /server/xuesong/tools/
下載:wget http://nginx.org/download/nginx-1.6.3.tar.gz
ls nginx-1.6.3.tar.gz -----下載成功
nginx-1.6.3.tar.gz
4、創建nginx用戶(虛擬用戶)並解壓並編譯nginx:
useradd nginx -s/sbin/nologin -M
tar xfnginx-1.6.3.tar.gz
cd nginx-1.6.3
./configure--user=nginx --group=nginx --prefix=/app/nginx-1.6.3--with-http_stub_status_module --with-http_ssl_module
echo $? 0 ------看是否有錯誤
mkdir /app
make
make install
ln -s/app/nginx-1.6.3/ /app/nginx
ll /app/nginx
做軟連接並檢查語法及啓動nginx服務:
lrwxrwxrwx. 1root root 17 May 26 06:56 /app/nginx -> /app/nginx-1.6.3/
[[email protected]]# /app/nginx/sbin/nginx -t
nginx: theconfiguration file /app/nginx-1.6.3/conf/nginx.conf syntax is ok
nginx:configuration file /app/nginx-1.6.3/conf/nginx.conf test is successful
[[email protected]]# /app/nginx/sbin/nginx
[[email protected]]# lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 7511 root 6u IPv4 24823 0t0 TCP *:http (LISTEN)
nginx 7512 nginx 6u IPv4 24823 0t0 TCP *:http (LISTEN)
服務啓動完畢,用window瀏覽器訪問IP測試:
更改主頁並測試:
cd /app/nginx
vim ./html/index.html
並在此用windows測試。
Nginx主要組成文件:
tree ./
|--client_body_temp
|-- conf
| |-- fastcgi.conf ---------->這是fastcgi的主配置文件
| |-- fastcgi.conf.default
| |-- fastcgi_params
| |-- fastcgi_params.default
| |-- koi-utf
| |-- koi-win
| |-- mime.types ---------->這是媒體類型
| |-- mime.types.default
| |-- nginx.conf ---------->這是nginx的主配置文件
| |-- nginx.conf.default
| |-- scgi_params
| |-- scgi_params.default
| |-- uwsgi_params
| |-- uwsgi_params.default
| `-- win-utf
|-- fastcgi_temp
|-- html ---------->這是nginx的默認站點目錄
| |-- 50x.html
| `-- index.html
|-- logs ---------->這是nginx的錯誤和訪問日誌
| |-- access.log
| |-- error.log
| `-- nginx.pid
|-- proxy_temp
|-- sbin ---------->這是nginx的命令目錄,如啓動命令
| `-- nginx
|-- scgi_temp
`-- uwsgi_temp
9 directories, 21files
配置和優化的事項:
一、配置文件的歸類(xx.conf和fast-cgi.conf等)
基礎的主配置文件nginx.conf只有一個,如果配置web或proxy服務時,所有的配置和參數都配置到nginx.conf中看起來比較混亂而且不易於管理,所以把虛擬主機的配置分拆xx.conf和fast-cgi.conf等,放置到conf/下,並在主配置文件Nginx.conf中添加include,使之生效並加載到配置文件中;
二、虛擬主機程序的歸類(www或bbs等)
基礎(默認)的虛擬主機的首頁文件在/html/下,但是如果你有多個虛擬主機可以在html/下建立多個文件夾比如www或bbs等,並在對應的配置文件中更改root 標籤項,對應的路徑:
location / {
root html/www;
index index.html index.htm;
}
三、Nginx的主配置文件的示例(說明):
組成結構:
-------------------------------------------------------------------------------
…………
events
{
……..
}
http
{
………….
Server
{
……
}
Server
{
……
}
…………
}
-------------------------------------------------------------------------------
配置說明:
#user:指定 Nginx Worker進程運行用戶和用戶組,默認 nobody 賬號
user nginx nginx;
#worker_processes:指定 Nginx 要開啓的進程數,建議和 cpu 數量一樣的
worker_processes 1;
# error_log:定義全局錯誤日誌文件。日誌有輸出級別:
[ debug | info | notice | warn | error | crit ]
error_log /var/log/nginx/error.log info;
#進程文件
pid /var/run/nginx.pid;
#一個 nginx 進程打開的最多文件描述符數目,理論值應該是最多打開文件數(系統的值 ulimit-n)與 nginx 進程數相除,但是 nginx 分配請求並不均勻,所以建議與 ulimit -n 的值保持一致。
worker_rlimit_nofile 65535;
#工作模式與連接數上限
events
{
#參考事件模型,use [ kqueue | rtsig | epoll | /dev/poll |select | poll ]; epoll 模型是 Linux 2.6 以上版本內核中的高性能網絡 I/O 模型,如果跑在 FreeBSD 上面,就用 kqueue模型。
use epoll;
#單個進程最大連接數(最大連接數=連接數*進程數)
worker_connections 65535;
}
#設定服務器
http
{
include mime.types; #文件拓展名和文件類型映射表
default_type application/octet-stream; #默認文件類型
charset utf-8; #默認編碼
server_names_hash_bucket_size 128; #服務器名字的 bash 表大小
client_header_buffer_size 32k; #上傳文件大小限制
large_client_header_buffers 4 64k;
client_max_body_size 8m;
sendfile on; #開啓高效文件傳輸模式
autoindex on;#開啓目錄表訪問,合適下載服務器,默認關閉
tcp_nopush on;防止網絡阻塞
tcp_nodelay on; #防止網絡阻塞
keepalive_timeout 120; #長連接超時時間,單位是秒
#FastCGI 相關參數是爲了改善網站的性能:減少資源佔用,提高訪問速度。下面參數看字面意思都能理解。
fastcgi_connect_timeout300; 132
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
#gzip 模塊設置
gzip on; #開啓 gzip 壓縮文件大小
gzip_min_length 1k; #最小壓縮文件大小
gzip_buffers 4 16k; #壓縮緩衝區
gzip_http_version 1.1;#壓縮版本
gzip_comp_level 2; #壓縮等級
gzip_types text/plain application/x-javascript text/css application/xml;
#壓縮類型,默認就已經包含text/html,所以下面就不用再寫了,寫上去也不會有問題,但是會有一個 warn。
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m; #開啓限制 IP 連接數的時候需要使用
upstream zhouxuesong.com{
#upstream 的負載均衡,weight 是權重,可以根據機器配置定義權重。weigth 參數表示權值,權值越高被分配到的機率越大。
server 192.168.80.121:80 weight=3;
server 192.168.80.122:80 weight=2;
server 192.168.80.123:80 weight=3;
}
#日誌格式設定
log_formataccess '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
#虛擬主機的配置
server
{
#監聽端口
listen 80;
#域名可以有多個,用空格隔開
server_name www.zhouxuesong.com xuesong.com
index index.html index.htm index.php;
root /data/www/
location ~ .*.(php|php5)?$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
#圖片緩存時間設置
location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 10d;
}
#JS 和 CSS 緩存時間設置
location ~ .*.(js|css)?$
{
expires 1h;
}
#定義本虛擬主機的訪問日誌
access_log /var/log/nginx/access.log access;
#對 "/" 啓用反向代理
location / {
proxy_pass http://zhouxuesong.com;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
#後端的 Web 服務器可以通過 X-Forwarded-For 獲取用戶真實 IP
proxy_set_header X-Forwarded-For $remote_addr;
#以下是一些反向代理的配置,可選。
proxy_set_header Host $host;
client_max_body_size 10m; #允許客戶端請求的最大單文件字節數
client_body_buffer_size 128k; #緩衝區代理緩衝用戶端請求的最大字節數,
proxy_connect_timeout 90; #nginx跟後端服務器連接超時時間(代理連接超時)
proxy_send_timeout 90; #後端服務器數據回傳時間(代理髮送超時)
proxy_read_timeout 90; #連接成功後,後端服務器響應時間(代理接收超時)
proxy_buffer_size 4k; #設置代理服務器(nginx)保存用戶頭信息的緩衝區大小
proxy_buffers 4 32k;#proxy_buffers 緩衝區,網頁平均在32k 以下的設置
proxy_busy_buffers_size 64k; #高負荷下緩衝大小(proxy_buffers*2)
proxy_temp_file_write_size 64k;
#設定緩存文件夾大小,大於這個值,將從 upstream 服務器傳
}
#設定查看 Nginx 狀態的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file conf/htpasswd;
#htpasswd 文件的內容可以用 apache 提供的 htpasswd 工具來產生。
}
#本地動靜分離反向代理配置
#所有 jsp 的頁面均交由 tomcat 或 resin 處理
location ~ .(jsp|jspx|do)?$ {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://127.0.0.1:8080;
}
#所有靜態文件由 nginx 直接讀取不經過 tomcat 或 resin
location~.*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
{ expires 15d; }
location ~ .*.(js|css)?$
{ expires 1h; }
} }
nginx.conf裏面的參數都做了註釋或詳解,對大的區塊做一個小結:
1、 虛擬主機(域名、端口、IP地址)
2、 Log日誌(存放位置、格式)
3、 Nginx的壓縮輸出配置
4、 Nginx的瀏覽器本地緩存設置
5、 與php以及tomcat動態網頁配合
6、 Nginx的反向代理和負載均衡
不同的區域對應的功能(核心功能模塊):
1、 nginx作爲web服務的配置示例:
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"';
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
gzip on;
server {
listen 80;
server_name www.zhouxuesong.com;
access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
注意說明:
如果web服務器作爲代理服務器後端的real server那麼log的格式就是以上所示,但是還需要在proxy server配置文件內(proxy_pass下方)加上proxy_set_headerX-Forwarded-For $remote_addr; proxy_set_header Host $host;這兩個參數,後端服務才能生效!(格式在http標籤裏面、access_log在server標籤裏面)
nginx與動態網頁(PHP、java)配合配置示例:
location~ .*.(php|php5)?$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf; -à配置文件在conf/下,這是針對fastcgi優化配置
}
java(Tomcat)
location~ .(jsp|jspx|do)?$ {
proxy_set_headerHost $host;
proxy_set_header X-Forwarded-For$remote_addr;
proxy_pass http://127.0.0.1:8080;
}
nginx瀏覽器本地緩存設置
location~ .*.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 10d;
}
#JS 和 CSS 緩存時間設置
location ~ .*.(js|css)?$
{
expires 1h;
}
Nginx代理服務器(反向代理和負載均衡)
upstream zhouxuesong.com {
server 192.168.80.121:80 weight=3 max_fail=3 fail_timeout=30s;
server 192.168.80.122:80 weight=2;
server 192.168.80.123:80 weight=3;
}
#在sever{}標籤裏面,一個虛擬主機;
location / {
proxy_pass http://zhouxuesong.com;
proxy_redirect off;
#後端的 Web 服務器可以通過 X-Forwarded-For 獲取用戶真實 IP
proxy_set_header X-Forwarded-For $remote_addr;
#以下是一些反向代理的配置,可選。
proxy_set_header Host $host;
nginx的正向代理:
正向代理就是通常所說的內網通過服務器來上Internet網的這種方式,這臺服務器就叫代理服務器;
……
Server
{
listen8080;
location / {
resolver 8.8.8.8;
proxy_passhttp://$host$request_uri;
}
access_log /logs/proxy.log;
}
………….
以上是proxy的代理服務器上的配置,最後在客戶端的IE上設置LAN代理填寫。
nginx的緩存服務器(pass_cache、fastcgi_cache):
proxy_cache和fastcgi_cache構成了Nginx的緩存,proxy_cache主要用於反向代理時,對後端內容源服務器進行緩存,可能是任何內容,包括靜態的和動態,緩存減少了nginx與後端通信的次數,節省了傳輸時間和後端寬帶;fastcgi_cache主要用於對FastCGI的動態程序進行緩存,很多情況是php生成的動態的內容,fastcgi_cache緩存減少了nginx與php的通信的次數,更減輕了php和數據庫(mysql)的壓力,這比用memcached之類的緩存要輕鬆得多。。兩者的功能基本上一樣。
pass_cache (代理緩存):
mkdir–p /data0/proxy_temp_path 注:兩個緩存文件必須要在同一磁盤分區,不能
mkdir–p /data0/proxy_cache_path 誇分區。
Nginx配置文件nginx.conf:對擴展名爲gif\jpg\jpeg、 swf\bmp、的圖片、flash、js\css、文件開啓web緩存,其他文件不緩存;
proxy_temp_path /data0/proxy_temp_path
proxy_cache_path /data0/proxy_cache_path levels=1:2keys_zone=cache_one:200m inactive=1d max_size=30g;
注:上面這些參數都是在http{}標籤裏面;
location~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
proxy_cache cache_one ; #使用web緩存區cache_one
proxy_cache_valid200 304 12h;
proxy_cache_valid301 302 1m;
proxy_cache_validany 1m;
#對不同http狀態碼緩存設置不同的緩存時間
proxy_cache_key$host$uri$is_args$args;
proxy_passhttp://www.zhouxuesong.com;
proxy_set_headerHost $host;
proxy_set_headerX-Forwarded-For $remote_addr;
}
Fastcgi_cache(fastcgi緩存):
mkdir–p /data0/fastcgi_temp_path 注:兩個緩存文件必須要在同一磁盤分區,不能
mkdir–p /data0/fastcgi_cache_path 誇分區。
Nginx配置文件nginx.conf:對擴展名爲php文件開啓web緩存,其他文件不緩存;
fastcgi_temp_path /data0/ fastcgi _temp_path;
fastcgi_cache_path /data0/fastcgi_cache_pathlevels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
注:這些參數都是在http{}標籤裏面;
location~ .*.(php|php5)?$
{
fastcgi _cache cache_one #使用web緩存區cache_one
fastcgi_cache_valid 200 304 12h;
fastcgi_cache_valid 301 302 1m;
fastcgi_cache_valid any 1m;
#對不同http狀態碼緩存設置不同的緩存時間
fastcgi_cache_key $host$uri$is_args$args;
}
nginx日誌的輪詢 nginx rwrite 規則
Nginx的日誌切割和輪詢:
#mv www.access.log www.access_$(date +%F -d -1day).log
#/application/nginx/sbin/nginx -s reload