nginx配置文件結構和講解

在nginx.conf的註釋符號位#

nginx文件的結構,這個對剛入門的同學,可以多看兩眼。

默認的config:

複製代碼
#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;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # 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 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
複製代碼

nginx文件結構:

按 Ctrl+C 複製代碼
...              #全局塊


events {         #events塊
   ...
}


http      #http塊
{
    ...   #http全局塊
    server        #server塊
    { 
        ...       #server全局塊
        location [PATTERN]   #location塊
        {
            ...
        }
        location [PATTERN] 
        {
            ...
        }
    }
    server
    {
      ...
    }
    ...     #http全局塊
}
按 Ctrl+C 複製代碼

1、全局塊:配置影響nginx全局的指令。一般有運行nginx服務器的用戶組,nginx進程pid存放路徑,日誌存放路徑,配置文件引入,允許生成worker process數等。

2、events塊:配置影響nginx服務器或與用戶的網絡連接。有每個進程的最大連接數,選取哪種事件驅動模型處理連接請求,是否允許同時接受多個網路連接,開啓多個網絡連接序列化等。

3、http塊:可以嵌套多個server,配置代理,緩存,日誌定義等絕大多數功能和第三方模塊的配置。如文件引入,mime-type定義,日誌自定義,是否使用sendfile傳輸文件,連接超時時間,單連接請求數等。

4、server塊:配置虛擬主機的相關參數,一個http中可以有多個server。

5、location塊:配置請求的路由,以及各種頁面的處理情況。

下面給大家上一個配置文件,作爲理解,同時也配入我搭建的一臺測試機中,給大家示例。

複製代碼
########### 每個指令必須有分號結束。#################
#user administrator administrators;  #配置用戶或者組,默認爲nobody nobody。
#worker_processes 2;  #允許生成的進程數,默認爲1
#pid /nginx/pid/nginx.pid;   #指定nginx進程運行文件存放地址
error_log log/error.log debug;  #制定日誌路徑,級別。這個設置可以放入全局塊,http塊,server塊,級別以此爲:debug|info|notice|warn|error|crit|alert|emerg
events {
    accept_mutex on;   #設置網路連接序列化,防止驚羣現象發生,默認爲on
    multi_accept on;  #設置一個進程是否同時接受多個網絡連接,默認爲off
    #use epoll;      #事件驅動模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
    worker_connections  1024;    #最大連接數,默認爲512
}
http {
    include       mime.types;   #文件擴展名與文件類型映射表
    default_type  application/octet-stream; #默認文件類型,默認爲text/plain
    #access_log off; #取消服務日誌    
    log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定義格式
    access_log log/access.log myFormat;  #combined爲日誌格式的默認值
    sendfile on;   #允許sendfile方式傳輸文件,默認爲off,可以在http塊,server塊,location塊。
    sendfile_max_chunk 100k;  #每個進程每次調用傳輸數量不能大於設定的值,默認爲0,即不設上限。
    keepalive_timeout 65;  #連接超時時間,默認爲75s,可以在http,server,location塊。

    upstream mysvr {   
      server 127.0.0.1:7878;
      server 192.168.10.121:3333 backup;  #熱備
    }
    error_page 404 https://www.baidu.com; #錯誤頁
    server {
        keepalive_requests 120; #單連接請求上限次數。
        listen       4545;   #監聽端口
        server_name  127.0.0.1;   #監聽地址       
        location  ~*^.+$ {       #請求的url過濾,正則匹配,~爲區分大小寫,~*爲不區分大小寫。
           #root path;  #根目錄
           #index vv.txt;  #設置默認頁
           proxy_pass  http://mysvr;  #請求轉向mysvr 定義的服務器列表
           deny 127.0.0.1;  #拒絕的ip
           allow 172.18.5.54; #允許的ip           
        } 
    }
}
複製代碼

上面是nginx的基本配置,需要注意的有以下幾點:

1、1.remoteaddrremoteaddr與http_x_forwarded_for 用以記錄客戶端的ip地址; 2.remoteuser3.remoteuser:用來記錄客戶端用戶名稱;3.time_local : 用來記錄訪問時間與時區;4.$request : 用來記錄請求的url與http協議;

  5.status2006.status:用來記錄請求狀態;成功是200,6.body_bytes_s ent :記錄發送給客戶端文件主體內容大小;7.httpreferer8.httpreferer:用來記錄從那個頁面鏈接訪問過來的;8.http_user_agent :記錄客戶端瀏覽器的相關信息;

2、驚羣現象:一個網路連接到來,多個睡眠的進程被同事叫醒,但只有一個進程能獲得鏈接,這樣會影響系統性能。

3、每個指令必須有分號結束。

 

=       開頭表示精確匹配
        如 A 中只匹配根目錄結尾的請求,後面不能帶任何字符串。
^~      開頭表示uri以某個常規字符串開頭,不是正則匹配
~       開頭表示區分大小寫的正則匹配;
~*      開頭表示不區分大小寫的正則匹配
/      通用匹配, 如果沒有其它匹配,任何請求都會匹配到



在此記錄下Nginx服務器nginx.conf的配置文件說明, 部分註釋收集與網絡.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#運行用戶
user www-data; 
#啓動進程,通常設置成和cpu的數量相等
worker_processes 1;
 
#全局錯誤日誌及PID文件
error_log /var/log/nginx/error.log;
pid    /var/run/nginx.pid;
 
#工作模式及連接數上限
events {
  use  epoll;       #epoll是多路複用IO(I/O Multiplexing)中的一種方式,但是僅用於linux2.6以上內核,可以大大提高nginx的性能
  worker_connections 1024;#單個後臺worker process進程的最大併發鏈接數
  # multi_accept on;
}
 
#設定http服務器,利用它的反向代理功能提供負載均衡支持
http {
   #設定mime類型,類型由mime.type文件定義
  include    /etc/nginx/mime.types;
  default_type application/octet-stream;
  #設定日誌格式
  access_log  /var/log/nginx/access.log;
 
  #sendfile 指令指定 nginx 是否調用 sendfile 函數(zero copy 方式)來輸出文件,對於普通應用,
  #必須設爲 on,如果用來進行下載等應用磁盤IO重負載應用,可設置爲 off,以平衡磁盤與網絡I/O處理速度,降低系統的uptime.
  sendfile    on;
  #tcp_nopush   on;
 
  #連接超時時間
  #keepalive_timeout 0;
  keepalive_timeout 65;
  tcp_nodelay    on;
   
  #開啓gzip壓縮
  gzip on;
  gzip_disable "MSIE [1-6]\.(?!.*SV1)";
 
  #設定請求緩衝
  client_header_buffer_size  1k;
  large_client_header_buffers 4 4k;
 
  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/*;
 
  #設定負載均衡的服務器列表
   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 {
  #偵聽80端口
    listen    80;
    #定義使用www.xx.com訪問
    server_name www.xx.com;
 
    #設定本虛擬主機的訪問日誌
    access_log logs/www.xx.com.access.log main;
 
  #默認請求
  location / {
     root  /root;   #定義服務器的默認網站根目錄位置
     index index.php index.html index.htm;  #定義首頁索引文件的名稱
 
     fastcgi_pass www.xx.com;
     fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
     include /etc/nginx/fastcgi_params;
    }
 
  # 定義錯誤提示頁面
  error_page  500 502 503 504 /50x.html;
    location = /50x.html {
    root  /root;
  }
 
  #靜態文件,nginx自己處理
  location ~ ^/(images|javascript|js|css|flash|media|static)/ {
    root /var/www/virtual/htdocs;
    #過期30天,靜態文件不怎麼更新,過期可以設大一點,如果頻繁更新,則可以設置得小一點。
    expires 30d;
  }
  #PHP 腳本請求全部轉發到 FastCGI處理. 使用FastCGI默認配置.
  location ~ \.php$ {
    root /root;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /home/www/www$fastcgi_script_name;
    include fastcgi_params;
  }
  #設定查看Nginx狀態的地址
  location /NginxStatus {
    stub_status      on;
    access_log       on;
    auth_basic       "NginxStatus";
    auth_basic_user_file conf/htpasswd;
  }
  #禁止訪問 .htxxx 文件
  location ~ /\.ht {
    deny all;
  }
    
   }
}

以上是一些基本的配置,使用Nginx最大的好處就是負載均衡

如果要使用負載均衡的話,可以修改配置http節點如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#設定http服務器,利用它的反向代理功能提供負載均衡支持
http {
   #設定mime類型,類型由mime.type文件定義
  include    /etc/nginx/mime.types;
  default_type application/octet-stream;
  #設定日誌格式
  access_log  /var/log/nginx/access.log;
 
  #省略上文有的一些配置節點
 
  #。。。。。。。。。。
 
  #設定負載均衡的服務器列表
   upstream mysvr {
  #weigth參數表示權值,權值越高被分配到的機率越大
  server 192.168.8.1x:3128 weight=5;#本機上的Squid開啓3128端口
  server 192.168.8.2x:80 weight=1;
  server 192.168.8.3x:80 weight=6;
  }
 
  upstream mysvr2 {
  #weigth參數表示權值,權值越高被分配到的機率越大
 
  server 192.168.8.x:80 weight=1;
  server 192.168.8.x:80 weight=6;
  }
 
  #第一個虛擬服務器
  server {
  #偵聽192.168.8.x的80端口
    listen    80;
    server_name 192.168.8.x;
 
   #對aspx後綴的進行負載均衡請求
  location ~ .*\.aspx$ {
 
     root  /root;   #定義服務器的默認網站根目錄位置
     index index.php index.html index.htm;  #定義首頁索引文件的名稱
 
     proxy_pass http://mysvr ;#請求轉向mysvr 定義的服務器列表
 
     #以下是一些反向代理的配置可刪除.
 
     proxy_redirect off;
 
     #後端的Web服務器可以通過X-Forwarded-For獲取用戶真實IP
     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; #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服務器傳
 
    }
 
   }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章