全部的全部的全部的指令,都要以
;
結尾html
user nobody nobody;node
worker_processes auto;
worker_processes 4;linux
這個數字,跟電腦CPU核數要保持一致nginx
ganiks ➜ Nginx git:(master) ✗ grep ^proces /proc/cpuinfo processor : 0 processor : 1 processor : 2 processor : 3 ganiks ➜ Nginx git:(master) ✗ grep ^proces /proc/cpuinfo | wc -l 4
pid logs/nginx.pid;git
這裏面保存的就是一個數字,nginx master 進程的進程號web
error_log logs/error.log;
error_log logs/error.log error;正則表達式
include mime.types;
include fastcgi_params;
include ../../conf/*.conf;apache
accept_mutex on;瀏覽器
對多個nginx進程接收鏈接進行序列化,防止多個進程對鏈接的爭搶(驚羣)緩存
multi_accept off;
use select|poll|kqueue|epoll|rtsig|/dev/poll|eventport
這個重點,後面再看
worker_connections 512;
include mime.types;
default_type application/octet-stream;
access_log logs/access.log main;
access_log off;
sendfile off;
sendfile on;
sendfile_max_chunk 128k;
nginx 每一個worker process 每次調用 sendfile()傳輸的數據量的最大值
Refer:
與用戶創建鏈接後,Nginx能夠保持這些鏈接一段時間, 默認 75s
下面的65s能夠被Mozilla/Konqueror識別,是發給用戶端的頭部信息Keep-Alive
值
keepalive_timeout 75s 65s;
和用戶端創建鏈接後,用戶經過此鏈接發送請求;這條指令用於設置請求的上限數
keepalive_requests 100;
listen *:80 | *:8000; # 監聽全部的80和8000端口
listen 192.168.1.10:8000;
listen 192.168.1.10;
listen 8000; # 等同於 listen *:8000;
listen 192.168.1.10 default_server backlog=511; # 該ip的鏈接請求默認由此虛擬主機處理;最多容許1024個網絡鏈接同時處於掛起狀態
server_name myserver.com www.myserver.com;
server_name .myserver.com www.myserver. myserver2.*; # 使用通配符
不容許的狀況: server_name www.ab*d.com; #
*
只容許出如今www和com的位置
server_name ~^www\d+.myserver.com$; # 使用正則
nginx的配置中,能夠用正則的地方,都以
~
開頭
from Nginx~0.7.40 開始,server_name 中的正則支持 字符串捕獲功能(capture)
server_name ~^www.(.+).com$; # 當請求經過www.myserver.com請求時, myserver就被記錄到$1
中, 在本server的上下文中就可使用
若是一個名稱 被多個虛擬主機的 server_name 匹配成功, 那這個請求到底交給誰處理呢?看優先級:
基於IP的虛擬主機,須要將網卡設置爲同時可以監聽多個IP地址
ifconfig # 查看到本機IP地址爲 192.168.1.30 ifconfig eth1:0 192.168.1.31 netmask 255.255.255.0 up ifconfig eth1:1 192.168.1.32 netmask 255.255.255.0 up ifconfig # 這時就看到eth1增長來2個別名, eth1:0 eth1:1 # 若是須要機器重啓後仍保持這兩個虛擬的IP echo "ifconfig eth1:0 192.168.1.31 netmask 255.255.255.0 up" >> /etc/rc.local echo "ifconfig eth1:0 192.168.1.32 netmask 255.255.255.0 up" >> /etc/rc.local
再來配置基於IP的虛擬主機
http { ... server { listen 80; server_name 192.168.1.31; ... } server { listen 80; server_name 192.168.1.32; ... } }
location 塊的配置,應該是最經常使用的了
location [ = | ~ | ~* | ^~ ] uri {...}
這裏內容分2塊,匹配方式和uri, 其中uri又分爲 標準uri和正則uri
先不考慮 那4種匹配方式
標準uri
和請求字符串匹配, 若是有,記錄匹配度最高的一個;正則uri
和請求字符串匹配, 當第一個正則uri
匹配成功,即中止搜索, 並使用該location塊處理請求;正則uri
都匹配失敗,就使用剛記錄下的匹配度最高的一個標準uri
處理請求再看4種匹配方式:
=
: 用於標準uri
前,要求請求字符串與其嚴格匹配,成功則當即處理^~
: 用於標準uri
前,並要求一旦匹配到,當即處理,再也不去匹配其餘的那些個正則uri
~
: 用於正則uri
前,表示uri包含正則表達式, 並區分大小寫~*
: 用於正則uri
前, 表示uri包含正則表達式, 不區分大小寫
^~
也是支持瀏覽器編碼過的URI的匹配的哦, 如/html/%20/data
能夠成功匹配/html/ /data
Web服務器收到請求後,首先要在服務端指定的目錄中尋找請求資源
root /var/www;
除了使用root指明處理請求的根目錄,還可使用alias 改變location收到的URI的請求路徑
location ~ ^/data/(.+\.(htm|html))$ { alias /locatinotest1/other/$1; }
index 指令主要有2個做用:
location ~ ^/data/(.+)/web/$ { index index.$1.html index.htm; }
error_page 404 /404.html;
error_page 403 /forbidden.html;
error_page 404 =301 /404.html;
location /404.html { root /myserver/errorpages/; }
location / { deny 192.168.1.1; allow 192.168.1.0/24; allow 192.168.1.2/24; deny all; }
從192.168.1.0的用戶時能夠訪問的,由於解析到allow那一行以後就中止解析了
auth_basic "please login";
auth_basic_user_file /etc/nginx/conf/pass_file;
這裏的file 必須使用絕對路徑,使用相對路徑無效
# /usr/local/apache2/bin/htpasswd -c -d pass_file user_name # 回車輸入密碼,-c 表示生成文件,-d 是以 crypt 加密。 name1:password1 name2:password2:comment
通過basic auth認證以後沒有過時時間,直到該頁面關閉;
若是須要更多的控制,可使用 HttpAuthDigestModule http://wiki.nginx.org/HttpAuthDigestModule
user ganiks ganiks; worker_processes 3; error_log logs/error.log; pid myweb/nginx.pid; events { use epoll; worker_connections 1024; } http { include mime.types; default_type applicatioin/octet-stream; sendfile on; keepalive_timeout 65; log_format access.log '$remote_addr [$time_local] "$request" "$http_user_agent"'; server { listen 8081; server_name myServer1; access_log myweb/server1/log/access.log; error_page 404 /404.html; location /server1/location1 { root myweb; index index.svr1-loc1.htm; } location /server1/location2 { root myweb; index index.svr1-loc2.htm; } } server { listen 8082; server_name 192.168.0.254; auth_basic "please Login:"; auth_basic_user_file /home/ganiks/learn/nginx/Nginx/myweb/user_passwd; access_log myweb/server2/log/access.log; error_page 404 /404.html; location /server2/location1 { root myweb; index index.svr2-loc1.htm; } location /svr2/loc2 { alias myweb/server2/location2/; index index.svr2-loc2.htm; } location = /404.html { root myweb/; index 404.html; } } }
ganiks ➜ Nginx git:(master) ✗ ./sbin/nginx -c conf/nginx02.conf nginx: [warn] the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /home/ganiks/learn/nginx/Nginx/conf/nginx02.conf:1 ganiks ➜ myweb git:(master) ✗ tree . . ├── 404.html ├── server1 │ ├── location1 │ │ └── index.svr1-loc1.htm │ ├── location2 │ │ └── index.svr1-loc2.htm │ └── log │ └── access.log └── server2 ├── location1 │ └── index.svr2-loc1.htm ├── location2 │ └── index.svr2-loc2.htm └── log └── access.log 8 directories, 7 files
http://myserver1:8081/server1/location1/ this is server1/location1/index.svr1-loc1.htm http://myserver1:8081/server1/location2/ this is server1/location1/index.svr1-loc2.htm
http://192.168.0.254:8082/server2/location1/ this is server2/location1/index.svr2-loc1.htm http://192.168.0.254:8082/svr2/loc2/ this is server2/location1/index.svr2-loc2.htm http://192.168.0.254:8082/server2/location2/ 404 404 404 404