Nginx 配置文件

Nginx 主配置文件

Nginx 主配置文件 nginx.conf 是以區塊的形式組織的。通常,每一個區塊以一個大括號「{}」來表示,區塊能夠分爲幾個層次,整個配置文件中,main 區位於最上層,在 main 區下面能夠有 events 區、http 區等層級,在 http 區中又包含有一個或多個 server 區,每一個 server 區中又可有一個或多個 location 區,Nginx 整個配置文件 nginx.conf 的主體框架爲:php

#user  nobody;                              # main 區塊,Nginx 核心功能模塊 
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 區塊,Nginx 核心功能模塊
    worker_connections  1024;
}

http {                                      # http 區塊,Nginx 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 {                                # server 區塊,一個 server 表明一個虛擬主機
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {                        # location 區塊,屬於 server 區塊的內容
            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.conf 的核心配置參數作詳細解釋。html


Nginx 核心配置參數

首先去掉全部的註釋行和空行nginx

egrep -v "#|^$" /usr/local/nginx/nginx.conf.default

形式以下:vim

worker_processes  1; 							# worker 進程的數量,通常設置爲與CPU核心數一致,最好設置爲 auto,默認爲1
events {								# events(事件)區塊開始
    worker_connections  1024;					        # 每一個 worker 進程支持的最大鏈接數,默認值爲512
}									# events(事件)區塊結束
http { 									# http 區塊開始
    include       mime.types; 					        # Nginx 支持的媒體類型庫文件
    default_type  application/octet-stream; 	                        # 定義默認的媒體類型,默認值爲 text/plain
    sendfile        on;							# 開啓高效傳輸模式,默認值爲 off
    keepalive_timeout  65; 						# 設置客戶端的長鏈接在服務器端保持的最長時間(在此時間客戶端未發起新請求,則長鏈接關閉),默認值爲75s
    server {								# 第一個 server 區塊開始,表示一個獨立的虛擬主機站點
        listen       80;  						# 提供服務的端口,默認值爲 *:80
        server_name  localhost; 				        # 提供服務的域名
        location / {    						# 第一個 location 區塊開始
            root   html;						# 爲請求設置根目錄,默認爲 Nginx 安裝目錄下的 html 目錄(相對路徑),能夠使用絕對路徑
            index  index.html index.htm;                                # 默認的首頁文件,多個用空格分開,默認值爲 index.html
        }								# 第一個 location 區塊結束
        error_page   500 502 503 504  /50x.html;                        # 出現對應的 http 狀態碼時,使用50x.html迴應客戶
        location = /50x.html { 					        # location 區塊開始,訪問50x.html
            root   html;						# 指定對應的站點目錄爲html
        }
    }
} 									# http 區塊結束
相關文章
相關標籤/搜索