1.nginx是什么
Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,Nginx,它的发音为“engine X”,是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP 代理服务器。Nginx是由俄罗斯人 Igor Sysoev为俄罗斯访问量第二的 Rambler.ru站点开发的,它已经在该站点运行超过两年半了。Igor Sysoev在建立的项目时,使用基于BSD许可。
英文主页:http://nginx.net
到2013年,目前有很多国内网站采用Nginx作为Web服务器,如国内知名的新浪、163、腾讯、Discuz、豆瓣等。据netcraft统计,Nginx排名第3,约占15%的份额(参见:http://news.netcraft.com/archives/category/web-server-survey/ )
Nginx以事件驱动的方式编写,所以有非常好的性能,同时也是一个非常高效的反向代理、负载平衡。其拥有匹配Lighttpd的性能,同时还没有Lighttpd的内存泄漏问题,而且Lighttpd的mod_proxy也有一些问题并且很久没有更新。
现在,Igor将源代码以类BSD许可证的形式发布。Nginx因为它的稳定性、丰富的模块库、灵活的配置和低系统资源的消耗而闻名.业界一致认为它是Apache2.2+mod_proxy_balancer的轻量级代替者,不仅是因为响应静态页面的速度非常快,而且它的模块数量达到Apache的近2/3。对proxy和rewrite模块的支持很彻底,还支持mod_fcgi、ssl、vhosts ,适合用来做mongrel clusters的前端HTTP响应
现在,Igor将源代码以类BSD许可证的形式发布。Nginx因为它的稳定性、丰富的模块库、灵活的配置和低系统资源的消耗而闻名.业界一致认为它是Apache2.2+mod_proxy_balancer的轻量级代替者,不仅是因为响应静态页面的速度非常快,而且它的模块数量达到Apache的近2/3。对proxy和rewrite模块的支持很彻底,还支持mod_fcgi、ssl、vhosts ,适合用来做mongrel clusters的前端HTTP响应。
nginx做为HTTP服务器,有以下几项基本特性:
处理静态文件,索引文件以及自动索引;打开文件描述符缓冲.
无缓存的反向代理加速,简单的负载均衡和容错.
FastCGI,简单的负载均衡和容错.
模块化的结构。包括gzipping, byte ranges, chunked responses,以及 SSI-filter等filter。如果由FastCGI或其它代理服务器处理单页中存在的多个SSI,则这项处理可以并行运行,而不需要相互等待。
支持SSL 和 TLSSNI.
Nginx专为性能优化而开发,性能是其最重要的考量,实现上非常注重效率 。它支持内核Poll模型,能经受高负载的考验,有报告表明能支持高达 50,000个并发连接数。
Nginx具有很高的稳定性。其它HTTP服务器,当遇到访问的峰值,或者有人恶意发起慢速连接时,也很可能会导致服务器物理内存耗尽频繁交换,失去响应,只能重启服务器。例如当前apache一旦上到200个以上进程,web响应速度就明显非常缓慢了。而Nginx采取了分阶段资源分配技术,使得它的CPU与内存占用率非常低。nginx官方表示保持10,000个没有活动的连接,它只占2.5M内存,所以类似DOS这样的攻击对nginx来说基本上是毫无用处的。就稳定性而言,nginx比lighthttpd更胜一筹。
Nginx支持热部署。它的启动特别容易, 并且几乎可以做到7*24不间断运行,即使运行数个月也不需要重新启动。你还能够在不间断服务的情况下,对软件版本进行进行升级。
Nginx采用master-slave模型,能够充分利用SMP的优势,且能够减少工作进程在磁盘I/O的阻塞延迟。当采用select()/poll()调用时,还可以限制每个进程的连接数。
Nginx代码质量非常高,代码很规范,手法成熟, 模块扩展也很容易。特别值得一提的是强大的Upstream与Filter链。Upstream为诸如reverse proxy,与其他服务器通信模块的编写奠定了很好的基础。而Filter链最酷的部分就是各个filter不必等待前一个filter执行完毕。它可以把前一个filter的输出做为当前filter的输入,这有点像Unix的管线。这意味着,一个模块可以开始压缩从后端服务器发送过来的请求,且可以在模块接收完后端服务器的整个请求之前把压缩流转向客户端。
Nginx采用了一些os提供的最新特性如对sendfile (Linux2.2+),accept-filter (FreeBSD4.1+),TCP_DEFER_ACCEPT (Linux 2.4+)的支持,从而大大提高了性能。
当然,nginx还很年轻,多多少少存在一些问题,比如:Nginx是俄罗斯人创建,虽然前几年文档比较少,但是目前文档方面比较全面,英文资料居多,中文的资料也比较多,而且有专门的书籍和资料可供查找。
nginx的作者和社区都在不断的努力完善,我们有理由相信nginx将继续以高速的增长率来分享轻量级HTTP服务器市场,会有一个更美好的未来
2.nginx功能
反向代理和正向代理
正向代理
简单的说,我是一个用户,我无法直接访问一个网站,但是我能访问一个代理服务器,这个代理服务器能访问那个我不能访问的网站,于是我先连上代理服务器,告诉它我需要那个无法访问网站的内容,代理服务器去取回来,然后返回给我。从网站的角度,只在代理服务器来取内容的时候有一次记录。
结论就是,正向代理,是一个位于客户端和原始服务器(origin server)之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并指定目标(原始服务器),然后代理向原始服务器转交请求并将获得的内容返回给客户端。客户端必须要进行一些特别的设置才能使用正向代理
反向代理
例如我要访问 localhost:8080/views/test1 这个页面,但view对应的服务器上并没有test1这个资源,它是从另一台服务器上调用的资源。这样view对应的那个服务器就使用了反向代理。即用户只需要把请求发给特定的反向代理服务器,具体请求是谁处理的用户不需要知道(其实也不知道),由代理服务器统一处理。
结论就是反向代理正好相反,对于客户端而言它就像是原始服务器,并且客户端不需要进行任何特别的设置。客户端向反向代理的命名空间(name-space)中的内容发送普通请求,接着反向代理将判断向何处(原始服务器)转交请求,并将获得的内容返回给客户端,就像这些内容 原本就是它自己的一样。正向代理的典型用途是为在防火墙内的局域网客户端提供访问Internet的途径。
正向代理还可以使用缓冲特性减少网络使用率。反向代理的典型用途是将 防火墙后面的服务器提供给Internet用户访问。反向代理还可以为后端的多台服务器提供负载平衡,或为后端较慢的服务器提供缓冲服务。
总结
正向代理:针对客户端而言, 代理服务器代理客户端,转发请求,并将获得的内容返回给客户端。
反向代理:针对客户端而言, 代理服务器就像是原始服务器,代理集群的web节点服务器返回结果
负载均衡
负载均衡也是Nginx常用的一个功能,负载均衡其意思就是分摊到多个操作单元上进行执行,例如Web服务器、FTP服务器、企业关键应用服务器和其它关键任务服务器等,从而共同完成工作任务。
简单而言就是当有2台或以上服务器时,根据规则随机的将请求分发到指定的服务器上处理,负载均衡配置一般都需要同时配置反向代理,通过反向代理跳转到负载均衡。而Nginx目前支持自带3种负载均衡策略,还有2种常用的第三方策略
RR
按照轮询(默认)方式进行负载,每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。虽然这种方式简便、成本低廉。但缺点是:可靠性低和负载分配不均衡
权重
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
upstream test{ server localhost:8080 weight=9; server localhost:8081 weight=1; }
此时8080和8081分别占90%和10%。
ip_hash
上面的2种方式都有一个问题,那就是下一个请求来的时候请求可能分发到另外一个服务器,当我们的程序不是无状态的时候(采用了session保存数据),这时候就有一个很大的很问题了,比如把登录信息保存到了session中,那么跳转到另外一台服务器的时候就需要重新登录了,所以很多时候我们需要一个客户只访问一个服务器,那么就需要用iphash了,iphash的每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题
upstream test { ip_hash; erver localhost:8080; server localhost:8081; }
fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配
upstream backend { fair; server localhost:8080; server localhost:8081; }
url_hash(第三方)
按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法。
upstream backend { hash $request_uri; hash_method crc32; server localhost:8080; server localhost:8081; }
3.nginx环境搭建
版本选择
最新稳定版本(stable),上一个最新稳定版, 最新的lts版本(长期维护版本)
环境准备
[root@web01 ~]$ hostname -I
10.0.0.45 172.16.1.45
[root@web02 ~]$ hostname -I
10.0.0.145 172.16.1.145
[root@web03 ~]$ hostname -I
10.0.0.245 172.16.1.245
安装nginx
第一种yum
[root@web01 ~]$yum install nginx -y
第二种配置源(在官网(https://nginx.org/en/linux_packages.html)上复制源)
[root@web02 ~]$ cat /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
启动nginx
[root@web01 ~]$ systemctl start nginx
[root@web01 ~]$ systemctl enable nginx
检查端口进程
[root@web01 ~]$ ss -lntup | grep nginx
tcp LISTEN 0 128 *:80 *:* users:((“nginx”,pid=5906,fd=6),(“nginx”,pid=2233,fd=6))
tcp LISTEN 0 128 [::]:80 [::]:* users:((“nginx”,pid=5906,fd=7),(“nginx”,pid=2233,fd=7))
[root@web01 ~]$ ps -ef | grep nginx
root 2233 1 0 Jul27 ? 00:00:00 nginx: master process /usr/sbin/nginx
nginx 5906 2233 0 20:19 ? 00:00:00 nginx: worker process
root 5914 4610 0 20:19 pts/0 00:00:00 grep –color=auto nginx
检查web是否可以访问
[root@web01 ~]$ curl 10.0.0.145
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href=”http://nginx.org/”>nginx.org</a>.<br/>
Commercial support is available at
<a href=”http://nginx.com/”>nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
测试web页面
上传一个压缩包,解压到web显示目录
[root@web01 ~]$ cd /usr/share/nginx/html/
[root@web01 /usr/share/nginx/html]$
[root@web01 /usr/share/nginx/html]$ mkdir yx
[root@web01 /usr/share/nginx/html/yx]$ unzip 80款H5小游戏源码大合集_带网页导航.zip
访问web页面(ip后面上需要加上yx的目录,因为index.html在这个yx目录里,正常如果.html的文件放在html目录下是不要需要加上yx目录)
在web上显示正常
4.nginx配置文件
(yum安装和源安装时不太一样的)
/etc/nginx/nginx.conf
[root@web01 ~]$ cat /etc/nginx/nginx.conf
user nginx; ##nginx运行用户(默认)
worker_processes auto; ##worker进程数量(处理用户请求)
error_log /var/log/nginx/error.log notice; ##error错误日志 notice错误日志级别 或 debug
pid /var/run/nginx.pid; ##nginx pid文件网址
events { ##events模块/区域
worker_connections 1024; ##每个worker进程的连接数(并发)
}
http { ##http区域/模块
include /etc/nginx/mime.types; ##媒体类型(文件类型)include引用或包含其它地方配置,精简nginx主配置文件
default_type application/octet-stream;
log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘ ##指定nginx访问日志的格式
‘$status $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” “$http_x_forwarded_for”‘; ##log format 格式名称 格式详细定义
access_log /var/log/nginx/access.log main; ##access.log指定日志的位置及格式
sendfile on; ##开启高效传输模式
#tcp_nopush on;
keepalive_timeout 65; ##keepalive超时时间
#gzip on; ##是否开启gzip压缩 静态文本内容 节省带宽
include /etc/nginx/conf.d/*.conf; ##引用的配置文件路径
}
/etc/nginx/conf.d/default.conf
[root@web02 ~]$ cat /etc/nginx/conf.d/default.conf
server { ##server区域
listen 80; ##监听的端口
server_name localhost; ##监听的服务名称(域名)
#access_log /var/log/nginx/host.access.log main; ##网站访问日志路径
location / { ##location用来匹配uri
root /usr/share/nginx/html; ##访问的站点目录
index index.html index.htm; ##指定网站的首页文件
}
#error_page 404 /404.html; ##404错误返回的页面
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; ##500 502 503 504返回的页面
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ { ##nginx代理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 { ##以ht结尾的全部拒绝访问
# deny all;
#}
}
http{}server{}location{}关联
http{}
处理用户http请求
server{}
http请求中,站点
location{}
server中匹配请求中的uri
http{}层下允许有多个Server{},一个Server{}下又有多个location{}
5.自定义nginx的web站点
书写nginx配置文件,定义端口,定义域名,定义返回的路径,以及默认页面
[root@web01 ~]$ cat /etc/nginx/conf.d/dmxsp.qiang.com.conf
server {
listen 80;
server_name dmxsp.qiang.com;
location / {
root /code/dmxsp;
index index.html index.php;
}
}
检查语法
[root@web01 ~]$ nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
创建文件,目录,在文件里写上东西
[root@web01 ~]$ mkdir -p /code/dmxsp
[root@web01 ~]$ cat /code/dmxsp/index.html
qiang
重启nginx
[root@web01 ~]$ systemctl reload nginx.service
配置host劫持,通过域名访问网站
windows的hosts文件路径C:\Windows\System32\drivers\etc\hosts
(最好是用Notepad++)
linux的hosts文件路径/etc/hosts
页面测试
6.nginx虚拟主机
虚拟主机,nginx配置中的多个server{}区域 ,每个server{}对应不同的业务(站点)
虚拟主机方式
基于域名(host)的虚拟主机
不同的域名访问不同的,站点
基于IP的虚拟主机
不同的ip地址可以访问不同的站点
基于端口的虚拟主机
不同的端口可以访问不同的站点,用于隐藏重要路径或业务
基于域名虚拟主机
修改配置
/code/dmxsp index.html 内容 qiang
/code/qiang index.html 内容 dmxsp
修改配置文件
[root@web01 ~]$ cat /etc/nginx/conf.d/dmxsp.qiang.com.conf
server {
listen 80;
server_name dmxsp.qiang.com;
location / {
root /code/dmxsp;
index index.html index.php;
}
}
[root@web01 ~]$ cat /etc/nginx/conf.d/qiang.dmxsp.com.conf
server {
listen 80;
server_name qiang.dmxsp.com;
location / {
root /code/qiang;
index index.html index.php;
}
}
创建目录
[root@web01 ~]$ mkdir -p /code/qiang
[root@web01 ~]$ cat /code/qiang/index.html
dmxsp
检查
[root@web01 ~]$ nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
重启nginx
[root@web01 ~]$ systemctl reload nginx.service
测试
基于ip虚拟主机
添加一个临时IP
[root@web01 ~]$ ip addr add 10.0.0.100/24 dev eth0 label eth0:0
修改配置文件
[root@web01 ~]$ cat /etc/nginx/conf.d/dmxsp.qiang.com.conf
server {
listen 10.0.0.45:80;
# server_name dmxsp.qiang.com;
location / {
root /code/dmxsp;
index index.html index.php;
}
}
[root@web01 ~]$ cat /etc/nginx/conf.d/qiang.dmxsp.com.conf
server {
listen 10.0.0.100:80;
# server_name qiang.dmxsp.com;
location / {
root /code/qiang;
index index.html index.php;
}
}
检查
[root@web01 ~]$ nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
重启nginx
[root@web01 ~]$ systemctl reload nginx.service
测试
基于端口虚拟主机
修改配置文件
[root@web01 ~]$ cat /etc/nginx/conf.d/dmxsp.qiang.com.conf
server {
listen 85;
server_name dmxsp.qiang.com;
location / {
root /code/dmxsp;
index index.html index.php;
}
}
[root@web01 ~]$ cat /etc/nginx/conf.d/qiang.dmxsp.com.conf
server {
listen 8456;
server_name qiang.dmxsp.com;
location / {
root /code/qiang;
index index.html index.php;
}
}
检查
[root@web01 ~]$ nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
重启nginx
[root@web01 ~]$ systemctl reload nginx.service
查看端口
tcp LISTEN 0 128 *:8456 *:* users:((“nginx”,pid=8215,fd=12),(“nginx”,pid=2233,fd=12))
tcp LISTEN 0 128 *:85 *:* users:((“nginx”,pid=8215,fd=11),(“nginx”,pid=2233,fd=11))
测试
7.nginx目录结构
目录和文件(yum安装和源安装有所不同)
[root@web02 ~]$ rpm -ql nginx
/etc/logrotate.d/nginx ##日志切割文件
/etc/nginx ##nginx配置文件目录
/etc/nginx/conf.d ##存放站点配置文件的目录
/etc/nginx/conf.d/default.conf ##默认的站点配置文件
/etc/nginx/fastcgi_params ##fastcgi参数
/etc/nginx/mime.types ##媒体类型
/etc/nginx/modules
/etc/nginx/nginx.conf ##nginx主配置文件
/etc/nginx/scgi_params ##scgi参数
/etc/nginx/uwsgi_params ##uwsgi参数
/usr/lib/systemd/system/nginx-debug.service
/usr/lib/systemd/system/nginx.service ##启动文件
/usr/lib64/nginx
/usr/lib64/nginx/modules ##nginx模块存放路径
/usr/libexec/initscripts/legacy-actions/nginx ##引导的脚本文件,没用
/usr/libexec/initscripts/legacy-actions/nginx/check-reload ##引导的脚本文件,没用
/usr/libexec/initscripts/legacy-actions/nginx/upgrade ##引导的脚本文件,没用
/usr/sbin/nginx ##nginx的命令
/usr/sbin/nginx-debug
/usr/share/doc/nginx-1.22.0 ##nginx帮助文档,不需要,看官网即可
/usr/share/doc/nginx-1.22.0/COPYRIGHT ##nginx帮助文档,不需要,看官网即可
/usr/share/man/man8/nginx.8.gz ##nginx帮助文档,不需要,看官网即可
/usr/share/nginx ##nginx默认的页面
/usr/share/nginx/html ##nginx默认的页面
/usr/share/nginx/html/50x.html ##nginx默认的页面
/usr/share/nginx/html/index.html ##nginx默认的页面
/var/cache/nginx ##nginx的缓存目录
/var/log/nginx ##Nginx默认存放日志目录
/etc/nginx/koi-utf ##Nginx编码转换映射文件
/etc/nginx/koi-win ##Nginx编码转换映射文件