Nginx——配置文件詳解

      網(wǎng)友投稿 696 2022-05-28

      1、nginx參數(shù)詳解

      1.1、nginx.conf 配置文件全覽

      #---全局塊開始---- #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 塊開始==== events { worker_connections 1024; } #====events 塊結束==== #****http 塊開始**** 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; #} } } #****http 塊結束****

      1.1.1、第一部分:全局塊

      從配置文件開始到 events 之間的內(nèi)容,主要會設置一些影響 nginx 服務器整體運行的配 置參數(shù)。主要包括配置運行 Nginx 服務器的用戶(組)、允許生成的 worker process 數(shù),進 程 PID 存放路徑、日志存放路徑和類型以及配置文件的引入等。

      #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;

      worker_processes 是 Nginx 服務器并發(fā)處理服務的關鍵配置,值越大,可以支持的并發(fā)處理 量也越多,但是會受到硬件、軟件等設備的制約。

      error_log 配置 nginx 日志文件的全路徑名

      pid 配置進程 PID 存放路徑

      1.1.2、第二部分:events 塊

      events { worker_connections 1024; }

      events 塊涉及的參數(shù)主要影響 Nginx 服務器與用戶的網(wǎng)絡連接,常用的設置包括是否開 啟對多 work process 下的網(wǎng)絡連接進行序列化,是否允許同時接受多個網(wǎng)絡連接,選取哪種 事件驅(qū)動模型來處理連接請求,每個 work process 可以同時支持的最大連接數(shù)等。

      上述的例子表示每個 work process 支持的最大連接數(shù)為 1024。這部分的配置對 Nginx 的性能影響比較大,在實際中應該靈活配置。

      1.1.3、第三部分:http塊{}

      Nginx——配置文件詳解

      http 全局塊配置的指令包括文件引入、MIME-TYPE 定義、連接超時時間、單鏈接請求數(shù)上 限等。

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

      這塊和虛擬主機有密切關系,虛擬主機從用戶角度看,和一臺獨立的硬件主機是完全一 樣的,該技術的產(chǎn)生是為了節(jié)省互聯(lián)網(wǎng)服務器硬件成本。

      每個 http 塊可以包括多個 server 塊,而每個 server 塊就相當于一個虛擬主機。而每個 server 塊也分為全局 server 塊,以及可以同時包含多個 location 塊。

      #gzip on; server { listen 80;#監(jiān)聽的端口號 server_name localhost;#監(jiān)聽的域名 #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; #} }

      全局 server 塊

      最常見的配置是本虛擬主機的監(jiān)聽配置和本虛擬主機的名稱或 IP 配置。

      location 塊

      一個 server 塊可以配置多個 location 塊。

      這塊的主要作用是基于 Nginx 服務器接受到的請求字符串(例如 server_name/uri-string), 對虛擬主機名稱(也可以是 IP 別名)之外的字符串(列如 前面的/uri-string)進行匹配,對 特定的請求進行處理。地址定向、數(shù)據(jù)緩存和應答控制等功能,還有許多第三方模塊的配置 也在這里進行。

      1.2、 工作模式與連接數(shù)上限

      #user nobody; worker_processes 1; events { use epoll; worker_connections 1024; }

      1. 用戶與工作進程

      #user nobody; worker_processes 1; [root@nginx1 conf]# ps aux |grep nginx root 1170 0.0 0.0 22568 680 ? Ss 09:14 0:00 nginx: master process /opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf nobody 1171 0.0 0.1 23020 1288 ? S 09:14 0:00 nginx: worker process root 1174 0.0 0.0 103264 876 pts/0 S+ 09:14 0:00 grep nginx [root@nginx1 conf]# ps aux |grep nginx root 1170 0.0 0.0 22568 680 ? Ss 09:14 0:00 nginx: master process /opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf nobody 1171 0.0 0.1 23020 1288 ? S 09:14 0:00 nginx: worker process [root@nginx1 conf]# id nobody uid=99(nobody) gid=99(nobody) groups=99(nobody) [root@nginx1 conf]# cat /etc/passwd root:x:0:0:root:/root:/bin/bash …… nobody:x:99:99:Nobody:/:/sbin/nologin

      2. use epoll;

      參考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll 模型是 Linux 2.6 以上版本內(nèi)核中的高性能網(wǎng)絡 I/O 模型,如果跑在 FreeBSD上面,就用 kqueue 模型。

      3. worker_connections 1024;

      單個后臺 worker process 進程的最大并發(fā)鏈接數(shù)。 并發(fā)總數(shù)是 worker_processes 和 worker_connections 的乘積,即 max_clients = worker_processes * worker_connections 在 設 置 了 反 向 代 理 的 情 況 下 , max_clients=worker_processes * worker_connections / 4 為什么上面反向代理要除以 4,應該說是一個經(jīng)驗值 根據(jù)以上條件,正常情況下的 Nginx Server 可以應付的最大連接數(shù)為:4 * 8000 = 32000 #worker_connections 值的設置跟物理內(nèi)存大小有關 #因為并發(fā)受 IO 約束,max_clients 的值須小于系統(tǒng)可以打開的最大文件數(shù) 系統(tǒng)可以打開的最大文件數(shù)和內(nèi)存大小成正比,一般 1GB 內(nèi)存的機器上可以打開的文件數(shù) 大約是 10 萬左右 #我們來看看 360M 內(nèi)存的 VPS 可以打開的文件句柄數(shù)是多少: #$ cat /proc/sys/fs/file-max #輸出 34336 # 32000 < 34336,即并發(fā)連接總數(shù)小于系統(tǒng)可以打開的文件句柄總數(shù),這樣就在操 作系統(tǒng)可以承受的范圍之內(nèi) #worker_connections 的值需根據(jù) worker_processes 進程數(shù)目和系統(tǒng)可以 打開的最大文件總數(shù)進行適當?shù)剡M行設置 #使得并發(fā)總數(shù)小于操作系統(tǒng)可以打開的最大文件數(shù)目 #其實質(zhì)也就是根據(jù)主機的物理 CPU 和內(nèi)存進行配置 #當然,理論上的并發(fā)總數(shù)可能會和實際有所偏差,因為主機還有其他的工作進程需 要消耗系統(tǒng)資源。 # ulimit -SHn 65535 設置可以打開的文件數(shù)量

      1.3、開啟零拷貝

      sendfile on; #tcp_nopush on;

      sendfile 實際上是 Linux2.0+以后的推出的一個系統(tǒng)調(diào)用,web 服務器可以通過調(diào)整自 身的配置來決定是否利用 sendfile 這個系統(tǒng)調(diào)用。先來看一下不用 sendfile 的傳統(tǒng)網(wǎng) 絡傳輸過程:

      read(file,tmp_buf, len); write(socket,tmp_buf, len); 硬盤 >> kernel buffer >> user buffer>> kernel socket buffer >>協(xié)議棧

      一個基于 socket 的服務,首先讀硬盤數(shù)據(jù),然后寫數(shù)據(jù)到 socket 來完成網(wǎng)絡傳輸?shù)摹?/p>

      上面 2 行用代碼解釋了這一點,不過上面 2 行簡單的代碼掩蓋了底層的很多操作。來看看 底層是怎么執(zhí)行上面 2 行代碼的:

      1、系統(tǒng)調(diào)用 read()產(chǎn)生一個上下文切換:從 user mode 切換到 kernel mode,然 后 DMA 執(zhí)行拷貝,把文件數(shù)據(jù)從硬盤讀到一個 kernel buffer 里。 2、數(shù)據(jù)從 kernel buffer 拷貝到 user buffer,然后系統(tǒng)調(diào)用 read() 返回,這時 又產(chǎn)生一個上下文切換:從 kernel mode 切換到 user mode。 3、 系統(tǒng)調(diào)用 write()產(chǎn)生一個上下文切換:從 user mode 切換到 kernel mode,然 后把步驟 2 讀到 user buffer 的數(shù)據(jù)拷貝到 kernel buffer (數(shù)據(jù)第 2 次拷貝到 kernel buffer),不過這次是個不同的 kernel buffer,這個 buffer 和 socket 相關聯(lián)。 4、系統(tǒng)調(diào)用 write()返回,產(chǎn)生一個上下文切換:從 kernel mode 切換到 user mode ,然后 DMA 從 kernel buffer 拷貝數(shù)據(jù)到協(xié)議棧。

      上面 4 個步驟有 4 次上下文切換,有 4 次拷貝,我們發(fā)現(xiàn)如果能減少切換次數(shù)和拷貝次數(shù) 將會有效提升性能。在 kernel2.0+ 版本中,系統(tǒng)調(diào)用 sendfile() 就是用來簡化上面 步驟提升性能的。sendfile() 不但能減少切換次數(shù)而且還能減少拷貝次數(shù)。

      再來看一下用 sendfile()來進行網(wǎng)絡傳輸?shù)倪^程:

      sendfile(socket,file, len); 硬盤 >> kernel buffer (快速拷貝到 kernelsocket buffer) >>協(xié)議棧

      1、 系統(tǒng)調(diào)用 sendfile()通過 DMA 把硬盤數(shù)據(jù)拷貝到 kernel buffer,然后數(shù)據(jù)被 kernel 直接拷貝到另外一個與 socket 相關的 kernel buffer。 這里沒有 user mode 和 kernel mode 之間的切換,在 kernel 中直接完成了從一個 buffer 到另一個 buffer 的拷貝。 2、DMA 把數(shù)據(jù)從 kernelbuffer 直接拷貝給協(xié)議棧,沒有切換,也不需要數(shù)據(jù)從 user mode 拷貝到 kernel mode,因為數(shù)據(jù)就在 kernel 里。

      簡單說,sendfile 是個比 read 和 write 更高性能的系統(tǒng)接口, 不過需要注意的是, sendfile 是將 in_fd 的內(nèi)容發(fā)送到 out_fd 。而 in_fd 不能是 socket , 也就是 只能文件句柄。 所以當 Nginx 是一個靜態(tài)文件服務器的時候,開啟 SENDFILE 配置項 能大大提高 Nginx 的性能。 但是當 Nginx 是作為一個反向代理來使用的時候, SENDFILE 則沒什么用了,因為 Nginx 是反向代理的時候。 in_fd 就不是文件句柄而 是 socket,此時就不符合 sendfile 函數(shù)的參數(shù)要求了。

      1.4、keepalive_timeout

      keepalive_timeout 65;

      測試時改為 0,便于看出負載切換的效果,部署到生產(chǎn)前進行優(yōu)化來提高效率。

      1.5、是否啟用壓縮

      #gzip on;

      壓縮可以有效減少文件的大小,有利于網(wǎng)絡傳輸。

      1.6、autoindex

      autoindex on; #開啟目錄列表訪問,合適下載服務器,默認關閉。

      1.7、nginx 虛擬主機演示

      虛擬主機,就是將一臺物理服務器虛擬為多個服務器來使用,從而實現(xiàn)在一臺服務器上 配置多個站點,即可以在一臺物理主機上配置多個域名。Nginx 中,一個 server 標簽 就是一臺虛擬主機,配置多個 server 標簽就虛擬出了多臺主機。Nginx 虛擬主機的實 現(xiàn)方式有兩種:域名虛擬方式與端口虛擬方式。域名虛擬方式是指不同的虛擬機使用不同的 域名,通過不同的域名虛擬出不同的主機;端口虛擬方式是指不同的虛擬機使用相同的域名 不同的端口號,通過不同的端口號虛擬出不同的主機。基于端口的虛擬方式不常用。

      修改 nginx.conf 文件

      gzip on; server { listen 80; server_name www.sxthenhao.com; location / { root /mnt; autoindex on; } } server { listen 80; server_name www.123.com; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } ......

      重新加載 nginx

      [root@nginx1 conf]# service nginx reload

      修改本機 hosts 文件(C:\Windows\System32\drivers\etc)

      192.168.20.11 nginx1 www.123.com www.sxthenhao.com

      訪問測試

      自動提供了一個歡迎頁面,由于/mnt 下什么也沒有掛載,所以列表中什么也沒有。

      為/mnt 掛載,并重新測試

      [root@nginx1 conf]# mount /dev/cdrom /mnt

      對比下圖

      1.8、 日志配置

      Nginx 還可以作為日志服務器

      [root@nginx1 logs]# pwd /opt/nginx/logs [root@nginx1 logs]# tail -f access.log

      本地瀏覽器訪問:http://www.123.com/2019-12-03maxwd19

      先不管 404 的問題,查看日志多了一天記錄

      修改一下 http://www.123.com/2019-12-04maxwd20,日志又記錄一條

      當然日志格式我們也可以自定義

      access_log 配置 http 下,多 server 公用,配置 http->某 server 下,僅對該 server 使用。

      http://www.sxthenhao.com/2019-12-04maxwd20myfmt.log 日志記錄多一條

      http://www.123.com/2019-12-04maxwd20 日志記錄 access.log 多一條(并沒有 使用 myfmt.log)

      1.9、 Location(重點)

      參考:

      http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_core_m odule.html 語法 location [ = | ~ | ~* | ^~ ] uri { ... } location @name { ... } 默認值 - 上下文 server, location

      讓我們用一個例子解釋上面的說法:

      location = / {

      [ configuration A ]

      }

      location / {

      [ configuration B ]

      }

      location /documents/ {

      [ configuration C ]

      }

      location ^~ /images/ {

      [ configuration D ]

      }

      location ~* .(gif|jpg|jpeg)$ {

      [ configuration E ]

      }

      請求" / “匹配配置 A,

      請求” /index.html “匹配配置 B,

      請求”/documents/document.html"匹配配置 C,

      請求"/images/1.gif"匹配配置 D,

      請求“/documents/1.jpg”匹配配置 E。

      修改 nginx.conf 配置文件

      server { listen 80; server_name www.sxthenhao.com; access_log logs/myfmt.log myfmt; location / { root /mnt; autoindex on; } location /aabb { proxy_pass http://192.168.20.102/;#帶上/訪問該 url 對應的首頁, #不帶/ 訪問 http://192.168.20.102/aabb } }

      重新加載 nginx

      [root@nginx1 conf]# !ser

      訪問測試

      http://www.sxthenhao.com/ooxx

      修改 nginx.conf

      location /ooxx {

      proxy_pass http://www.baidu.com/;

      }

      重啟 nginx

      [root@nginx1 conf]# !ser

      如果重啟沒有問題,直接跳步驟 7

      如果出現(xiàn)下圖所示的錯誤:

      找不到域名,也就是訪問不到域名解析服務器。

      解決辦法:

      訪問測試 http://www.sxthenhao.com/ooxx

      雖然訪問到了百度,但是確實通過重定向的方式,以后發(fā)生的事情和我們的服務器就沒有半 毛錢關系了。

      優(yōu)化配置 nginx.conf:

      #盡量在服務器端跳轉(zhuǎn),不要在客戶端跳轉(zhuǎn)

      proxy_pass https://www.baidu.com/;

      重啟 nginx,再次測試,地址欄沒有重定向,但是當我們查詢(比如:ssd)時出現(xiàn)

      修改 nginx.conf

      location /ooxx { proxy_pass http://www.baidu.com/; } location ~* /s.* { proxy_pass https://www.baidu.com; }

      1.10、 Bug https protocol requires SSL support in

      [root@nginx1 conf]# service nginx reload nginx: [emerg] https protocol requires SSL support in /opt/nginx/conf/nginx.conf:45 nginx: configuration file /opt/nginx/conf/nginx.conf test failed

      當初編譯的時候沒有啟用 SSL 支持,在配置反向代理到 https 的網(wǎng)站時,編輯配置文件報 錯,無法啟動 nginx。

      解決辦法:先將 nginx.conf 備份/root/目錄下,刪除/opt/nginx 和/opt/apps/ nginx-1.16.1,然后在解壓一份,最后編譯安裝。

      [root@nginx1 nginx-1.16.1]# ./configure --prefix=/opt/nginx --with-http_ssl_module [root@nginx1 nginx-1.16.1]# make && make install [root@nginx1 nginx-1.16.1]# cd /opt/nginx/conf/ [root@nginx1 conf]# cp /root/nginx.conf ./ cp: overwrite `./nginx.conf'? yes [root@nginx1 conf]# service nginx reload nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok nginx: configuration file /opt/nginx/conf/nginx.conf test is successful Reloading nginx:

      然后再訪問 http://www.sxthenhao.com/ooxx

      我是小白弟弟,一個在互聯(lián)網(wǎng)行業(yè)的小白,立志成為一名架構師

      https://blog.csdn.net/zhouhengzhe?t=1

      kernel Nginx

      版權聲明:本文內(nèi)容由網(wǎng)絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權內(nèi)容。

      上一篇:Tomcat - Tomcat 網(wǎng)絡通信模型剖析 &amp; 并發(fā)參數(shù)解讀
      下一篇:JNI 學習筆記系列(一)
      相關文章
      亚洲熟妇无码八AV在线播放| 亚洲熟妇av一区二区三区漫画| 久久久久久久久亚洲| 国产亚洲精品线观看动态图| 亚洲国产综合人成综合网站| 免费亚洲视频在线观看| 国产亚洲男人的天堂在线观看| 亚洲一区二区三区写真| 亚洲码和欧洲码一码二码三码 | 亚洲成av人无码亚洲成av人| 亚洲欧洲专线一区| 亚洲国产精品网站在线播放 | 亚洲avav天堂av在线网毛片| 亚洲码和欧洲码一码二码三码| 亚洲AV无码XXX麻豆艾秋| 亚洲a∨无码一区二区| 亚洲av无码成人精品区一本二本| 国产在亚洲线视频观看| 亚洲精品偷拍视频免费观看 | 亚洲AV永久精品爱情岛论坛| 久久亚洲一区二区| 亚洲综合激情六月婷婷在线观看| 亚洲精品美女久久久久| 亚洲一区电影在线观看| 亚洲中文字幕无码中文字| 亚洲AV成人无码网站| 亚洲成?v人片天堂网无码| 久久精品亚洲男人的天堂| 久久久亚洲精品蜜桃臀| 久久夜色精品国产亚洲AV动态图| 亚洲一二成人精品区| 亚洲图片激情小说| 自拍偷区亚洲国内自拍| 亚洲AV网一区二区三区| 久久亚洲AV无码西西人体| 亚洲av永久无码精品表情包| 亚洲第一页在线观看| 亚洲欧美日韩国产精品一区| 日韩色日韩视频亚洲网站| 亚洲一区二区精品视频| 亚洲av无码无在线观看红杏|