nginx配置文件詳解

nginx配置文件:html

user nginx;nginx

worker_processes 1; ##啓動進程的個數(進程個數並非併發越多越好。通常來講,進程個數與CPU個數相等。若是進程個數多於CPU個數,要進行上下文切換,會耗時。)正則表達式

events {express

  worker_connections 1024;瀏覽器

} ##一個進程併發請求數緩存


nginx不只能夠作網頁服務器,還能夠作nginx反向代理和mail代理。服務器

在以下行進行配置:併發


http {app

   include mime.types;ide

   default_type application/cetet-stream;

   #log_format main  '$remote_addr - $remote_user [$time_local] "$request"' 

   #                 '$status $body_bytes_sent "$http_referer"'

   #                 '"$http_user_agent" "$http_x_forwarded_for"';  

   #remote_addr 客戶端ip remote_user遠程用戶 time_local本地時間 request用戶請求資源 status狀態碼 body_bytes_sent發送的字節數 http_referer訪問網頁的來源連接 http_user_agent瀏覽器類型 http_x_forwarded_for緩存服務器軟件

  access_log /var/log/nginx/access.log main;日誌的存放路徑,使用main格式

  sendfile on;

  keepalive_timeout 65;使用保持鏈接

  #gzip on;網頁文件壓縮之後再發送


server {

    listen       80 default_server;

    server_name  localhost;主機名稱


    #charset koi8-r;


    #access_log  logs/host.access.log  main;


    location / { 是http://172.16.100.1/的根目錄

        root   /usr/share/nginx/html;相對於網頁文件存放位置的相對路徑,最好使用絕對路徑

        index  index.html index.htm;網頁主頁面

    }


    error_page  404              /404.html;

    location = /404.html {

        root   /usr/share/nginx/html;

    }


listen配置的格式:(只能在server中進行設置)

listen address:port[ default [ backlog=num | rcvbuf=size接收緩存大小 | sndbuf=size 發送緩存大小 | accept_filter=filter | deferred | bind | ssl 只能使用443端口]]

listen 127.0.0.1:8000;

listen 127.0.0.1;

listen 8000;

listen *:8000;

listen localhost:8000;


location配置的格式:(只能在server中進行設置)

location [=|~|~*|^~|@] /url/ {...}

= 精確匹配,所請求的必須是特定的文件

~ ~* 正則表達式,url是正則表達式patter ~區分字母大小寫 ~*不區分字母大小寫

^~ 不使用正則表達式,只作逐個字符匹配

@ 轉向其餘代理服務器

優先級:= ^~ (~ ~*)


例子:

= 只能匹配/

location = / {

  #matches the query / only.

  [ configuration A ]

}


/ 能夠匹配所包含的目錄

location / {

  #matches any query, since all queries begins with /, but regular expresions and any longer conventional blocks will be matched first.

  [ configuration B ]

}


^~ 匹配之後面URL定義起始的目錄

location ^~ /p_w_picpaths/ {

  #matches any query beginning with /p_w_picpaths/ and halts searching, so regular expressions will not be checked.

  [ configuration C ]

}


~* 匹配正則表達式

location ~* \.(gif|jpg|jpeg)$ {

  #matches any request ending in gif, jpg, or jpeg. However, all requests to the /p_w_picpaths/ directory will be handled by Configuration C.

  [ configuration D ]

}


Example requests:

 * / --> configuration A

 * /documents/document.html --> configuration B

 * /p_w_picpaths1.gif --> configuration C

 * /documents/1.jpg --> configuration D


alias 設定別名,在location中定義

語法:alias file-path|directory-path

location / {

  root /spool/w3;

}

location /bbs/ {

  alias /spool/bbs/;

}


index 設置主頁面


autoindex 若是訪問時候沒有主頁面,就列出該目錄下的全部頁面

location / {

  autoindex on;

}


AccessModule 訪問控制列表

location / {

  deny 192.168.1.1;

  allow 192.168.1.0/24;

  allow 10.1.0.0/16;

  deny all;

}


開啓nginx狀態監控的功能;

location /nginx status {

  stub_status on;

  access_log off;關閉訪問日誌

}

Active connections:2 處於活動狀態的鏈接

server accepts handled requests

 125202 125202 125254 

(已經接入的鏈接數 已經處理過的鏈接數 正在處理的鏈接數 )

Reading:1 Writing:1 Waiting:0

(讀取請求頭 處理過請求,並將結果發送給客戶端 尚處於保活狀態的鏈接數)


啓動基於用戶的認證:

server {

  server_name www.magedu.com;

  ...

  location / {

    auth_basic "Restricted";

    auth_basic_user_file /etc/nginx/.htpasswd;

    ...

  }

}

#htpasswd -cm /etc/nginx/.htpasswd jerry

相關文章
相關標籤/搜索