Nginx基礎配置

來源:互聯網
上載者:User

標籤:虛擬機器主機   設定檔   網路連接   伺服器   server   

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/89/82/wKioL1gWB7uAM1jpAAAT6crvI0A365.png-wh_500x0-wm_3-wmp_4-s_2206704308.png" title="下載.png" alt="wKioL1gWB7uAM1jpAAAT6crvI0A365.png-wh_50" />

查看nginx設定檔分類

主設定檔:

nginx.conf

include conf.d/*.conf

fascgi uwsgi scgi 等協議相關設定檔

nginx.conf檔案結構


主設定檔結構:


main block;#全域塊配置全域生效event{#事件驅動相關配置}http{#http/https協議相關配置段server {...}:#每個server用於定義一個虛擬機器主機;server {...server_namerootaliaslocation [OPERATOR] URL {...if CONDITION {...}}}}stream{#tcp協議配置段}


1.全域塊

全域塊主預設的設定檔從開始到event塊之間的的一部分內容,主要設定影響Nginx整個配置指令,影響全域;

2.event塊

event塊主要涉及到的指令用於響應nginx伺服器與使用者的網路連接,常用於配置時間驅動等模組資訊;

3.http塊

http塊是nginx設定檔中最為重要的塊,http自己的全域塊,包括大多數第三方模組配置也可以添加到這個模組中,server塊也可以包含在這個快中,

4.server塊主要定義虛擬機器主機

5.location塊

每個server塊中可以包含多個location塊,location塊主要用於nginx伺服器接收來自用戶端的請求的URL字串進行匹配處理,nginx許多功能都是在此進行配置的。


正常啟動並執行配置:


http執行個體配置

user nobody;pid /var/log/nginx/nginx.pid;worker_processes auto;worker_rlimit_nofile 1024;master_process on;error_log /var/log/nginx/error.log;--------------------------------------------------------------------------------------------------------------------------events {worker_connections 1024;use epoll;accept_mutex on;}--------------------------------------------------------------------------------------------------------------------------http{include       mime.types;default_type  application/octet-stream;keepalive_timeout  65;gzip  on;server {        listen 80;        server_name app.liaoxz.com;        root   /usr/local/nginx/html/app/;        index  index.html index.htm;}server {        listen 8090;        server_name abb.liaoxz.com;       root /usr/local/nginx/html/abb/;        index index.html;}}



1.cpu相關配置

1.設定worker process 數

worker process是nginx實現高並發的主要關鍵配置,這個建議與cpu核心數一致,

配置work proccess的文法格式:

worker_processes  number;

2.worker_cpu_affinity cpumask ...;

worker_cpu_affinity auto [cpumask];

將worker進程與cpu進行綁定(綁定之後來自同一worker個進程的請求就會直接使用當前所綁定所指定的cpu)

建議設定成auto,auto表示在服務啟動時就將worker進程與之綁定。


使用者及進程相關

1.user uesr [group]

設定背景工作處理序所使用的使用者或組

2.pid /PATH/TO/PID_FILE;

指定儲存nginx主進程進程號碼的檔案路徑;

3.worker_priority number;

指定worker進程的nice值,設定worker進程優先順序;[-20,20]

4.worker_rlimit_nofile number;

worker進程所能夠開啟的檔案數量上限;

5.master_process on |off;

是否以master/worker模型運行nginx


4.include file | mask;

指明包含進來的其它設定檔片斷;

5.load_module file;

指明要裝載的動態模組;




事件驅動相關配置

events{

1)worker_cnnections number;

每個worker進程所能開啟的最大並發串連數

2)use method epoll|select|poll;

指明並發串連請求的處理方法;

建議使用epoll

use epoll;

3)accept_mutex on |off;

處理新的串連要求方法;on意味著由各worker輪流處理新請求,off意味著每個新的請求會通知所有的worker進程

}



Nginx Gzip壓縮


使用模組 ngx_http_gzip_module 

http://nginx.org/en/docs/http/ngx_http_gzip_module.html

1.Syntax:gzip on | off;

Default:

gzip off;

Context:http, server, location, if in location

預設是off,不啟動Gzip功能,設定為on為生效


2.Syntax:gzip_buffers number size;

Default:

gzip_buffers 32 4k|16 8k;

Context:http, server, location

用於指定緩衝區數量及每個緩衝區的大小

number 緩衝區數量

size 緩衝區大小


3。Syntax:gzip_comp_level level;

Default:

gzip_comp_level 1;

Context:http, server, location

指定壓縮程度數字越高表示壓縮效率越高,但是佔用系統資源,建議適當設定


4.Syntax:gzip_disable regex ...;

Default:—

Context:http, server, location

This directive appeared in version 0.6.23.

針對不同類型用戶端進行選擇是否開啟gzip功能

regex 表示瀏覽器類型 支援使用Regex

IE瀏覽器 MSIE [6-10]\;


5.Syntax:gzip_types mime-type ...;

Default:

gzip_types text/html;

Context:http, server, location

壓縮過濾器,僅對此處設定的MIME類型的內容啟用壓縮功能;


6.Syntax:gzip_proxied off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any ...;

Default:

gzip_proxied off;

Context:http, server, location

Enables or disables gzipping of responses for proxied requests depending on the request and response. The fact that the request is proxied is determined by the presence of the “Via” request header field. The directive accepts multiple parameters:


off

disables compression for all proxied requests, ignoring other parameters;

expired

enables compression if a response header includes the “Expires” field with a value that disables caching;

no-cache

enables compression if a response header includes the “Cache-Control” field with the “no-cache” parameter;

no-store

enables compression if a response header includes the “Cache-Control” field with the “no-store” parameter;

private

enables compression if a response header includes the “Cache-Control” field with the “private” parameter;

no_last_modified

enables compression if a response header does not include the “Last-Modified” field;

no_etag

enables compression if a response header does not include the “ETag” field;

auth

enables compression if a request header includes the “Authorization” field;

any

enables compression for all proxied requests.


nginx作為Proxy 伺服器接收到從被Proxy 伺服器發送的響應報文後,在何種條件下啟用壓縮功能的;

off:對代理的請求不啟用

no-cache, no-store,private:表示從被Proxy 伺服器收到的響應報文首部的Cache-Control的值為此三者中任何一個,則啟用壓縮功能;

其他相關請查閱的官方文檔http://nginx.org/en/docs/http/ngx_http_gzip_module.html#gzip_disable


http{


include       mime.types;

default_type  application/octet-stream;

keepalive_timeout  65;

gzip  on;

gzip_comp_level 4;

gzip_disable MISE [4-6]\;

gzip_buffers 32 4k;

}


nginx rewrite功能配置

Rewrite 是nginx提供的一個重要功能,其在web伺服器中必然會使用到的指令,例如在網站結構更改後,用戶端任可以使用之前使用的URL來進行訪問等操作,實現URL替換功能;


Rewrite功能是由ngx_http_rewrite_module模組提供;

1、rewrite regex replacement [flag]

將使用者請求的URI基於regex所描述的模式進行檢查,匹配到時將其替換為replacement指定的新的URI;

注意:如果在同一級配置塊中存在多個rewrite規則,那麼會自下而下逐個檢查;被某條件規則替換完成後,會重新一輪的替換檢查,因此,隱含有迴圈機制;[flag]所表示的標誌位用於控制此迴圈機制;

如果replacement是以http://或https://開頭,則替換結果會直接以重向返回給用戶端;

301:永久重新導向;

[flag]:

last:重寫完成後停止對當前URI在當前location中後續的其它重寫操作,而後對新的URI啟動新一輪重寫檢查;提前重啟新一輪迴圈;


break:重寫完成後停止對當前URI在當前location中後續的其它重寫操作,而後直接跳轉至重寫規則配置塊之後的其它配置;結束迴圈;


redirect:重寫完成後以臨時重新導向方式直接返回重寫後產生的新URI給用戶端,由用戶端重新發起請求;不能以http://或https://開頭;


permanent:重寫完成後以永久重新導向方式直接返回重寫後產生的新URI給用戶端,由用戶端重新發起請求;


        location / {

          rewrite ^/ http://abb.liaoxz.com/abb;  

           root   html;

            index  index.html index.htm;

        }


2.if (condition) { ... }

引入一個新的配置上下文 ;條件滿足時,執行配置塊中的配置指令;server, location;

condition:

比較操作符:

==

!=

~:模式比對,區分字元大小寫;

~*:模式比對,不區分字元大小寫;

!~:模式不匹配,區分字元大小寫;

!~*:模式不匹配,不區分字元大小寫;

檔案及目錄存在性判斷:

-e, !-e

-f, !-f

-d, !-d

-x, !-x


3.return

return code [text];

return code URL;

return URL;

Stops processing and returns the specified code to a client. 

停止處理並將指定的代碼返回給用戶端。 非標準代碼444關閉串連而不發送響應前序。

 

 4.rewrite_log on | off;

是否開啟重寫日誌;



5.set $variable value;

使用者自訂變數 






本文出自 “筆記” 部落格,請務必保留此出處http://snowbamboo1.blog.51cto.com/9291858/1867467

Nginx基礎配置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.