深刻Nginx

Nginx功能模塊彙總

--with-http_core_module  #包括一些核心的http參數配置,對應nginx的配置爲http區塊部分
--with-http_access_module  #訪問控制模塊,用來控制網站用戶對nginx的訪問
--with-http_gzip_module  #壓縮模塊,nginx返回的數據壓縮,屬於性能優化模塊
--with-http_fastcgi_module  #FastCGI模塊,和動態應用相關的模塊,例如PHP
--with-http_proxy_module  #proxy代理模塊
--with-http_upstream_module  #負載均衡模塊,能夠實現網站的負載均衡功能及節點的健康檢查
--with-http_rewrite_module  #URL地址重寫模塊
--with-http_limit_conn_module  #限制用戶併發鏈接及請求數模塊
--with-http_limit_req_module  #根據定義的key限制nginx請求過程的sulv
--with-http_log_module  #請求日誌模塊,以制定的個事記錄nginx客戶訪問日誌的信息
--with-http_auth_basic_module  #web認證模塊,設置web用戶經過帳號、密碼訪問nginx
--with-http_ssl_module  #ssl模塊,用於加密的http鏈接,如https
--with-http_stub_status_module  記錄nginx基本訪問狀態信息等的模塊

 

Nginx的目錄結構說明

conf   #這是nginx全部配置文件的目錄
  fastcgi.conf     #fastcgi相關參數的配置文件
  fastcgi.conf.default    #fastcgi.conf的原始備份
  fastcgi_params       #fastcgi的參數文件
  fastcgi_params.default   
  koi_utf
  koi_win
  mime.types  #媒體類型
  mime.types.defualt
  nginx.conf  #Nginx默認的配置文件
  nginx.conf.default
  scgi_params  #scgi相關參數文件
  uwsgi_params  #uwsgi相關參數配置文件
fastcgi_temp   #fastcgi臨時配置文件
html  
logs  #默認的日誌路徑
  access.log  #默認訪問日誌文件
  error.log
  nginx.pid
proxy_temp   #臨時文件
sbin  #nginx的命令目錄
  nginx  #nginx的 啓動命令

 

Nginx的配置文件說明

cat nginx.conf.defaultuser www www;worker_processes 2;#worker進程的數量
pid logs/nginx.pid; events {        #事件區塊開始 use epoll; worker_connections 2048;  #每一個worker進程支持的最大鏈接數 } http {        #http區塊開始 include mime.types;  #ngninx支持的媒體類型庫文件 default_type application/octet-stream;  #默認的媒體類型 sendfile on;        #開啓高效傳輸模式 keepalive_timeout 65;    #鏈接超時  # 很重要的虛擬主機配置 server {        #第一個server區塊開始 listen 80;   server_name itoatest.example.com;    #提供服務的域名主機名ip/domain
charset utf-8; location / {
root /apps/oaapp;      #站點的根目錄
     index  index.html  index.htm;  #默認首頁文件
     }   
    error_page  500  502  504  504  /50x.html #出現對應的http狀態碼時,使用50x。html迴應客戶
    local  = /50x.html{
      root html;
    }
}

 

 

 Nginx虛擬主機配置

1.基於域名的nginx.conf配置文件html

    server {  
        listen       80;  
        server_name  www.abc.com;  
  
        location / {  
            root   html/www;  
            index  index.html index.htm;  
        }  
    server {  
        listen       80;  
        server_name  blog.abc.com;  
  
        location / {  
            root   html/blog;  
            index  index.html index.htm;  
        }  
    server {  
        listen       80;  
        server_name  bbs.abc.com;  
  
        location / {  
            root   html/bbs;  
            index  index.html index.htm;  
        } 

 

 2.基於端口的虛擬主機前端

    server {  
        listen       80;  
        server_name  www.abc.com;  
  
        location / {  
            root   html/www;  
            index  index.html index.htm;  
        }  
    server {  
        listen       81;  
        server_name  blog.abc.com;  
  
        location / {  
            root   html/blog;  
            index  index.html index.htm;  
        }  
    server {  
        listen       82;  
        server_name  bbs.abc.com;  
  
        location / {  
            root   html/bbs;  
            index  index.html index.htm;  
        } 

3.基於ip的虛擬配置nginx

    server {  
        listen       10.0.0.1:80;  
        server_name  www.abc.com;  
  
        location / {  
            root   html/www;  
            index  index.html index.htm;  
        }  
    server {  
        listen       10.0.0.2:81;  
        server_name  blog.abc.com;  
  
        location / {  
            root   html/blog;  
            index  index.html index.htm;  
        }  
    server {  
        listen       10.0.0.3:82;  
        server_name  bbs.abc.com;  
  
        location / {  
            root   html/bbs;  
            index  index.html index.htm;  
        } 

 

Nginx的規範優化配置文件

主文件包含的全部虛擬主機的子配置文件會統一放在extra目錄中。虛擬主機的配置文件按照網站的域名或功能名稱取名,例如www.conf等web

events {        #事件區塊開始
    use epoll;
    worker_connections  2048;  #每一個worker進程支持的最大鏈接數
}
http {        #http區塊開始
    include       mime.types;  #ngninx支持的媒體類型庫文件
    default_type  application/octet-stream;  #默認的媒體類型
    sendfile        on;        #開啓高效傳輸模式
    keepalive_timeout  65;    #鏈接超時
  
  include extra/www.conf;
  include extra/bbs.conf;
}

 

[root@www conf]# cat extra/www.conf
    server {  
        listen       80;  
        server_name  www.abc.com;  
  
        location / {  
            root   html/www;  
            index  index.html index.htm;  
        } 
}

 

location裏面設置容許和禁止的ip段訪問

location /nginx_status{
         stub_status on;   #打開狀態信息開關
       access_log off;
     allow 10.0.0.0/24;  #設置容許和禁止的IP段訪問
     deny all;  #設置容許和禁止的ip段訪問
}

 

Nginx錯誤日誌配置

錯誤日誌常見的級別【debug|info|notice|warn|error|crit|alert|emerg】瀏覽器

生存環境經過是warn|error|crit,注意不要配置info等較低級別,會損壞巨大磁盤IO性能優化

一般配置以下:服務器

worker_processes 1;
error_log    logs/error.logs   error;    #就在這裏配置
events    {
.....
}

 

Nginx訪問日誌

1.訪問日誌兩個參數併發

log_format  用來定義記錄日誌格式(能夠定義多種日誌個事,取不一樣名字)app

access_log  用來制定日誌文件的路徑和使用何種日誌格式記錄日誌負載均衡

2.訪問日誌配置說明

  log_format

  

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
          '$status $body_bytes_sent "$http_referer" '
          '"$http_user_agent" "$http_x_forwarded_for" ';

 

$remote_addr #記錄訪問網站的客戶端地址
$http_x_forwarded_for#當前端有代理服務器時,設置web節點記錄客戶端地址的配置,此參數生效的前提是代理服務器上也進行了相關的
$remote_user  #遠程客戶端用戶名稱
$time_local  #記錄訪問時間與時區
$request  #用戶的http請求其實行信息
$status  #http狀態碼,記錄請求返回的狀態,例如200,404,301等
$body_bytes_sents  #服務器發送給客戶端的響應body字節數
$http_referer  #記錄這次請求是從那個連接訪問過來的,能夠根據referer進行防盜鏈設置
$http_user_agent  #記錄客戶端訪問信息。例如瀏覽器,手機客戶端等

  access_log

語法以下:

access_log path [format [buffer=size [flush=time]] [if=condition]];
access_log path format gzip[=level] [buffer=size] [flush=time][if=codition];
access_log syslog:server=address[,parameter=value] [format [if=condition]];

buffer=size 爲存放訪問日誌的緩衝區大小

flush=time爲將緩衝區的日誌刷到磁盤的時間

gzip[=level]表示壓縮級別

[if=condition] 表示其餘條件

access_log off表示不記錄訪問日誌

通常狀況無須配置,極端優化菜考慮

樣例

 

events {        #事件區塊開始
    use epoll;
    worker_connections  2048;  #每一個worker進程支持的最大鏈接數
}
http {        #http區塊開始
    include       mime.types;  #ngninx支持的媒體類型庫文件
    default_type  application/octet-stream;  #默認的媒體類型
    sendfile        on;        #開啓高效傳輸模式
    keepalive_timeout  65;    #鏈接超時
  
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
          '$status $body_bytes_sent "$http_referer" '
          '"$http_user_agent" "$http_x_forwarded_for" ';  
    
  include extra/www.conf;
  include extra/bbs.conf; } 

 

 

[root@www conf]# cat extra/www.conf
    server {  
        listen       80;  
        server_name  www.abc.com;  
  
        location / {  
            root   html/www;  
            index  index.html index.htm;  
        }  
  access_log logs/access_www.log main;

}

 若是在高併發場景下提高網站的訪問性能,能夠加入buffer和flush選項

access_log logs/access_www.log main gzip buffer=32k flush=5s;

 

 

而後重啓服務,記得建立文件呀access_www.log

相關文章
相關標籤/搜索