nginx的配置說明

Nginx是主流的web負載、代理轉發工具,這裏說明nginx的配置。其配置文件是nginx/conf/nginx.conf文件。

總覽ngnix.conf

user  nobody;
worker_processes  1;
error_log  logs/error.log  error;
pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    gzip  on;

    upstream  myClusterServer1 {
       server 127.0.0.1:8081  weight=5;
       server 127.0.0.1:8082  weight=5;
       server 127.0.0.1:8083  weight=5;
    }

    server {
        listen 80;
        server_name 127.0.0.1;
        charset utf-8;
        location / {
            root html;
            index index.html index.htm index.jsp;
            proxy_pass http://myClusterServer1;
            proxy_redirect defaut;
            proxy_connect_timeout 10;
        }
        error_page 404 /404.html;
        error_page 500 502 503 503 /50x.html;
        location = /50x.html{
            root html;
        }
    }
}

1.創建進程的用戶和用戶組

user  nobody

2.服務進程數量,一般等於CPU數量

worker_processes 1;

3.全局錯誤日誌定義

建議開啓error級別日誌,如輸出錯誤日誌或以上內容到 logs/error.log 文件:

error_log logs/error.log error;

可選級別:

級別 說明
debug 調試內容或以上
info 信息內容或以上
notice 通知內容或以上
warn 警告內容或以上
error 錯誤內容或以上
crit 致命異常內容或以上

4.記錄進程ID的文件

pid logs/nginx.pid;

5.啓用epoll多路複用提高性能

epoll多路複用IO(I/O Multiplexing)中的一種方式,但僅用於linux2.6以上內核,可以大大提高nginx的性能。

Linux建議使用epoll,FreeBSD建議使用kqueue。

events {
   use epoll;
   worker_connections  1024;
}

說明:worker_connections 表示一個worker_processe允許的最近併發連接數量。

6.常用http設置

http {
   include       mime.types;
   default_type  application/octet-stream;
   access_log    logs/access.log  main;
   sendfile      on;
   keepalive_timeout  65;
}

7.http>gzip配置

ngnix可開啓gzip功能來降低數據大小,以提高數據傳輸效率。gzip常用配置如下:

http {
    gzip  on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 2;   
    gzip_vary on;
    gzip_types text/plain text/javascript text/css application/xmlapplication/x-javascript application/json;
}
  • 開啓gzip

    gzip  on; 
  • 最小壓縮文件大小

    gzip_min_length 1k; 
  • 壓縮緩衝區

    gzip_buffers 4 16k;  
  • http的協議版本(1.0/1.1)

    • 默認1.1,前端如果是squid2.5請使用1.0。
    gzip_http_version 1.1;
  • gzip壓縮比

    • 1壓縮比最小處理速度最快。
    • 9壓縮比最大但處理速度最慢(傳輸快但比較消耗cpu)。
    gzip_comp_level 2;   
  • 自動識別是否壓縮

    • 有的瀏覽器支持壓縮,有的不支持,所以避免浪費不支持的也壓縮,可啓用vary,以便根據客戶端的HTTP頭來判斷是否需要壓縮。
    gzip_vary on;
  • gzip壓縮類型

    gzip_types text/plain text/javascript text/css 

注意:不用添加text/html,否則會有警告信息。

8.http>upstream 負載均衡服務器列表

http {
    upstream  myClusterServer1 {
       server 127.0.0.1:8081  weight=5;
       server 127.0.0.1:8082  weight=5;
       server 127.0.0.1:8083  weight=5;
    }

    server {
        listen 80;
        server_name 127.0.0.1;
        charset utf-8;
        location / {
            root html;
            index index.html index.htm index.jsp;
            proxy_pass http://myClusterServer1;
            proxy_redirect defaut;
            proxy_connect_timeout 10;
        }
        error_page 404 /404.html;
        error_page 500 502 503 503 /50x.html;
        location = /50x.html{
            root html;
        }
    }
}
  • server指定服務器

    • 域名或IP:端口號表示服務器地址。
    • weight 參數表示權值,權值越高被分配到的機率越大。
    server 127.0.0.1:8081  weight=5;

9.http>server配置服務節點

http {
    upstream  myClusterServer1 {
       server 127.0.0.1:8081  weight=5;
       server 127.0.0.1:8082  weight=5;
       server 127.0.0.1:8083  weight=5;
    }

    server {
        listen 80;
        server_name 127.0.0.1;
        charset utf-8;
        location / {
            root html;
            index index.html index.htm index.jsp;
            proxy_pass http://myClusterServer1;
            proxy_redirect defaut;
            proxy_connect_timeout 10;
        }
        error_page 404 /404.html;
        error_page 500 502 503 503 /50x.html;
        location = /50x.html{
            root html;
        }
    }
}
  • nginx監聽的端口號。
listen       80;
  • 偵聽域名可以有多個,用空格隔開。
server_name  localhost 127.0.0.1;
  • 字符編碼方式
charset utf-8;
  • 設定本虛擬主機的訪問日誌。
    • 關閉日誌可以減少IO,提高性能。
access_log logs/host.access.log  main;
  • http>server>location默認請求配置
location / {
    root html;
    index index.html index.htm index.jsp;
    proxy_pass http://myClusterServer1;
    proxy_redirect defaut;
    proxy_connect_timeout 10;
}
  • 定義服務器的默認網站根目錄位置
root html;
  • 定義首頁索引文件的名稱
index  index.html index.htmindex.jsp;
  • 請求轉向myClusterServer1定義的服務器列表
proxy_pass    http://myClusterServer1;
  • 代理連接超時時間
    根代理服務器連接的超時時間,必須留意這個timeout時間不能超過75秒,當一臺服務器當掉時,過10秒轉發到另外一臺服務器。
proxy_connect_timeout 10;
  • error_page 配置錯誤重定向地址,

可配置一個錯誤重定向,如404錯誤重定向到/404.html 路徑:

error_page 404 /404.html;

亦可批量重定向,如50x錯誤通配符:

error_page   500 502 503 504  /50x.html;
   location = /50x.html {
       root   html;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章