linux的nginx的安装和使用
1.下载源码包 wget -c https://nginx.org/download/nginx-1.12.0.tar.gz2.解压缩源码tar -zxvf nginx-1.12.0.tar.gz3.配置,编译安装 开启nginx状态监测功能./configure --prefix=/opt/nginx1-12/ --with-http_ssl_module --with-http_stub_status_module make && make install 4.启动nginx,进入sbin目录,找到nginx启动命令cd sbin./nginx #启动./nginx -s stop #关闭./nginx -s reload #重新加载安装完成后检测服务
netstat -tunlp |grep 80curl -I 127.0.0.1#如果访问不了,检查selinux,iptables部署一个web站点
nginx默认站点是Nginx目录下的html文件夹,这里可以从nginx.conf中查到
location /{ root html; #这里是默认的站点html文件夹,也就是 /opt/nginx1-12/html/文件夹下的内容 index index.html index.htm; #站点首页文件名是index.html }
如果要部署网站业务数据,只需要把开发好的程序全放到html目录下即可
[root@oldboy_python /tmp 11:34:52]#ls /opt/nginx1-12/html/ index.html jssts.jpeg lhy.mp4 man.jpg wget-log
因此只需要通过域名/资源,即可访问
http://www.pyyuc.cn/man.jpgNginx的目录结构
[root@oldboy_python /opt/nginx1-12 11:44:02]#ls client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp static uwsgi_tempconf 存放nginx所有配置文件的目录,主要nginx.conf html 存放nginx默认站点的目录,如index.html、error.html等 logs 存放nginx默认日志的目录,如error.log access.log sbin 存放nginx主命令的目录,sbin/nginx Nginx主配置文件解析
Nginx主配置文件/etc/nginx/nginx.conf是一个纯文本类型的文件,整个配置文件是以区块的形式组织的。一般,每个区块以一对大括号{}来表示开始与结束。
CoreModule核心模块 user www; #Nginx进程所使用的用户 worker_processes 1; #Nginx运行的work进程数量(建议与CPU数量一致或auto) error_log /log/nginx/error.log #Nginx错误日志存放路径 pid /var/run/nginx.pid #Nginx服务运行后产生的pid进程号
events事件模块 events { worker_connections //每个worker进程支持的最大连接数 use epool; //事件驱动模型, epoll默认 }
http内核模块 //公共的配置定义在http{} http { //http层开始 ... //使用Server配置网站, 每个Server{}代表一个网站(简称虚拟主机) 'server' { listen 80; //监听端口, 默认80 server_name localhost; //提供服务的域名或主机名 access_log host.access.log //访问日志 //控制网站访问路径 'location' / { root /usr/share/nginx/html; //存放网站代码路径 index index.html index.htm; //服务器返回的默认页面文件 } //指定错误代码, 统一定义错误页面, 错误代码重定向到新的Locaiton error_page 500 502 503 504 /50x.html; } ... //第二个虚拟主机配置 'server' { ... } include /etc/nginx/conf.d/*.conf; //包含/etc/nginx/conf.d/目录下所有以.conf结尾的文件 } //http层结束Nginx虚拟主机
如果每台linux服务器只运行了一个小网站,那么人气低,流量小的草根站长需要承担高额的服务器租赁费,也造成了硬件资源浪费。
虚拟主机就是将一台服务器分割成多个“虚拟服务器”,每个站点使用各自的硬盘空间,由于省资源,省钱,众多网站都使用虚拟主机来部署网站。
虚拟主机的概念就是在web服务里的一个独立的网站站点,这个站点对应独立的域名(IP),具有独立的程序和资源目录,可以独立的对外提供服务。这个独立的站点配置是在nginx.conf中使用server{}代码块标签来表示一个虚拟主机。Nginx支持多个server{}标签,即支持多个虚拟主机站点。
虚拟主机类型
基于域名的虚拟主机 通过不同的域名区分不同的虚拟主机,是企业应用最广的虚拟主机。基于端口的虚拟主机通过不同的端口来区分不同的虚拟主机,一般用作企业内部网站,不对外直接提供服务的后台,例如www.pythonav.cn:9000基于IP的虚拟主机通过不同的IP区分不同的虚拟主机,此类比较少见,一般业务需要多IP的常见都会在负载均衡中绑定VIP
Nginx状态信息(status)配置及信息详解 nginx与php-fpm一样内建了一个状态页,对于想了解nginx的状态以及监控nginx非常有帮助。为了后续的zabbix监控,我们需要先了解一下nginx的状态页。 Nginx状态信息(status)介绍 Nginx软件在编译时又一个with-http_stub_status_module模块,这个模块功能是记录Nginx的基本访问状态信息,让使用者了解Nginx的工作状态。要想使用状态模块,在编译时必须增加--with-http_stub_status_module参数。
监测你的nginx是否安装了status模块
[root@master conf]# /opt/nginx/sbin/nginx -V nginx version: nginx/1.12.0 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) configure arguments: --prefix=/opt/nginx/ --with-http_stub_status_module
启动status状态功能,修改配置文件
#在访问ip/status的时候,进入状态功能 location /status { #开启nginx状态功能 stub_status on; }
平滑重启nginx
./sbin/nginx -s reload
访问status页面
http://192.168.119.10/status
通过ab压测命令检测
-n requests #执行的请求数,即一共发起多少请求。
-c concurrency #请求并发数。
-k #启用HTTP KeepAlive功能,即在一个HTTP会话中执行多个请求。
ab -kc 1000 -n 100000 http://192.168.119.10/
status页面解析
基于域名的多虚拟主机实战
nginx可以自动识别用户请求的域名,根据不同的域名请求服务器传输不同的内容,只需要保证服务器上有一个可用的ip地址,配置好dns解析服务。
/etc/hosts是linux系统中本地dns解析的配置文件,同样可以达到域名访问效果
修改nginx.conf
[root@oldboy_python ~ 14:33:16]#egrep -v '#|^$' /opt/nginx1-12/conf/nginx.conf worker_processes 1; events { worker_connections 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; keepalive_timeout 65; server { listen 80; server_name www.pyyuc.cn; location /{ root html/pyyuc; index index.html index.htm; } } }
上述代码配置了一个www.pyyuc.cn域名的站点,虚拟主机的部分就是server{}里的内容
创建pyyuc.cn的站点目录和文件[root@oldboy_python /opt/nginx1-12/html 14:36:08]#mkdir pyyuc
[root@oldboy_python /opt/nginx1-12/html 14:36:18]#echo "<meta charset=utf8>我是pyyuc站点" > pyyuc/index.html[root@oldboy_python /opt/nginx1-12/html 14:37:21]#cat pyyuc/index.html<meta charset=utf8>我是pyyuc站点
上述作用创建了一个html/pyyuc站点目录,对应于虚拟主机配置文件里的root根目录的设置html/pyyuc
然后生成一个首页文件index.html,内容是“我是pyyuc站点”
检查nginx语法重新加载nginx[root@oldboy_python /opt/nginx1-12/html 14:37:28]#../sbin/nginx -tnginx: the configuration file /opt/nginx1-12/conf/nginx.conf syntax is oknginx: configuration file /opt/nginx1-12/conf/nginx.conf test is successful
#平滑重启nginx
[root@oldboy_python /opt/nginx1-12/html 14:39:18]#../sbin/nginx -s reload
检查nginx端口,进程,访问pyyuc虚拟站点
[root@oldboy_python /opt/nginx1-12/html 14:40:02]#netstat -tunlp|grep nginx[root@oldboy_python /opt/nginx1-12/html 14:40:29]#ps -ef|grep nginx#我这里是有dns解析,没有的话则需要/etc/hosts解析#成功配置了pyyuc虚拟主机站点[root@oldboy_python /opt/nginx1-12/html 14:41:37]#curl www.pyyuc.cn<meta charset=utf8>我是pyyuc站点配置多个域名的虚拟主机
其实就是新增一个server{}虚拟主机
egrep -v '#|^$' /opt/nginx1-12/conf/nginx.conf worker_processes 1; events { worker_connections 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; keepalive_timeout 65; server { listen 80; server_name www.pyyuc.cn; location /{ root html/pyyuc; index index.html index.htm; } } server { listen 80; server_name www.pythonav.cn; location /{ root html/pythonav; index index.html index.htm; } } }
创建pythonav虚拟主机站点的目录和文件
[root@oldboy_python /opt/nginx1-12 14:47:21]#mkdir -p /opt/nginx1-12/html/pythonav[root@oldboy_python /opt/nginx1-12 14:49:33]#echo "<meta charset=utf8>我是pythonav,未成年禁止入内"> /opt/nginx1-12/html/pythonav/index.html
[root@oldboy_python /opt/nginx1-12 14:50:44]#./sbin/nginx -t nginx: the configuration file /opt/nginx1-12/conf/nginx.conf syntax is ok nginx: configuration file /opt/nginx1-12/conf/nginx.conf test is successful [root@oldboy_python /opt/nginx1-12 14:51:32]#./sbin/nginx -s reload大功告成,基于域名的虚拟主机实战搞定
[root@oldboy_python /opt/nginx1-12 14:52:12]#curl www.pythonav.cn <meta charset=utf8>我是pythonav,未成年禁止入内 [root@oldboy_python /opt/nginx1-12 14:52:40]#curl www.pyyuc.cn <meta charset=utf8>我是pyyuc站点nginx访问日志(access_log)
日志功能对每个用户访问网站的日志信息记录到指定的日志文件里,开发运维人员可以分析用户的浏览器行为,此功能由ngx_http_log_module模块负责,官网地址是:
http://nginx.org/en/docs/http/ngx_http_log_module.html
控制日志的参数
log_format 记录日志的格式,可定义多种格式 accsss_log 指定日志文件的路径以及格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
对应参数解析
$remote_addr 记录客户端ip $remote_user 远程用户,没有就是 “-”$time_local 对应[14/Aug/2018:18:46:52 +0800]$request 对应请求信息"GET /favicon.ico HTTP/1.1"$status 状态码$body_bytes_sent 571字节 请求体的大小$http_referer 对应“-” 由于是直接输入浏览器就是 -$http_user_agent 客户端身份信息$http_x_forwarded_for 记录客户端的来源真实ip 97.64.34.118
日志效果如下
66.102.6.6 - - [14/Aug/2018:18:46:52 +0800] "GET /favicon.ico HTTP/1.1" 404 571 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36 Google Favicon" "97.64.34.118"
nginx.conf默认配置
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;
日志格式配置定义
log_format是日志关键字参数,不能变 main是日志格式指定的标签,记录日志时通过main标签选择指定的格式。nginx限制网站来源IP访问
如果哪天发现你的nginx很慢,或者检查access.log时候,有一个some body疯狂请求你的nginx server,那么可以禁止这个IP访问
限制ip或ip段访问禁止访问/av/底下的资源 location /av { deny 122.71.240.254; #alias /opt/nginx1-12/html/av; allow 10.1.1.0/16; }
Nginx错误页面优化
在网站运行过程中,可能因为页面不存在等原因,导致网站无法正常响应请求,此时web服务会返回系统的错误码,但是默认的错误页面很不友好。
因此我们可以将404,403等页面的错误信息重定向到网站首页或者其他指定的页面,提升用户访问体验。
server { listen 80; server_name www.pythonav.cn; root html/pythonav; location /{ index index.html index.htm; } #在pythonav路径下的40x.html错误页面 error_page 400 403 404 405 /40x.html; }
40x.html
<img style='width:100%;height:100%;' src=https://pic1.zhimg.com/80/v2-77a9281a2bebc7a2ea5e02577af266a8_hd.png>
此时访问www.pythonav.cn/asdasd错误页面已经优化了
Nginx代理 正向代理
正向代理,也就是传说中的代理,他的工作原理就像一个跳板(***),简单的说:
我是一个用户,我访问不了某网站,但是我能访问一个代理服务器,这个代理服务器呢,他能访问那个我不能访问的网站,于是我先连上代理服务器,告诉他我需要那个无法访问网站的内容,代理服务器去取回来,然后返回给我。
反向代理
对于客户端而言,代理服务器就像是原始服务器。
nginx实现负载均衡的组件
ngx_http_proxy_module proxy代理模块,用于把请求抛给服务器节点或者upstream服务器池实现一个简单的反向代理
机器准备,两台服务器
master 192.168.11.63 主负载slave 192.168.11.64 web1
主负载均衡节点的配置文件
worker_processes 1; error_log logs/error.log; pid logs/nginx.pid; events { worker_connections 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; keepalive_timeout 65; upstream slave_pools{ server 192.168.11.64:80 weight=1; } server { listen 80; server_name localhost; location / { proxy_pass http://slave_pools; root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }nginx.conf