全部的全部的全部的指令,都要以
;
结尾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