日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

nginx.conf配置——Nginx

 一本正經(jīng)地胡鬧 2019-07-25

Nginx在運(yùn)行時(shí)候,至少要加載幾個(gè)核心模塊和一個(gè)事件類模塊。這些模塊運(yùn)行時(shí)所支持的配置項(xiàng)稱為基本配置——所有其他模塊執(zhí)行時(shí)都依賴的配置項(xiàng)。

由于配置項(xiàng)較多,所以把它們按照用戶使用時(shí)的預(yù)期功能分成以下4類:

  1. 用于調(diào)試、定位問(wèn)題的配置項(xiàng);
  2. 正常運(yùn)行的必備配置項(xiàng);
  3. 優(yōu)化性能的配置項(xiàng);
  4. 事件類配置項(xiàng)(有些事件類配置項(xiàng)歸納到優(yōu)化性能類,這是因?yàn)樗鼈冸m然也屬于event{}塊,但作用是優(yōu)化性能)

有一些配置項(xiàng),幾十沒(méi)有顯式的進(jìn)行配置,他們會(huì)有默認(rèn)的值,如:daemon,即是在nginx.conf中沒(méi)有對(duì)它進(jìn)行配置,也相當(dāng)于打開(kāi)了這個(gè)功能,這點(diǎn)需要注意。

官網(wǎng)對(duì)各個(gè)模塊參數(shù)配置的解釋說(shuō)明網(wǎng)址: Nginx中文文檔

  1. ##代碼塊中的events、http、server、location、upstream等都是塊配置項(xiàng)##
  2. ##塊配置項(xiàng)可以嵌套。內(nèi)層塊直接繼承外層快,例如:server塊里的任意配置都是基于http塊里的已有配置的##
  3. ##Nginx worker進(jìn)程運(yùn)行的用戶及用戶組
  4. #語(yǔ)法:user username[groupname] 默認(rèn):user nobody nobody
  5. #user用于設(shè)置master進(jìn)程啟動(dòng)后,fork出的worker進(jìn)程運(yùn)行在那個(gè)用戶和用戶組下。當(dāng)按照"user username;"設(shè)置時(shí),用戶組名與用戶名相同。
  6. #若用戶在configure命令執(zhí)行時(shí),使用了參數(shù)--user=usergroup 和 --group=groupname,此時(shí)nginx.conf將使用參數(shù)中指定的用戶和用戶組。
  7. #user nobody;
  8. ##Nginx worker進(jìn)程個(gè)數(shù):其數(shù)量直接影響性能。
  9. #每個(gè)worker進(jìn)程都是單線程的進(jìn)程,他們會(huì)調(diào)用各個(gè)模塊以實(shí)現(xiàn)多種多樣的功能。如果這些模塊不會(huì)出現(xiàn)阻塞式的調(diào)用,那么,有多少CPU內(nèi)核就應(yīng)該配置多少個(gè)進(jìn)程,反之,有可能出現(xiàn)阻塞式調(diào)用,那么,需要配置稍多一些的worker進(jìn)程。
  10. worker_processes 1;
  11. ##ssl硬件加速。
  12. #用戶可以用OpneSSL提供的命令來(lái)查看是否有ssl硬件加速設(shè)備:openssl engine -t
  13. #ssl_engine device;
  14. ##守護(hù)進(jìn)程(daemon)。是脫離終端在后臺(tái)允許的進(jìn)程。它脫離終端是為了避免進(jìn)程執(zhí)行過(guò)程中的信息在任何終端上顯示。這樣一來(lái),進(jìn)程也不會(huì)被任何終端所產(chǎn)生的信息所打斷。##
  15. ##關(guān)閉守護(hù)進(jìn)程的模式,之所以提供這種模式,是為了放便跟蹤調(diào)試nginx,畢竟用gdb調(diào)試進(jìn)程時(shí)最繁瑣的就是如何繼續(xù)跟進(jìn)fork出的子進(jìn)程了。##
  16. ##如果用off關(guān)閉了master_proccess方式,就不會(huì)fork出worker子進(jìn)程來(lái)處理請(qǐng)求,而是用master進(jìn)程自身來(lái)處理請(qǐng)求
  17. #daemon off; #查看是否以守護(hù)進(jìn)程的方式運(yùn)行Nginx 默認(rèn)是on
  18. #master_process off; #是否以master/worker方式工作 默認(rèn)是on
  19. ##error日志的設(shè)置#
  20. #語(yǔ)法: error_log /path/file level;
  21. #默認(rèn): error_log / log/error.log error;
  22. #當(dāng)path/file 的值為 /dev/null時(shí),這樣就不會(huì)輸出任何日志了,這也是關(guān)閉error日志的唯一手段;
  23. #leve的取值范圍是debug、info、notice、warn、error、crit、alert、emerg從左至右級(jí)別依次增大。
  24. #當(dāng)level的級(jí)別為error時(shí),error、crit、alert、emerg級(jí)別的日志就都會(huì)輸出。大于等于該級(jí)別會(huì)輸出,小于該級(jí)別的不會(huì)輸出。
  25. #如果設(shè)定的日志級(jí)別是debug,則會(huì)輸出所有的日志,這一數(shù)據(jù)量會(huì)很大,需要預(yù)先確保/path/file所在的磁盤(pán)有足夠的磁盤(pán)空間。級(jí)別設(shè)定到debug,必須在configure時(shí)加入 --with-debug配置項(xiàng)。
  26. #error_log logs/error.log;
  27. #error_log logs/error.log notice;
  28. #error_log logs/error.log info;
  29. ##pid文件(master進(jìn)程ID的pid文件存放路徑)的路徑
  30. #pid logs/nginx.pid;
  31. events {
  32. #僅對(duì)指定的客戶端輸出debug級(jí)別的日志: 語(yǔ)法:debug_connection[IP|CIDR]
  33. #這個(gè)設(shè)置項(xiàng)實(shí)際上屬于事件類配置,因此必須放在events{……}中才會(huì)生效。它的值可以是IP地址或者是CIRD地址。
  34. #debug_connection 10.224.66.14; #或是debug_connection 10.224.57.0/24
  35. #這樣,僅僅以上IP地址的請(qǐng)求才會(huì)輸出debug級(jí)別的日志,其他請(qǐng)求仍然沿用error_log中配置的日志級(jí)別。
  36. #注意:在使用debug_connection前,需確保在執(zhí)行configure時(shí)已經(jīng)加入了--with-debug參數(shù),否則不會(huì)生效。
  37. worker_connections 1024;
  38. }
  39. ##核心轉(zhuǎn)儲(chǔ)(coredump):在Linux系統(tǒng)中,當(dāng)進(jìn)程發(fā)生錯(cuò)誤或收到信號(hào)而終止時(shí),系統(tǒng)會(huì)將進(jìn)程執(zhí)行時(shí)的內(nèi)存內(nèi)容(核心映像)寫(xiě)入一個(gè)文件(core文件),以作為調(diào)試只用,這就是所謂的核心轉(zhuǎn)儲(chǔ)(coredump).
  40. http {
  41. ##嵌入其他配置文件 語(yǔ)法:include /path/file
  42. #參數(shù)既可以是絕對(duì)路徑也可以是相對(duì)路徑(相對(duì)于Nginx的配置目錄,即nginx.conf所在的目錄)
  43. include mime.types;
  44. default_type application/octet-stream;
  45. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  46. # '$status $body_bytes_sent "$http_referer" '
  47. # '"$http_user_agent" "$http_x_forwarded_for"';
  48. #access_log logs/access.log main;
  49. sendfile on;
  50. #tcp_nopush on;
  51. #keepalive_timeout 0;
  52. keepalive_timeout 65;
  53. #gzip on;
  54. server {
  55. ##listen監(jiān)聽(tīng)的端口
  56. #語(yǔ)法:listen address:port [ default(deprecated in 0.8.21) | default_server | [ backlog=num | rcvbuf=size | sndbuf=size | accept_filter=filter | deferred | bind | ssl ] ]
  57. #default_server: 如果沒(méi)有設(shè)置這個(gè)參數(shù),那么將會(huì)以在nginx.conf中找到的第一個(gè)server塊作為默認(rèn)server塊
  58. listen 8080;
  59. #主機(jī)名稱:其后可以跟多個(gè)主機(jī)名稱,開(kāi)始處理一個(gè)HTTP請(qǐng)求時(shí),nginx會(huì)取出header頭中的Host,與每個(gè)server中的server_name進(jìn)行匹配,以此決定到底由那一個(gè)server來(lái)處理這個(gè)請(qǐng)求。有可能一個(gè)Host與多個(gè)server塊中的server_name都匹配,這時(shí)會(huì)根據(jù)匹配優(yōu)先級(jí)來(lái)選擇實(shí)際處理的server塊。server_name與Host的匹配優(yōu)先級(jí)見(jiàn)文末。
  60. server_name localhost;
  61. #charset koi8-r;
  62. #access_log logs/host.access.log main;
  63. #location / {
  64. # root html;
  65. # index index.html index.htm;
  66. #}
  67. ##location 語(yǔ)法: location [=|~|~*|^~] /uri/ { ... }
  68. # location的使用實(shí)例見(jiàn)文末。
  69. #注意:location時(shí)有順序的,當(dāng)一個(gè)請(qǐng)求有可能匹配多個(gè)location時(shí),實(shí)際上這個(gè)請(qǐng)求會(huì)被第一個(gè)location處理。
  70. location / {
  71. proxy_pass http://192.168.1.60;
  72. }
  73. #error_page 404 /404.html;
  74. # redirect server error pages to the static page /50x.html
  75. #
  76. error_page 500 502 503 504 /50x.html;
  77. location = /50x.html {
  78. root html;
  79. }
  80. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  81. #
  82. #location ~ \.php$ {
  83. # proxy_pass http://127.0.0.1;
  84. #}
  85. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  86. #
  87. #location ~ \.php$ {
  88. # root html;
  89. # fastcgi_pass 127.0.0.1:9000;
  90. # fastcgi_index index.php;
  91. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  92. # include fastcgi_params;
  93. #}
  94. # deny access to .htaccess files, if Apache's document root
  95. # concurs with nginx's one
  96. #
  97. #location ~ /\.ht {
  98. # deny all;
  99. #}
  100. }
  101. # another virtual host using mix of IP-, name-, and port-based configuration
  102. #
  103. #server {
  104. # listen 8000;
  105. # listen somename:8080;
  106. # server_name somename alias another.alias;
  107. # location / {
  108. # root html;
  109. # index index.html index.htm;
  110. # }
  111. #}
  112. # HTTPS server
  113. #
  114. #server {
  115. # listen 443 ssl;
  116. # server_name localhost;
  117. # ssl_certificate cert.pem;
  118. # ssl_certificate_key cert.key;
  119. # ssl_session_cache shared:SSL:1m;
  120. # ssl_session_timeout 5m;
  121. # ssl_ciphers HIGH:!aNULL:!MD5;
  122. # ssl_prefer_server_ciphers on;
  123. # location / {
  124. # root html;
  125. # index index.html index.htm;
  126. # }
  127. #}
  128. }

 

server_name與Host的匹配優(yōu)先級(jí)
首先選擇所有字符串完全匹配的server_name 如:www.testwab.com
其次選擇通配符在前面的server_name 如:*.testwab.com
其次選擇通配符在后面的server_name 如:www.testwab.*
最后選擇使用正在表達(dá)式才匹配的server_name 如:~^\.testwab\.com$
location的使用實(shí)例
  1. location = / {
  2. # matches the query / only.
  3. [ configuration A ]
  4. }
只有當(dāng)用戶請(qǐng)求是/時(shí),才會(huì)使用該location下的配置
  1. location / {
  2. # matches any query, since all queries begin with /, but regular
  3. # expressions and any longer conventional blocks will be
  4. # matched first.
  5. [ configuration B ]
  6. }
可以匹配所有請(qǐng)求
  1. location ^~ /images/ {
  2. # matches any query beginning with /images/ and halts searching,
  3. # so regular expressions will not be checked.
  4. [ configuration C ]
  5. }

匹配以/images/開(kāi)頭的任何查詢并停止搜索

表示匹配URL時(shí)忽略字母大小寫(xiě)問(wèn)題

  1. location ~* \.(gif|jpg|jpeg)$ {
  2. # matches any request ending in gif, jpg, or jpeg. However, all
  3. # requests to the /images/ directory will be handled by
  4. # Configuration C.
  5. [ configuration D ]
  6. }

匹配任何以gif、jpg或jpeg結(jié)尾的請(qǐng)求。然而,所有

location的使用實(shí)例 —— 以root方式設(shè)置資源路徑

location /download/ {

           root    /opt/wab/html/;

}          [[[意思是有一個(gè)請(qǐng)求的URL是 /download/index/test.html, 那么Web服務(wù)器就會(huì)返回服務(wù)器上 /opt/wab/html/download/index/test.html 文件的內(nèi)容]]]

location的使用實(shí)例 —— 以alias方式設(shè)置資源路徑

alias也是用來(lái)設(shè)置文件資源路徑的,它與root不同點(diǎn)主要在于如何解讀緊跟location后面的uri參數(shù),這將會(huì)致使alias與root以不同的方式將用戶請(qǐng)求映射到真正的磁盤(pán)文件上。

例如:如果有一個(gè)請(qǐng)求的URI是/conf/nginx.conf,而用戶實(shí)際想訪問(wèn)的是 /usr/local/nginx/conf/nginx.conf,則兩種方式如下:

         alias:

                           location  /conf {

                                             alias    /usr/local/nginx/conf

                          }

 

        root:

                         location    /conf{

                                            root    /usr/local/nginx

                         }

使用alias時(shí),在URI向?qū)嶋H文件路徑的映射過(guò)程中,已經(jīng)把location后配置的 /conf這部分字符串丟棄掉了,因此若path中不加/conf這部分,直接映射回的地址是/usr/local/nginx/nginx.conf 與用戶實(shí)際想訪問(wèn)的路徑不符。root可以放置在http、server、location或if塊中,而alias只能放置在location塊中。

location的使用實(shí)例 —— 以index方式訪問(wèn)首頁(yè)

有時(shí),訪問(wèn)站點(diǎn)時(shí)的URI是/ ,這時(shí)返回網(wǎng)站的首頁(yè),而這與root和alias都不同。這里用ngx_http_index_module模塊提供的index配置實(shí)現(xiàn)。index后可以跟多個(gè)文件參數(shù),Nginx將會(huì)按照順序來(lái)訪問(wèn)這些文件。

location  /?。?/p>

   root    path;

           index   /index.html   /html/index.php          /index.php


接受到請(qǐng)求后,Nginx首先會(huì)嘗試訪問(wèn)path/index.php 文件,如果可以訪問(wèn),就直接返回文件內(nèi)容結(jié)束請(qǐng)求,否則再試圖返回path/html/index.php 文件的內(nèi)容,以此類推。(從后向前)

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多