1. 程式人生 > 其它 >1.8 核心配置檔案詳解(重點看)

1.8 核心配置檔案詳解(重點看)

nginx.conf核心配置檔案內容如下:


#user  nobody;
worker_processes  2;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    #預設使用epoll
    use epoll;
    #每個worker允許連線的客戶端最大連線數
    worker_connections  10240;
}


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

}

1.設定worker程序的使用者,指的linux的使用者,會涉及到nginx操作目錄或檔案的一些許可權,預設為nobody。

user root;

2.worker程序工作數設定,一般來說CPU有幾個,就設定幾個,或者設定N-1也行。

worker_processes 1;

3.nginx日誌級別debug | info | notice | warn | error | crit | alert | emerg,錯誤級別從左到右越來越大。

4.設定nginx程序pid

pid       logs/nginx.pid

5.設定工作模式

events {
    # 預設使用epoll
    use epoll;
    # 每個worker允許連線的客戶端最大連線數
    worker_connections  10240;
}

6.http是指令塊,針對http網路傳輸的一些指令配置

http {
}

7.include引入外部配置,提高可讀性,避免單個配置檔案過大。

include       mime.types;

8.設定日誌格式,main為定義的格式名稱,比如access_log就可以直接使用這個變量了。

#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;
引數名引數意義
$remote_addr客戶端ip
$remote_user遠端客戶端使用者名稱,一般為:"-"
$time_local時間和時區
$request請求的url以及method
$status響應狀態碼
$body_bytes_send響應客戶端內容位元組數
$http_referer記錄使用者從哪個連結跳轉過來的
$http_user_agent使用者所使用的代理,一般來說都是瀏覽器。
$http_x_forwarded_for通過代理伺服器來記錄客戶端的ip

9.sendfile使用使用高效檔案傳輸,提升傳輸效能。啟用後才能使用tcp_nopush,是指當資料表積累到一定大小之後才傳送,節約資源損耗。

sendfile        on;
tcp_nopush      on;

10.keepalive_timeout設定客戶端與服務端請求的超時時間,保證客戶端多次請求的時候不會重複建立新的連線,節約資源損耗。

#keepalive_timeout  0;
keepalive_timeout  65;

11.gzip啟用壓縮,html/js/css壓縮後傳輸會更快

#開啟gzip壓縮功能,目的:提高傳輸效率,節約頻寬。
gzip on;
#限制最小壓縮,小於1位元組檔案不會被壓縮
gzip_min_length 1;
#定義壓縮級別(壓縮比,檔案越大,壓縮越多,但是cpu使用也越多)
gzip_comp_level 3;
#定義壓縮檔案型別
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg  image/gif  image/png application/json;

12.server可以在http指令塊中設定多個虛擬主機

  • listen 監聽埠
  • server_name localhost、ip、域名
  • location 請求路由對映,匹配攔截
  • root 請求位置
  • index 首頁位置
    server {
            listen       80;
            server_name  localhost;
    
            location / {
                root   html;
                index  index.html index.htm;
            }
    }