Loading

15、nginx编译安装


15.1、nginx介绍:

1、Nginx是一个开源,支持高性能,高并发的www服务和代理服务软件,由俄罗斯人开发,是开源的,nginx因具有高并发(特别是静态的

资源)、占用系统资源少等特性,且功能丰富逐渐流行起来;

2、Nginx不但是一个优秀的web服务软件,还可以具有反向代理(正向代理:内部-外部;反向代理:外部-内部)负载均衡功能和缓存服务功能,

在反向代理负载均衡功能方面,他类似于大名鼎鼎的lvs以及haproxy专业代理软件,但是nginx部署起来更为简单,方便;在缓存服务功能方面,

它类似于squid等专业缓存服务软件;

3、Nginx可以使用在window上,也可以使用在linux,unix上;

15.2、Nginx的重要特性:

可针对静态资源高速高并发访问及缓存;

可使用反向代理加速,并且可进行数据维护;

具有简单的负载均衡、节点健康检查和容错功能;

支持远程fastCGI服务的缓存加速;

支持SSL、TLS、SNI;

支持基于名字、端口及ip的多虚拟主机站点;

支持keep-alive和pipelined连接;

可进行简单、方便、灵活的配置和管理;

支持修改nginx配置,并且在代码上线是,可以平滑重启,不中断业务的访问;

可自定义访问日志格式,临时缓冲写日志操作,快速日志轮训及通过rsyslog处理日志;

可利用信号控制nginx进程;

支持3xx-5xx http状态码重定向;

支持rewrite模块,支持uri重写及正则表达式匹配;

支持客户端ip地址和http基本认证访问控制;

支持flv流和mp4流技术产品应用;

支持http相应速率限制;

支持同一ip地址的并发连接或请求数限制;

采用的是异步模型;

15.3、Ngix作为web服务器的主要应用场景:

使用nginx运行HTML、js、css、小图片等静态数据(此功能类似于apach;apach并发:4-6k;php并发:1000;db并发:几百;lighttpd软件);

nginx结合fastCGI运行php等动态程序(列如使用fastcgi_pass方式);

nginx结合Tomcat/resin等支持java动态程序(常用proxy_pass方式);

15.4、nginx、apach、lighttpd之间的区别:

1、apach:

市场份额一直在减少;

2、nginx:

3、lighttpd:

15.5、nginx、apach、lighttpd的select模型和epoll模型:

15.6、nginx、apach、lighttpd的使用场景:

15.7、nginx的编译安装:

https://opsx.alibaba.com/mirror

wget:选择yum的镜像地址(点击帮助)wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo;

http://nginx.org/ #nginx的官网;

1、安装pcre:

pcre中文简称“perl兼容正则表达式”,安装pcre库可以为nginx支持具备url重写功能的rewrite的功能;

[root@web01 ~]# yum install pcre pcre-devel -y

[root@web01 ~]# rpm -qa pcre pcre-devel

pcre-7.8-7.el6.x86_64

pcre-devel-7.8-7.el6.x86_64

2、安装openssl openssl-devle:

[root@web01 ~]# yum install openssl openssl-devel -y

[root@web01 ~]# rpm -qa openssl openssl-devel

openssl-1.0.1e-57.el6.x86_64

openssl-devel-1.0.1e-57.el6.x86_64

3、安装nginx:

[root@web01 ~]# mkdir tools -p

[root@web01 tools]# wget http://nginx.org/download/nginx-1.8.1.tar.gz #复制nginx的链接地址在linux中进行安装:

[root@web01 tools]# tar -xzvf nginx-1.8.1.tar.gz

[root@web01 ~]#mkdir -p /application/

[root@web01 nginx-1.8.1]# pwd

/root/tools/nginx-1.8.1

[root@web01 nginx-1.8.1]# useradd -Ms /sbin/nologin nginx

[root@web01 nginx-1.8.1]# ./configure --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --prefix=/application/nginx-1.8.1

#安装https模块,安装nginx状态模块,指定安装目录;

[root@web01 nginx-1.8.1]#make #编译

[root@web01 nginx-1.8.1] echo $? #编译是否完成;

[root@web01 nginx-1.8.1]#make install #安装

[root@web01 application]# pwd

/application

[root@web01 application]# ln -s nginx-1.8.1/ nginx #创建软链接,方便以后软件升级;

[root@web01 ~]# /application/nginx/sbin/nginx -t #检查配置文件语法是否正确;

nginx: the configuration file /application/nginx-1.8.1/conf/nginx.conf syntax is ok

nginx: configuration file /application/nginx-1.8.1/conf/nginx.conf test is successful

[root@web01 application]# /application/nginx/sbin/nginx #启动ngix服务

[root@web01 application]# netstat -tunlp | grep nginx #查看nginx服务;

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 11826/nginx

[root@web01 ~]# /application/nginx/sbin/nginx -V #查看nginx的版本号和编译时所用的参数;

nginx version: nginx/1.8.1

built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)

built with OpenSSL 1.0.1e-fips 11 Feb 2013

TLS SNI support enabled

configure arguments: --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --prefix=/application/nginx-1.8.1

4、访问网站:

5、nginx启动错误总结:

nginx:[emerg] getpwnam("nginx") failed :

这是因为没有对应的nginx服务用户所致,执行useradd -sM /sbin/nologin nginx 创建用户即可;

[root@web01 ~]# yum grouplist #必装

15.8、nginx配置参数详解:

[root@web01 nginx]# tree

.

├── client_body_temp

├── conf #nginx所有的配置文件目录,非常重要;

│ ├── fastcgi.conf

│ ├── fastcgi.conf.default

│ ├── fastcgi_params #与php交流时的fastcgi配置参数,一般用不到;

│ ├── fastcgi_params.default

│ ├── koi-utf

│ ├── koi-win

│ ├── mime.types #媒体类型配置文件

│ ├── mime.types.default

│ ├── nginx.conf #nginx配置文件

│ ├── nginx.conf.default #nginx配置文件的备份

│ ├── scgi_params

│ ├── scgi_params.default

│ ├── uwsgi_params

│ ├── uwsgi_params.default

│ └── win-utf

├── fastcgi_temp #fastcgi的临时目录;

├── html #html的存放目录;

│ ├── 50x.html #错误的html;

│ └── index.html #首页;

├── logs #日志目录

│ ├── access.log #访问日志;

│ ├── error.log #错误日志;

│ └── nginx.pid #nginx的进程号

├── proxy_temp

├── sbin #启动目录

│ └── nginx #启动文件

├── scgi_temp

└── uwsgi_temp

1、nginx配置文件:

[root@web01 conf]# pwd

/application/nginx/conf

2、清除配置文件中的空行和注释:

[root@web01 conf]# egrep -v "^$|#" nginx.conf.default >nginx.conf #去掉包含#号和空行的内容;

3、nginx配置文件详解:

[root@web01 conf]# cat nginx.conf

worker_processes 1; #worker进程数量,一般和cpu的核数相当;

events { #事件区(核心模块)块开始;

worker_connections 1024; #每个worker进程支持的最大并发连接数;

} #事件区块结束

http { #http区块开始

include mime.types; #nginx支持的媒体类型库文件包含(include表示包含的配置文件);

default_type application/octet-stream; #nginx支持的默认媒体库类型;

sendfile on; #开启高效传输模式;

keepalive_timeout 65; #连接超时;

server { #网站配置第一个server区块 开始,可以配置多个server;

listen 80; #网站监听的端口号,默认是80;

server_name www.web01.com; #网站域名;

location / { #“/”是nginx的安装目录, 即是 /application/nginx/ 目录;

root html; #存放html文件的目录,即是/application/nginx/html/ 目录;

index index.html index.htm; #是指当你属于网站的域名而不输入具体的文件名时所显示的html首页,如果没有首页会报403权限错误,需要改首页的名字和配置一致;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

} #网站配置第一个server区块结束;

}

[root@web01 nginx]# /application/nginx/sbin/nginx -t #修改配置文件后,检查语法是否存在错误;

nginx: the configuration file /application/nginx-1.8.1/conf/nginx.conf syntax is ok

nginx: configuration file /application/nginx-1.8.1/conf/nginx.conf test is successful

[root@web01 nginx]# /application/nginx/sbin/nginx -s reload#优雅的重启 ;

4、访问:

(1)在windows系统的hosts文件添加如下一行内容,方便解析:

172.16.1.8 www.web01.com

(2)在浏览器中进行访问:

15.9、nginx的模块配置:

nginx核心模块(http://nginx.org/en/docs/ngx_core_module.html)#events:

nginx核心功能模块负责nginx的全局应用,主要对应Main区块和Events区块区域;

nginx的http模块:

[root@web01 nginx-1.8.1]# ./configure --help #查看帮助,里面有所需要的http模块

1、虚拟主机介绍:

所谓虚拟主机,在web服务里就是一个独立的网站站点,这个站点对应独立的域名(也可能是ip或者是端口号),

具有独立的程序及资源目录,可以独立的对外提供服务供用户访问。这个独立的站点在配置里是有一定的格式标签段

标记,nginx使用一个server{}标签来标示一个虚拟主机,一个web服务里可以有多个虚拟主机标签对,即同时可以支持

多个虚拟主机站点。

2、基于域名的虚拟主机:

通过不同的域名区分不同的虚拟主机,基于域名的虚拟主机是企业应用最广的虚拟主机类型,几乎所有对外提供服

务的网站都是使用基于域名的虚拟主机;

(1)nginx.conf配置文件:

[root@web01 nginx]# cat conf/nginx.conf

worker_processes 1;

events {

worker_connections 1024;

}

http {

include mime.types;

default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

server {

listen 80;

server_name lc.web01.com;

location / {

root html/lc;

index lc.html;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

server {

listen 80;

server_name lc1.web01.com;

location / {

root html/lc1;

index lc1.html;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

}

[root@web01 html]# mkdir -p {lc1,lc} #创建虚拟目录;

[root@web01 lc]# echo "hello i am is lc" >lc.html

[root@web01 lc1]# echo "hello i am is lc1" >lc1.html

[root@web01 nginx]# sbin/nginx -t

nginx: the configuration file /application/nginx-1.8.1/conf/nginx.conf syntax is ok

nginx: configuration file /application/nginx-1.8.1/conf/nginx.conf test is successful

[root@web01 nginx]# sbin/nginx -s reload

[root@web01 conf]# netstat -tunlp | grep nginx

tcp 0 0.0.0.0:80 0.0.0.0:* LISTEN 1559/nginx

(2)访问:

在windows系统的hosts文件添加如下内容,方便解析:

#解决windos7 hosts文件权限不够的方法:附件——记事本——右键以管理员运行——打开hosts文件

172.16.1.8 lc.web01.com lc1.web01.com

3、基于端口的虚拟 主机:

通过不同的端口号来区分不同的虚拟主机,此类虚拟主机对应的企业应用主要是为公

司的内部网站,列如一些不希望直接对外提供用户访问的网站后台等,访问基于端口的

虚拟主机地址要带端口号;

(1)nginx.conf配置文件:

[root@web01 nginx]# cat conf/nginx.conf

worker_processes 1;

events {

worker_connections 1024;

}

http {

include mime.types;

default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

server {

listen 81;

server_name lc.web01.com;

location / {

root html/lc;

index lc.html;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

server {

listen 82;

server_name lc1.web01.com;

location / {

root html/lc1;

index lc1.html;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

}

[root@web01 conf]# /application/nginx/sbin/nginx -t

nginx: the configuration file /application/nginx-1.8.1/conf/nginx.conf syntax is ok

nginx: configuration file /application/nginx-1.8.1/conf/nginx.conf test is successful

[root@web01 conf]# /application/nginx/sbin/nginx -s reload

[root@web01 conf]# netstat -tunlp | grep nginx

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1559/nginx

tcp 0 0 0.0.0.0:81 0.0.0.0:* LISTEN 1559/nginx

tcp 0 0 0.0.0.0:82 0.0.0.0:* LISTEN 1559/nginx

(2)在windows系统的hosts文件添加如下内容,方便解析:

172.16.1.8 lc.web01.com lc1.web01.com

(也可以直接用ip加端口号进行访问):

4、基于IP的虚拟主机 (几乎不使用):

(1)添加临时ip地址:

[root@web01 conf]# ifconfig eth1:1 172.16.1.9/24

(2)nginx.conf配置文件:

[root@web01 nginx]# cat conf/nginx.conf

worker_processes 1;

events {

worker_connections 1024;

}

http {

include mime.types;

default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

server {

listen 172.16.1.8:80;

server_name lc.web01.com;

location / {

root html/lc;

index lc.html;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

server {

listen 172.16.1.9:80;

server_name lc1.web01.com;

location / {

root html/lc1;

index lc1.html;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

}

[root@web01 conf]# /application/nginx/sbin/nginx -t

nginx: the configuration file /application/nginx-1.8.1/conf/nginx.conf syntax is ok

nginx: configuration file /application/nginx-1.8.1/conf/nginx.conf test is successful

[root@web01 conf]# /application/nginx/sbin/nginx -s relaod

[root@web01 nginx]# netstat -tunlp | grep nginx

tcp 0 0 172.16.1.9:80 0.0.0.0:* LISTEN 2140/nginx

tcp 0 0 172.16.1.8:80 0.0.0.0:* LISTEN 2140/nginx

(3)访问:

在windows系统的hosts文件添加如下内容,方便解析:

172.16.1.8 lc.web01.com

172.16.1.9 lc1.web01.com

(也可以直接用ip直接进行访问):

5、小结:

(1)http访问虚拟主机的原理:

A、根据dns解析域名为ip地址,先建立tcp连接找到服务器的地址和端口号 ;列如:172.16.1.8:80;

B、读http 请求头 ,host内如果含域名,在服务器端找到对应的域名,返回对应域名的结果;

C、如果在浏览其中输入的是ip地址,则http请求头中host中对应的无域名,只会解析tcp第一个找到的服务并返回结果;

(2)、nginx出现403状态的问题:

没有首页文件(首页文件不匹配);

没有配置index标签;

首页文件权限不匹配;

(3)无主页报403错误的解决办法:

A、添加相应的主页,index.html

B、添加代码 :

server {

listen 80;

server_name lc1.web01.com;

autoindex on; #无主页时显示列表,可以提供http下载,适用于没有报错网页匹配的情况下;

#不过要注意权限,提供的目录是nginx安装目录下的html/lc1目录的内容,配置在server中,监听所的location;

location / {

root html/lc1;

index lc1as.html; #可以去除index标签;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}



a、测试:

(3)、一个ip可以对应多个域名,但是一个域名只对应一个ip地址;

6、ngix服务的优化:

(1)虚拟主机配置server服务配置单独放置:

[root@web01 conf]# pwd

/application/nginx/conf

(2)创建配置文件目录:

[root@web01 conf]# mkdir extra

(3)创建配置文件(本例使用的是基于ip的虚拟主机):

[root@web01 extra]# touch lc.web01.conf

server {

listen 172.16.1.8:80;

server_name lc.web01.com;

location / {

root html/lc;

index lc.html;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

[root@web01 extra]# touch lc1.web01.conf

server {

listen 172.16.1.9:80;

server_name lc1.web01.com;

location / {

root html/lc1;

index lc1.html;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

(4)修改nginx配置文件:

[root@web01 extra]# vim ../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/*.conf; #匹配所有配置文件,一般情况下不修改主配置文件;

}

(5)检查配置,平滑重启服务:

[root@web01 nginx]# sbin/nginx -t

nginx: the configuration file /application/nginx-1.8.1/conf/nginx.conf syntax is ok

nginx: configuration file /application/nginx-1.8.1/conf/nginx.conf test is successful

[root@web01 nginx]# sbin/nginx -s reload

[root@web01 nginx]# netstat -tunlp | grep nginx

tcp 0 0 172.16.1.9:80 0.0.0.0:* LISTEN 2140/nginx

tcp 0 0 172.16.1.8:80 0.0.0.0:* LISTEN 2140/nginx

(6)访问:

在windows系统的hosts文件添加如下内容,方便解析:

172.16.1.8 lc.web01.com

172.16.1.9 lc1.web01.com

(也可以直接用ip直接进行访问):

7、配置nginx虚拟主机别名:

(1)用途:

多数企业希望访问 www.web01.com和web01.com所浏览的是同一个页面,若有这类需求,就可以让web01.com

以别名的形式出现,这两个域名都要解析到服务器的ip地址;因为服务器在对外提供访问时,可能主域名对外提供服务,

端口号不同;可以在监控服务器里配置hosts(添加服务别名)来监控别名URL是否正常进而判断每一台服务器的服务是否正常;

(2)修改nginx server配置文件:

[root@web01 nginx]# vim conf/extra/web01.conf

server {

listen :80;

server_name www.web01.com web01.com;

location / {

root html;

index index.html index.htm;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

(3)访问:

在windows系统的hosts文件添加如下内容,方便解析:

172.16.1.8 web01.com www.web01.com

8、nginx状态:

nginx软件的功能模块中有一个ngx_http_stub_status_module模块,这个功能模块主要功能时记录nginx的基本访问状态信息,

让使用者了解nginx的工作状态,例如:连接数等信息;

(1)查看编辑时是否使用了该模块:

[root@web01 nginx]# sbin/nginx -V

nginx version: nginx/1.8.1

built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)

built with OpenSSL 1.0.1e-fips 11 Feb 2013

TLS SNI support enabled

configure arguments: --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --prefix=/application/nginx-1.8.1

(2)编辑配置文件:

[root@web01 nginx]# vim conf/extra/NginxStatus.conf

server {

listen :80;

server_name status.web01.com;

location / {

stub_status on; #开启状态描述;

access_log off; #访问日志,关;

#deny all;#设置禁止访问的ip

#allow 172.16.0.0/16 #设置允许访问的ip段 ,两者留一个就可,不设置默认允许所有;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

[root@web01 nginx]# sbin/nginx -t

nginx: the configuration file /application/nginx-1.8.1/conf/nginx.conf syntax is ok

nginx: configuration file /application/nginx-1.8.1/conf/nginx.conf test is successful

[root@web01 nginx]# sbin/nginx -s reload

(3)访问:

在windows系统的hosts文件添加如下内容,方便解析:

172.16.1.8 status.web01.com

访问参数详解:

Active connections: 1 #表示nginx正在处理的活动连接数,表示并发连接数(单位时间内,服务器正在处理的连接数)

server accepts handled requests

191 191 145 #server:从启动到现在一共处理的连接数;accepts:从启动到现在成功创建的握手数;

handled requests:已经处理完毕的连接数;

Reading: 0 Writing: 1 Waiting: 0 # Reading:nginx读取客户端的header信息数;Writing:nginx返回给客户端的header信息数;

Waiting:nginx已经处理完,正在等候下一次请求指令的驻留连接;

(4)注:nat网络编辑器的使用:

9、nginx 错误日志配置:

(1) 错误级别有 debug,info,notice,warm,error,crit,alert,emerg,级别越高记录的信息就越少,

生产场景一般使用warm,error、crit,不使用info,会导致磁盘io异常;

error_log的语法及参数说明为:

error_log file level;#关键字 日志文件 错误

nginx错误日志不配置时,默认是有的,在main区块中配置级别时error,监控所有错误, 错误记录位置是nginx安装目录下的logs/error.log文件:

worker_processes 1;

error_log logs/error.log error;

events {

worker_connections 1024;

}

(2)error_log可以放置的标签段为: main http server location,配置在相应的区块,监听相应的区块服务错误:

server {

listen :80;

server_name www.web01.com web01.com;

error_log logs/error.log error;

location / {

root html;

index index.html index.htm;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

10、nginx访问日志配置:

(1) nginx软件会把每个用户访问网站的日志信息记录到指定的日志文件中,供网站管理者分析用户浏览行为等;

位置:主配置:http; 附加配置:location,server;

nginx访问日志不配置时,默认是有的,在http区块中配置,监控所有server区块, 访问记录位置是nginx安装目录下的logs/access.log文件;

worker_processes 1;

events {

worker_connections 1024;

}

http {

include mime.types;

default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

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;

include extra/*.conf;

}

(2)注:配置参数解释:

172.16.1.254 - - [05/Nov/2018:20:12:05 +0800] "GET / HTTP/1.1" 200 62 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/

537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36" "-" #如果频繁出现404响应状态码,可以先清空浏览器的缓存;

$remote_addr:访问客户端ip地址;

$remote_user:访问的用户;

[$time_local]:访问时间;

$request:请求行信息;

$status:响应状态码;

$body_bytes_sent:服务器发送给用户的字节数;

$http_referer:请求的页面;

$http_user_agent:访问客户端电脑信息;

$http_x_forwarded_for:访问客户端浏览器信息;

(3)也可以配置在sever区块中达到监控一个web服务:

worker_processes 1;

events {

worker_connections 1024;

}

http {

include mime.types;

default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

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

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"';

include extra/*.conf;

}



server {

error_log logs/error.log error;

listen 172.16.1.8:80;

server_name www.web01.com web01.com;

access_log logs/host.access.log main;

location / {

root html;

index index.html index.htm;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

11、nginx日志轮询切割配置(方便每天搜集当天的日志);

(1)创建目录:

mkdir -p /server/root_scripts/

(2)创建脚本:

[root@web01 nginx]# vim /server/root_scripts/cut_nginx.sh

#!/bin/sh

export PATH=$PATH

cd /application/nginx/logs/

mv access.log access_$(date +%F -d -1day).log

#/application/nginx/sbin/nginx -s reload

find /application/nginx/logs/ -type f -mtime +7 | xargs rm -f

#如果有备份的话,需要做rsync备份处理;

[root@web01 nginx]# chmod u+x /server/root_scripts/cut_nginx.sh

(2)创建定时任务:

[root@web01 nginx]# crontab -e

00 00 * * * /bin/sh /server/root_scripts/cut_nginx.sh >/dev/null 2>$1

12、location的作用:

(1)location指令的作用可以根据用户请求的uri来执行不同的应用,其实就是根据用户请求的网站地址URL匹配,匹

配成功即进行相关的操作,可以理解为if else 条件语句;

(2)一个server中有多个location时的匹配规则从高到底:

location = / #1、精确的匹配,当无论放置的顺序如何都将优先被匹配并执行;

#www.web01.com 或www.web01.com/

location /document/ #2、路径匹配,即匹配了路径/documents/

#例:

location /lc/ {#目录

root lc; #目录

index index.html;

}

#www.web01.com/lc/ 或www.web01.com/lc/index.html;

location / #3、默认的匹配,如果没有匹配上其它的location,则匹配默认匹配的部分;

#www.web01.com/index.html 或任意不能匹配其它location的字符串;

结论:

13、nginx的rewirte用法

(1)nginx rewrite的主要功能时实现URL地址重写,nginx的rewirte规则需要pcre软件的支持,即通过pcrl

兼容正则表达式语法进行规则匹配的,默认参数编译的时候就会安装支持rewrite的模块,但是必须要pcre

软件的支持;

(2)指令语法:rewrite regex replacement[flag];

(3)应用位置:server、location;

例如 : rewrite ^/(.*) http://www.baidu.com.com/$1 permanenth;表示任何请求都跳转到百度:

rewrite是实现URL重写的关键指令;^/(.*)正则表达式,表示匹配所有的内容;匹配成功后

跳转到http://www.baidu.com/$1,这里的$1是指前面正则表达式()中的内容,是域名后面的内容;

perment表示永久301重定向标记, 即永远跳转到后面的http://www.baidu.com/$1地址上;

(4)flag标记:

last:本条规则匹配完成后,继续向下匹配新的URL规则;

break:本条规则匹配完成即终止,不在匹配任何后面的规则;

redirect:返回302临时重定向,浏览器地址栏会显示跳转后的URL地址;

permanent:返回301永久重定向,浏览器地址栏会显示跳转后的URL地址;

简单的正则表达式:

*:0或多个; +:一个或多个; .:任意单个字符; ?:0或一个; $:以什么结尾;

^:以什么开头;

(5)rewrite的企业应用场景:

可以调整用户浏览的URL,看起来更规范;

为了让搜索引擎收录网站内容和使用户的体验更好;

网站换新的域名后,让就得域名访问跳转到新的域名商 (不需要location也可以,直接放到server区块下面,

状态码会改变,会向新的域名网站发起请求;如果采用的是别名的方式,则看不到新的域名网址,但是返回的状态码是200,

只需一次请求,速度更快;

server {

listen :80;

server_name www.web01.com web01.com;

rewrite ^/(.*) http://www.baidu.com/$1 permanent;

}

14、根据特殊的变量、目录、进行URL跳转:

(1)nginx的口令认证(默认是关闭的状态);

位置:http,server,location;

(2)配置示例:

server {

listen :80;

server_name www.web01.com web01.com;

location / {

auth_basic "lc passwd"; # 提示

auth_basic_user_file /application/nginx/conf/htpasswd;#认证读取的账户和密码文件

root html;

index lc.html;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

(3)创建用户及密码:

[root@web01 nginx]# htpasswd -cb /application/nginx/conf/htpasswd lc 123456

Adding password for user lc

[root@web01 nginx]# cat /application/nginx/conf/htpasswd

lc:5/GWO2AQFjfFs

(4)平滑重启:

[root@web01 nginx]# sbin/nginx -t

nginx: the configuration file /application/nginx-1.8.1/conf/nginx.conf syntax is ok

nginx: configuration file /application/nginx-1.8.1/conf/nginx.conf test is successful

[root@web01 nginx]# sbin/nginx -s reload

(5)访问:

15、优化:

1、

vim /etc/security/limits.conf

//针对root用户

root soft nofile 65535

root hard nofile 65535

//所有用户, 全局

* soft nofile 25535

* hard nofile 25535


//对于Nginx进程

worker_rlimit_nofile 45535;


//root用户

//soft提醒

//hard限制

//nofile文件数配置项

//65535最大大小

2、隐藏版本号:

curl --head http://192.168.3.139

vi /usr/local/nginx0.8/conf/nginx.conf

worker_processes 1;

events {

worker_connections 1024;

}

http {

include mime.types;

default_type application/octet-stream;

server_tokens off;

expires 5s;

sendfile on;

keepalive_timeout 65;

include "sites-enabled/mail*";


3、Nginx通用优化配置文件:

[root@nginx ~]# cat nginx.conf

user nginx;

#worker_processes最多开启8个,8个以上性能提升不会再提升了,而且稳定性变得更低,所以8个进程够用了。

worker_processes auto;

#开启利用多核cpu的配置

worker_cpu_affinity auto;


error_log /var/log/nginx/error.log warn;

pid /run/nginx.pid;

#worker进程最大打开文件数,调整至1w以上,负荷较高建议2-3w以上

worker_rlimit_nofile 35535;


events {

# 使用epoll模型,linux的高性能方式

use epoll;

#限制每个进程能处理多少个连接请求,最大连接=连接数×进程数

worker_connections 10240;

}


http {

include /etc/nginx/mime.types;

default_type application/octet-stream;

# 统一使用utf-8字符集

charset utf-8;


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 /var/log/nginx/access.log main;


# Core module

sendfile on;

# 静态资源服务器建议打开

tcp_nopush on;

# 动态资源服务建议打开,需要打开keepalived

tcp_nodelay on;

keepalive_timeout 65;

#隐藏版本号

server_tokens off;


# Gzip module

gzip on;

gzip_disable "MSIE [1-6]\.";

gzip_http_version 1.1;


#Virtal Server

include /etc/nginx/conf.d/*.conf;

}


4、nginx压力测试:

[root@nginx ~]# yum install httpd-tools -y

[root@nginx-lua ~]# ab -n2000 -c2 http://127.0.0.1/index.html

//-n总的请求次数

//-c并发请求数

//-k是否开启长连接

...

Server Software: nginx/1.12.2

Server Hostname: 127.0.0.1

Server Port: 80


Document Path: /index.html

Document Length: 19 bytes


Concurrency Level: 200

# 总花费总时长

Time taken for tests: 1.013 seconds

# 总请求数

Complete requests: 2000

# 请求失败数

Failed requests: 0

Write errors: 0

Total transferred: 510000 bytes

HTML transferred: 38000 bytes

# 每秒多少请求/s(总请求出/总共完成的时间)

Requests per second: 9333.23 [#/sec] (mean)

# 客户端访问服务端, 单个请求所需花费的时间

Time per request: 101.315 [ms] (mean)

# 服务端处理请求的时间

Time per request: 0.507 [ms] (mean, across all concurrent requests)

# 判断网络传输速率, 观察网络是否存在瓶颈

Transfer rate: 491.58 [Kbytes/sec] received


5、影响nginx的性能指标有哪些:

(1)网络:

网络的流量

网络是否丢包

这些会影响http的请求与调用

(2)系统:

硬件有没有磁盘损坏,磁盘速率

系统负载、内存、系统稳定性

(3)服务:

连接优化、请求优化

根据业务形态做对应的服务设置

(4)程序:

接口性能

处理速度

程序执行效率

(5)数据库

每个架构服务与服务之间都或多或少有一些关联, 我们需要将整个架构进行分层, 找到对应系统或服务的短板, 然后进行优化


6、补充:nginx文件句柄:

(1)文件句柄:

Linux一切皆文件,文件句柄可以理解为就是一个索引,文件句柄会随着我们进程的调用频繁增加,系统默认对文件句柄有限

制,不能让一个进程无限的调用,需要限制每个进程和每个服务使用多大的文件句柄,文件句柄是必须要调整的优化参数;

(2)设置方式

系统全局性修改

用户局部性修改

进程局部性修改






















posted @ 2020-02-15 22:53  云起时。  阅读(113)  评论(0编辑  收藏  举报