1. Nginx介绍
现阶段 web 服务流行的 组合 LNMP(Linux Nginx(“Engine x”) MySQL PHP),前几年 有人还称之为(LEMP) 。Nginx 是 俄罗斯人开发的, 开源的 www 服务软件,程序的大小才780K左右。 Nginx 本身是一款 静态(html,js ,css,jsp 等)www软件,静态文件小 并发高,同时占用资源少。3W并发 10个进程 才使用150M 内存。www服务软件,现在在国内互联网企业web第一名。
使用平台: unix,linux,windows 都可以。
nginx 主要的功能:
a. www web服务,即http服务
b.负载均衡(反向代理proxy)
c.web cache(web缓存)
Nginx的特点:
1,配置简单,更灵活
2,占用资源少。(3W并发 10个进程 才使用150M 内存)
3,功能种类多(web,cache,proxy),但每一个功能并不是最强的
4,支持epoll模型。是的Nginx可以支持高并发(静态小文件),静态1-2W没问题
5,nginx配合动态服务和apache有区别
6,利用nginx可以对IP限速,可以限制连接数
LAMP和LNMP的区别:
Nginx的应用场合:
1、静态服务器(图片,视频服务),另一个lighttpd。并发:1-3W
html,js,css,.flv等。
2、动态服务,nginx+fastcgi的方式运行php,jsp。并发:500-1500。
原因;PHP FCGI :500-1500 mysql :300-1500
apache+php,lighttpd+fcgi php
3、反向代理,负载均衡。日PV2000W以下,都可以直接用Nginx做代理。
haproxy,F5,a10
4、缓存服务。SQUID,VARNISH
各个web服务器的性能对比测试,可参考:http://yifangyou.blog.51cto.com/900206/617213/
2. Nginx安装
2.1 nginx 安装过程
[root@nginx /]# cat /etc/redhat-release #查看系统版本
CentOS Linux release 7.3.1611 (Core)
[root@nginx /]# yum -y install wget #安装wget
[root@nginx /]# cd /etc/yum.repos.d #更换yum源
[root@nginx yum.repos.d]# mv CentOS-Base.repo CentOS-Base.repo.bak
[root@nginx yum.repos.d]# wget -O CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@nginx yum.repos.d]# mv CentOS-Base.repo.bak CentOS-Base.repo
[root@nginx yum.repos.d]# yum clean all
[root@nginx yum.repos.d]# cd
[root@nginx ~]# wget http://nginx.org/download/nginx-1.8.1.tar.gz #下载nginx源码包
[root@nginx ~]# yum -y install pcre-devel openssl-devel gcc gcc-c++ make #安装依赖
[root@nginx ~]# useradd www -s /sbin/nologin -M #添加 www 用户和组
[root@nginx ~]# tar zxf nginx-1.8.1.tar.gz #解压 nginx 源码包
[root@nginx ~]# cd nginx-1.8.1 #进入源码目录
[root@nginx nginx-1.8.1]# ./configure --prefix=/application/nginx-1.8.1 --user=www --group=www --with-pcre --with-http_stub_status_module --with-http_ssl_module #configure
[root@nginx nginx-1.8.1]# make && make install #编译和安装
[root@nginx nginx-1.8.1]# ln -s /application/nginx-1.8.1/ /application/nginx #做软连接
[root@nginx nginx-1.8.1]# /application/nginx/sbin/nginx #启动nginx
[root@nginx nginx-1.8.1]# curl -I http://127.0.0.1 #查看nginx 网站
HTTP/1.1 200 OK
Server: nginx/1.8.1
Date: Mon, 17 Apr 2017 13:43:04 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 17 Apr 2017 13:41:37 GMT
Connection: keep-alive
ETag: "58f4c611-264"
Accept-Ranges: bytes
安装成功!
2.2 nginx启动后不能访问 排查
本地排查:
1)注意启动时 是否有报错
2)先curl 自己IP,看看服务是否启动
3)ps -ef|grep nginx ,看看nginx进程是否活着
Windows浏览器访问:
1)ping 192.168.1.82 物理通不通
2)telnet 192.168.1.82 80 浏览器到web服务通不通
3)服务器本地curl 192.168.1.82 WEB服务开没开
3. Nginx配置文件
3.1 关于Nginx模块
Nginx 使用不同的模块来实现不同的功能,主要有2组重要的模块:
1,Nginx core modules(必须的)
包括
Main
Events
2,Standard HTTP modules (虽然不是必须,但是缺省都会安装,不建议改动),典型的包括
Core
Access
FastCGI
Gzip 压缩模块(性能优化)
Log 日志模块
Proxy
Rewrite URL重写模块
Upstream 负载均衡模块
等等
提示:缺省条件下,这两组模块都会安装,修改配置文件,主要是根据这2组模块的功能来修改模块的参数。
各个模块,参见官方文档:http://nginx.org/en/docs/
3.2 Nginx 默认相关的文件
[root@nginx application]# tree ./nginx/ #查看nginx程序的默认的文件
./nginx/
├── client_body_temp
├── conf #nginx 配置文件目录
│ ├── fastcgi.conf #fastcgi配置文件
│ ├── fastcgi.conf.default
│ ├── fastcgi_params #fastcgi参数配置文件
│ ├── fastcgi_params.default
│ ├── koi-utf
│ ├── koi-win
│ ├── mime.types #nginx 支持的文件类型列表文件
│ ├── mime.types.default
│ ├── nginx.conf #nginx 主网站配置文件
│ ├── nginx.conf.default
│ ├── scgi_params
│ ├── scgi_params.default
│ ├── uwsgi_params
│ ├── uwsgi_params.default
│ └── win-utf
├── fastcgi_temp
├── html
│ ├── 50x.html #访问错误 提示错误的网页文件
│ └── index.html #默认的主页文件
├── logs
│ ├── access.log #访问日志
│ ├── error.log #错误日志
│ └── nginx.pid #nginx的pid文件
├── proxy_temp
├── sbin
│ └── nginx #nginx命令
├── scgi_temp
└── uwsgi_temp
查看nginx初始配置文件
[root@nginx nginx]# egrep -v "#|^$" conf/nginx.conf|cat -n #查看行号
[root@nginx nginx]# egrep -vn "#|^$" conf/nginx.conf #查看行号
3.2.1 nginx.conf 文件注释
[root@nginx conf]# cat nginx.conf
#user nobody; #运行nginx主进程的用户,缺省nobody,我们编译的时候 指定了www
worker_processes 1; #主进程数量,一般和CPU核数一样
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #全局错误日志的类型
#pid logs/nginx.pid; #pid文件
events {
worker_connections 1024; #单个后台worker process进程的最大并发链接数
}
http { #设定http服务器,也可以利用它的反向代理功能提供负载均衡支持
include mime.types; #设定mime类型,类型由mime.type文件定义
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; #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime
#tcp_nopush on; #防止网络阻塞
#keepalive_timeout 0;
keepalive_timeout 65; #连接超时时间
#gzip on; #开启gzip压缩
server { #虚拟主机的配置
listen 80; #监听端口
server_name localhost; #域名可以有多个,用空格隔开
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root 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; #50x 错误网页
location = /50x.html {
root html; #"/50x.html" 网站的根目录
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1; #设置.php结尾的请求到这里来
#}
# 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; #拒绝所有.ht结尾的请求
#}
}
# 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;
# }
#}
}
4,Nginx的web使用示例
nginx 虚拟主机
1,域名区分
2,端口区分
3,IP地址区分
4.1 配置nginx虚拟主机:
1, egrep -v "#|^$" nginx.conf.default >nginx.conf
#去掉注释的内容去掉之后:
[root@co-1 conf]# cat 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 localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
可以简化的写server
server {
listen 80;
server_name www.linuxsp.com;
root html/www;
index index.html index.htm;
}
4.2 做基于域名的虚拟主机
Nginx 虚拟主机:
软件: ——————–Apache —————Nginx
虚拟主机标签: —<VirtualHost>
—— server{}
对于nginx,一个server标签就是一个虚拟主机。
1、基于域名的虚拟主机。通过域名来区分虚拟主机 ====》 应用场景: 外部网站
2、基于端口的虚拟主机。通过端口来区分虚拟主机 ====》 应用场景: 公司内部网站,外部网站的后台
3、基于IP的虚拟主机。几乎不用。不知从ifconfig别名,配置文件即可
1,添加3个server
server {
listen 80;
server_name www.linuxsp.com;
root html/www;
index index.html index.htm;
}
server {
listen 80;
server_name bbs.linuxsp.com;
root html/bbs;
index index.html index.htm;
}
server {
listen 80;
server_name blog.linuxsp.com;
root html/blog;
index index.html index.htm;
}
2,配置网站文件
[root@co-1 conf]# mkdir ../html/{www,bbs,blog}
[root@co-1 conf]# for n in www blog bbs;do echo "$n.linuxsp.com" >../html/$n/index.html;done
[root@co-1 conf]# for n in www blog bbs;do cat ../html/$n/index.html;done
[root@co-1 conf]#nginx -t #检查语法
[root@co-1 conf]# ../sbin/nginx -t
nginx: the configuration file /application/nginx1.6.2/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx1.6.2/conf/nginx.conf test is successful
[root@co-1 conf]# ../sbin/nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) ##表示nginx已启动
[root@co-1 conf]# ../sbin/nginx -s reload #平滑重启
3,在客户端 进行本地hosts文件解析
192.168.48.11 www.linuxsp.com bbs.linuxsp.com blog.linuxsp.com
cmd中ping一下
提示:window 系统很容易出现因没有权限或者杀毒软件修改的hosts文件没保存或被还原
apache默认如果没有首页文件,将显示目录结构,而nginx如果没有首页文件,默认回事403错误
4,基于域名区分的网站测试
再通过浏览器访问: www.linuxsp.com、bbs.linuxsp.com、 blog.linuxsp.com
虽然这三个域名对应的IP是一个,但通过域名访问,显示的网页是不一样
解决恶意域名绑定的问题:
如果nginx配置的是基于域名区分的,客户端也做了host或dns解析,当用户再以ip的方式访问时,访问的将是 第一个server 的内容;如果客户端 别的域名也解析 到这个ip,而nginx上并没有配置这个域名的虚拟主机或主机别名,也会去访问该端口下 nginx的第一个 server 虚拟主机,通过这个原理,我们设置,第一个server 为一个错误网页,活着拒绝访问,就可以防止 恶意将域名绑定到本机的问题。
4.3,nginx基于端口的虚拟主机
server {
listen 8001;
server_name www.linuxsp.com;
root html/www;
index index.html index.htm;
}
server {
listen 8002;
server_name bbs.linuxsp.com;
root html/bbs;
index index.html index.htm;
}
直接该listen后面的端口就行了,而且此时,就不会再区分www.linuxsp.com;
和bbs.linuxsp.com;
8001
显示blog.linuxsp.com
是因为端口
4.4,基于ip的虚拟主机
设置三个虚拟IP
ifconfig eth0:1 192.168.48.21/24
ifconfig eth0:2 192.168.48.22/24
ifconfig eth0:3 192.168.48.23/24
虚拟主机
server {
listen 192.168.48.20:80;
server_name www.linuxsp.com;
root html/www;
index index.html index.htm;
}
server {
listen 192.168.48.21:80;
server_name bbs.linuxsp.com;
root html/bbs;
index index.html index.htm;
}
4.5 配置虚拟主机流程:
1)复制一个完整的 server 标签段 到结尾, 注意:要放在http的大括号里面,server标签 要在http标签中。
2)更改 server_name 及对应网页的 root 根目录
3)检查配置文件语法,平滑重启服务
4)创建server_name 对应网页的根目录,并且建立测试文件,如果没有index首页会出现403错误
5)在客户端对server_name 的主机做host解析或dns配置,并检查(ping 域名看返回的IP对不对)
6)win32浏览器访问,或者在linux客户端做host解析,用wget 或者 curl 访问。
4.6 配置使用stub_status模块
可添加配置status的server模块,查看nginx的状态信息
##status
server{
listen 80;
server_name status.linuxsp.com;
stub_status on;
access_log off;
}
添加hosts,ping测试
nginx -t
nginx -s reload
解释如下:
Active connections:5 #nginx 正处理的活动连接数 5 个
server accepts handled requests
第一个 server 表示 nginx 启动到现在共处理了 26 个链接
第二个 server 表示 nginx 启动到现在共创建 26 个握手
请求丢失数 = (握手数 - 链接数)。可以看出, 本次显示的状态没有丢失请求。
第三个 handled requests, 表示共处理38 次请求
Reading:nginx 读取到客户端的Header 信息数
Writing:nginx 返回给客户端的 Header 信息数
Waiting: nginx 已经处理完 正在等候下一次请求指令的驻留链接,开启keep-alive的情况下,这个值等于 active - (reading + writing)
4.7 别名:
apache指定别名 ServerAlias
nginx指定别名 就在server_name 后面空格再接一个
server {
listen 80;
server_name www.linuxsp.com linuxsp.com;
root html/www;
index index.html index.htm;
}
这种并没有跳转,输入的域名 在网页加载出来之后 地址栏还是这个域名。
想百度那样,输入baidu.com
,域名变成www.baidu.com
百度nginx 301跳转
server {
listen 80;
server_name linuxsp.com;
rewrite ^/(.*) http://www.linuxsp.com/$1 permanent;
}
server {
listen 80;
server_name www.linuxsp.com;
root html/www;
index index.html index.htm;
}
默认情况下域名或IP没和Nginx的server_name匹配上,就会让其访问第一个虚拟主机(和apache差不多)
而第一个虚拟主机做跳转,这样的可以像访问百度那样跳转,这样人家就不能用我们的IP养他的域名,跳转
也可以增加自己网站的流量。
功能:
1)别名地址栏还是linuxsp.com ===>www.linuxsp.com
的内容
2)linuxsp.com
跳转地址栏www.linuxsp.com
(rewrite)
4.8 拒绝IP,非法域名访问
server {
listen 80;
location / {
deny all;
}
}
4.9 nginx日志
可在nginx.conf.default
中拷贝
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
放到http{}
标签中,这是访问日志的格式
单独为某个虚拟主机开启访问日志,如:
同理也可以开启错误日志
也可以在main{}中开启,表示所有的虚拟主机都开启
测试一下:
[root@nginx conf]# ../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@nginx conf]# ../sbin/nginx -s reload
[root@nginx conf]# ll ../logs/
total 12
-rw-r--r-- 1 root root 1508 Apr 19 14:27 access.log
-rw-r--r-- 1 root root 2106 Apr 19 15:01 error.log
-rw-r--r-- 1 root root 0 Apr 19 15:00 error_test.log
-rw-r--r-- 1 root root 5 Apr 17 13:42 nginx.pid
[root@nginx conf]# ../sbin/nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
^C
[root@nginx conf]# cat ../logs/error_test.log
2017/04/19 15:01:37 [emerg] 2627#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2017/04/19 15:01:37 [emerg] 2627#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2017/04/19 15:01:37 [emerg] 2627#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
#错误日志被记录了。
用法可以看官方文档,写的很清楚
常用nginx 日志变量:
$remote_addr 直接客户端地址
$http_x_forwarded_for 间接客户端地址(一般前面会有个代理服务器)
$remote_user 远程客户端用户名称
$time_local 记录访问时间和时区
$request 用户的请求,使用HTTTP协议
$status 返回状态,200,404,304 等等
$body_bytes_sents 发送的body 字节数
$http_referer 引用页(从哪个链接访问来的)
$http_user_agent 客户端浏览器
apache的日志轮询工具
1),cronolog
2),rotatelog
3),crond + mv +graceful ===
nginx常用的是apache的第三种
首先,写脚本 cut_nginx_log.sh
[root@co-1 logs]# vim /server/script/cut_nginx_log.sh
##cut_nginx_log.sh
cd /application/nginx/logs
/bin/mv www_access.log www_access_$(date +%F -d -1day).log
/application/nginx/sbin/nginx -s reload
#上面的这种方法要重载nginx服务
日志量不是很大的话,建议下面的操作
/usr/bin/cp www_access.log www_access_$(date +%F -d -1day).log
>./www_access.log
添加定时任务
[root@co-1 logs]# crontab -e
###cut_nginx_log
00 00 * * * /bin/sh /server/script/cut_nginx_log.sh >/dev/null 2>&1
4.10 生产环境常见的HTTP状态码列表:
200: OK,服务器成功返回网页
301: Moved Permanently (永久跳转),请求的网页永久跳转到新位置。
403: Forbidden(禁止访问),服务器拒绝请求
404: Not Fount,服务器找不到请求的页面
502: Bad Gatway(坏的网关),一般是网关服务器请求后端服务时,后端服务没有按照http协议正确返回结果
503: Service Unavailable(服务当前不可用),可能因为超载或停机维护
504: Gateway Timeout(网关超时),一般是网关服务器请求后端服务时,后端服务没有在特定时间内完成服务
4.11 使用extra目录(更好的扩展),多虚拟主机可减少杂乱
先在conf
下创建extra
目录
在nginx.conf
中添加
include extra/www.conf;
include extra/bbs.conf;
#上面是一条条的,也可以如下配置
include vhost/*.conf;
#这样配置了,只要在vhost目录下添加配置文件就可以了
[root@co-1 conf]# vim extra/www.conf
server {
listen 80;
server_name www.etiantian.org;
root html/www;
index index.html index.htm;
}