特性: ·模块化设计 ·nginx热部署(不需要中断正在处理的请求,更新配置) ·3xx-5xx错误重定向 ·重写(rewrite)模块 ·根据浏览器的类型返回哪个页面(比如手机、平板) ·支持验证Referer验证(防盗链) ·支持FLV流和MP4流(下载视频的过程中能一边一下载一边播放) 优点: ·支持kqueuq、epoll等IO模型(异步IO),由此来支持高并发 ·内存消耗小 应用场合: ·nginx结合FastCGI运行PHP.JSP,Perl等程序 ·nginx作反向代理(http、mail) ·nginx运行静态HTML页面 为什么nginx处理静态小文件能力强并发高?
答:使用epoll和kqueue I/O模型 静态业务:nginx 动态业务:apache nginx工作模式:一个master后台N个worker(1个主进程多个worker进程)
安装Nginx: 1.安装pcre库: ·pcre是什么:perl兼容正表达式
·为什么安装pcre:使nginx支持HTTP rewrite模块 1
2
3
4
| tar zxvf pcre-8.37.tar
cd pcre-8.37
./configure
make && make install
|
2.nginx参数 /usr/local/nginx/sbin/nginx -t 测试 /usr/local/nginx/sbin/nginx -s reload 平滑重启 /usr/local/nginx/sbin/nginx -V 查看怎么编译的 3.nginx路径 1
2
3
| ·配置文件路径:/uer/local/nginx/conf/nginx.conf
·启动文件路径:/uer/local/nginx/sbin/nginx
·网页文件路径:/uer/local/nginx/html
|
4.安装nginx
·关闭防火墙: service iptables stop ·关闭SElinux: setenforce 0 ·添加nginx账户: useradd nginx -s /sbin/nologin -M · tar zxvf nginx-1.8.0.tar.gz · cd nginx-1.8.0 · ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module · make && make install ·执行/usr/local/nginx/sbin/nginx -t 报错:error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory ·解决:1.find / -name libpcre.so* 找到libpcre.so*的路径 在/usr/local/lib 下 2.vim /etc/lb.so.conf 把/usr/local/lib添加进去 3.ldconfig重新加载,OK ·启动nginx:/usr/local/nginx/sbin/nginx
5.nginx主配置文件详解 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
| ·去掉注释: egrep -v "#|^$" nginx.conf
#user nobody; #使用哪个账户
worker_processes 1; #启动的worker线程数
#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; #启动一个worker线程为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 / { #uri,“/”表示默认路径/nginx/html下的页面
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; #50x错误时,跳转到50x.html
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ { #以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;
# }
#}
·zhaijunming.iyunv.com/5449012/1686898
URL:zhaijunming.iyunv.com
URI:/5449012/1686898
location [= | ~ | ~* | ^~ ] uri {}
location = uri {} 精确匹配
location ~ uri {} 区分大小写
location ~* uri {} 不区分大小写
location ^~ uri {} 不实使用正则表达式
|
6.把主配置文件和虚拟主机配置文件分离 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
| ·主配置文件(全局)nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
}
include extra/nginx_vhosts.conf; #定义虚拟主机配置文件,/usr/local/nginx/conf/extra/nginx_vhosts.conf
·创建虚拟主机文件:mkdir -p /usr/local/nginx/conf/extra
touch /usr/local/nginx/conf/extra/nginx_vhosts.conf
·虚拟主机配置文件
vim /usr/local/nginx/conf/extra/nginx_vhosts.conf
server {
listen 80;
server_name
location / {
root /data/www/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server{
listen 80;
server_name blog.aaa.com;
location / {
root /data/www/blog;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
|
7.定义日志 ·将这段加入到nginx.conf中的http {}中,表示为所有虚拟主机使用这种格式的日志 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 /usr/local/nginx/logs/access.log main; ·第一行main为定义的日志格式,最后一行main调用上面main的格式。 ·也可以将最后一行加入到nginx_vhots.conf中的server{}中,表示为某个虚拟主机使用此格式
|