首頁  >  文章  >  運維  >  nginx設定檔結構是什麼

nginx設定檔結構是什麼

WBOY
WBOY轉載
2023-05-11 21:28:171143瀏覽

nginx常用功能

1、http代理,反向代理:作為web伺服器最常用的功能之一,尤其是反向代理。

這裡我給來2張圖,對正向代理與反響代理做個詮釋,具體細節,大家可以翻閱下資料。

nginx設定檔結構是什麼

nginx在做反向代理時,提供效能穩定,並且能夠提供配置靈活的轉送功能。 nginx可以根據不同的正規匹配,採取不同的轉發策略,例如圖片檔案結尾的走文件伺服器,動態頁面走web伺服器,只要你正則寫的沒問題,又有相對應的伺服器解決方案,你就可以隨心所欲的玩。且nginx對回傳結果進行錯誤頁跳轉,異常判斷等。如果被分發的伺服器有異常,他可以將請求重新轉發給另外一台伺服器,然後自動移除異常伺服器。

2、負載平衡

nginx提供的負載平衡策略有2種:內建策略和擴充策略。內建策略為輪詢,加權輪詢,ip hash。擴展策略,就天馬行空,只有你想不到的沒有他做不到的啦,你可以參考所有的負載平衡演算法,給他一一找出來做下實現。

上3個圖,理解這三種負載平衡演算法的實作

nginx設定檔結構是什麼

ip hash演算法,對客戶端請求的ip進行hash操作,然後根據hash結果將同一個客戶端ip的請求分發給同一台伺服器處理,可以解決session不共享的問題。

nginx設定檔結構是什麼

3、web快取

nginx可以對不同的檔案做不同的快取處理,配置靈活,並且支援fastcgi_cache,主要用於對fastcgi的動態程式進行快取。配合第三方的ngx_cache_purge,對制定的url快取內容可以的進行增刪管理。

4、nginx相關位址

原始碼:

官網:

nginx設定檔結構

##如果你下載好啦,你的安裝文件,不妨打開conf資料夾的nginx.conf文件,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檔案結構

...       #全局块
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      
    } 
  }
}

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

1、1.$remote_addr 與$http_x_forwarded_for 用來記錄客戶端的ip位址;2.$remote_user :用來記錄客戶端使用者名稱;3.$time_local :用來記錄存取時間與時區;4.$request : 用來記錄請求的url與http協定;

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

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

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

以上是nginx設定檔結構是什麼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除