三、Nginx配置詳解

nginx.conf配置文件

在解壓後的nginx-1.14.2的conf目錄下有一個nginx.conf配置文件,這個文件

nginx文件結構

...              #全局塊

events {         #events塊
   ...
}

http      #http塊
{
    ...   #http全局塊
    server        #server塊
    { 
        ...       #server全局塊
        location [PATTERN]   #location塊
        {
            ...
        }
        location [PATTERN] 
        {
            ...
        }
    }
    server
    {
      ...
    }
    ...     #http全局塊
}

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           
        } 
    }
}

參考文章

https://blog.csdn.net/milogenius/article/details/79678786

https://www.cnblogs.com/knowledgesea/p/5175711.html

https://www.jianshu.com/p/672593444bcc

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