天天看點

nginx學習1.2 nginx主要配置詳解

環境:

centos6/7,nginx-1.9.15.

摘要說明:

上一篇我們介紹的nginx的基礎概念,安裝、目錄及啟停,連結;

本篇主要講述nginx的主要配置結構和各配置詳解;

步驟:

1.配置檔案整體結構:

首先我們基于nginx-1.9,15上一個預設nginx配置如下:

#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 {
    worker_connections  1024;
}


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

}
           

其結構如下:

nginx學習1.2 nginx主要配置詳解
  • main(全局設定)
  • events設定nginx的工作模式及連接配接數上限
  • http 伺服器相關屬性
  • server(虛拟主機設定)
  • upstream(上遊伺服器設定,主要為反向代理、負載均衡相關配置) l
  • location(URL比對特定位置後的設定)

2.詳細配置:

下面這些配置是全局性配置

  • user :主子產品指令, 指定Nginx的worker程序運作使用者以及使用者組,預設由nobody賬号運作。       
  • worker_processes: 指定Nginx要開啟的程序數,一般與服務cpu數目一緻。
  • error log:用來定義全局錯設日志檔案的路徑和日志名稱。日志輸出級别有debug,info,notice,warn,error,crit 可供選擇,其中debug輸出日志最為詳細,面crit(嚴重)輸出日志最少。預設是error
  • pid: 用來指定程序id的存儲檔案位置。
  • event:設定nginx的工作模式及連接配接數上限,其中參數use用來指定nginx的工作模式(這裡是epoll,epoll是多路複用IO(I/O Multiplexing)中的一種方式),nginx支援的工作模式有select ,poll,kqueue,epoll,rtsig,/dev/poll。其中select和poll都是标準的工作模式,kqueue和epoll是高效的工作模式,對于linux系統,epoll是首選。
  • worker_connection是設定nginx每個程序最大的連接配接數,預設是1024,是以nginx最大的連接配接數max_client=worker_processes * worker_connections。程序最大連接配接數受到系統最大打開檔案數的限制,需要設定ulimit。

下面這些是對對http伺服器相關屬性的配置詳解:

#下面部分是nginx對http伺服器相關屬性的設定
http {
    include       mime.types;               主子產品指令,對配置檔案所包含檔案的設定,減少主配置檔案的複雜度,相當于把部分設定放在别的地方,然後在包含進來,保持主配置檔案的簡潔
    default_type  application/octet-stream; 預設檔案類型,當檔案類型未定義時候就使用這類設定的。

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '   指定nginx日志的格式
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;
    sendfile        on;   開啟高效檔案傳輸模式(zero copy 方式),避免核心緩沖區資料和使用者緩沖區資料之間的拷貝。
    #tcp_nopush     on;  開啟TCP_NOPUSH套接字(sendfile開啟時有用)

    #keepalive_timeout  0;   用戶端連接配接逾時時間
    keepalive_timeout  65;

    #gzip  on;             設定是否開啟gzip子產品
#下面是server段虛拟主機的配置
server {
        listen       80;   虛拟主機的服務端口
        server_name  localhost;   用來指定ip或者域名,多個域名用逗号分開
        #charset koi8-r;
        location / {        
               #位址比對設定,支援正則比對,也支援條件比對,這裡是預設請求位址,使用者可以location指令對nginx進行動态和靜态網頁過濾處理
            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;
        }     
}
           

3.日志配置

通過通路日志,你可以得到使用者地域來源、跳轉來源、使用終端、某個URL通路量等相關資訊;通過錯誤日志,你可以得到系統某個服務或server的性能瓶頸等。是以,将日志好好利用,你可以得到很多有價值的資訊。 

日志預設為logs/access.log;

我們可以通過配置上述的nginx.conf來配置日志格式;即http下的log_format,這個是預設配置,可随意配置;

#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位址(代理伺服器,顯示代理服務ip)
$remote_user 用于記錄遠端用戶端的使用者名稱(一般為“-”)
$time_local 用于記錄通路時間和時區
$request 用于記錄請求的url以及請求方法
$status 響應狀态碼,例如:200成功、404頁面找不到等。
$body_bytes_sent 給用戶端發送的檔案主體内容位元組數
$http_user_agent 使用者所使用的代理(一般為浏覽器)
$http_x_forwarded_for 可以記錄用戶端IP,通過代理伺服器來記錄用戶端的ip位址
$http_referer 可以記錄使用者是從哪個連結通路過來的

4.日志定時切割

首先編寫切割腳本logcut.sh如下:

#!/bin/bash
#設定日志檔案存放目錄
LOG_HOME="/usr/local/nginx/logs/"
#備分檔案名稱
LOG_PATH_BAK="$(date -d yesterday +%Y%m%d%H%M)"
#重命名日志檔案
mv ${LOG_HOME}/access.log ${LOG_HOME}/access.${LOG_PATH_BAK}.log
mv ${LOG_HOME}/error.log ${LOG_HOME}/error.${LOG_PATH_BAK}.log
#向nginx主程序發信号重新打開日志
kill -USR1 `cat ${LOG_HOME}/nginx.pid`
           

添加執行權限:

chmod +x logcut.sh
           

接着我們可以手動執行下腳本:

./logcut.sh
           

接着使用crontab -e配置定時器配置每分鐘執行切割進行測試,更多可參考corn表達式:

*/1 * * * * /usr/local/nginx/sbin/logcut.sh
           

當然我們要确認定時任務是開啟的

/etc/rc.d/init.d/crond status
/etc/rc.d/init.d/crond start
           

定時任務的日志啟動和檢視:

/etc/init.d/rsyslog start
tail -f /var/log/cron
           

 上述都是基于centos6;下面centos7下直接使用指令:

systemctl start crond
systemctl stop crond
systemctl restart crond
systemctl reload crond
systemctl status crond.service
vi /etc/crond
crontab -l -u root #檢視root使用者任務
service rsyslog status
service rsyslog start
           

繼續閱讀