Nginx专为性能优化而开发,其最知名的优势是它的稳定性和低系统资源消耗,以及对HTTP并发链接的高处理能力(单台物理服务器可支持30000~50000个并发请求)。正由于如此,大量提供社交网络、新闻资讯、电子商务及虚拟主机等服务的企业纷纷选择Nginx来提供Web服务。php
Nginx是一个很牛的高性能Web和反向代理服务器,它具备有不少很是优越的特性:html
- 高并发链接:官方测试能支撑5万并发链接,在实际生产环境中跑到2,~3W并发连;
- 内存消耗少:在3W并发链接下,开启的10个NGINX进程才消耗150M内存(15M*10=150M);
- 配置文件很是简单:风格跟程序同样通俗易懂;
- 成本低廉:Nginx做为开源软件,能够无偿使用,而购买F5 BIG-IP、NetScaler等硬件负载均衡交换机则须要十多万至几十万人民币;
- 支持rewrite重写规则:可以根据域名、URL的不一样,将HTTP请求分发到不一样的后端服务器群组;
- 内置的健康检查功能:若是Nginx Proxy后端的后台web服务器宕机了,不会影响前端访问;
- 节省带宽:支持GZIP压缩,能够添加浏览器本地缓存的Header头;
- 稳定性高:用于反向代理,宕机的几率微乎其微;
Nginx最新的稳定版本为1.12.0,其安装文件能够从官方网站Nginx官方网站/下载。前端
1)Centos 7服务器一台;
2)Windows客户端一台:
3)Centos 7操做系统镜像;
4)Nginx镜像;
安装Nginx用到的全部镜像及软件包能够访问网盘提取:https://pan.baidu.com/s/18iRCuiMEyGbEFSeBp17uVQ
提取码:qsztnginx
[root@centos02 ~]# mount /dev/cdrom /mnt/ <!--挂载光盘--> mount: /dev/sr0 写保护,将以只读方式挂载 [root@centos02 ~]# cp /mnt/nginx-1.6.0.tar.gz /usr/src/ <!--拷贝Nginx包到/usr/src/目录-->
[root@centos02 ~]# umount /mnt/ <!--卸载光盘-->
web
[root@centos02 ~]# mount /dev/cdrom /mnt/ <!--挂载光盘--> mount: /dev/sr0 写保护,将以只读方式挂载 [root@centos02 ~]# cp /mnt/* /usr/src/ <!--将光盘目录下全部数据拷贝到/usr/src/目录-->
[root@centos02 ~]# umount /mnt/ <!--卸载光盘-->
vim
[root@centos02 ~]# mount /dev/cdrom /mnt/ <!--挂载光盘--> mount: /dev/sr0 写保护,将以只读方式挂载 [root@centos02 ~]# rm -rf /etc/yum.repos.d/CentOS-* <!--清除系统自带yum源--> [root@centos02 ~]# yum -y install pcre-devel zlib-devel <!--安装Nginx的依赖程序--> [root@centos02 ~]# useradd -M -s /sbin/nologin nginx <!--建立管理Nginx的用户--> [root@centos02 ~]# tar zxvf /usr/src/nginx-1.6.0.tar.gz -C /usr/src/ <!--解压缩Nginx软件包--> [root@centos02 ~]# cd /usr/src/nginx-1.6.0/ [root@centos02 nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module <!--配置Nginx--> [root@centos02 nginx-1.6.0]# make && make install <!--编译安装Nginx--> [root@centos02 ~]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/ <!--优化Nginx执行命令-->
[root@centos02 ~]# nginx <!--启动Nginx服务--> [root@centos02 ~]# netstat -anptu | grep nginx <!--监听Nginx服务是否启动成功--> tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4663/nginx: master [root@centos02 ~]# killall -s QUIT nginx <!--关闭Nginx服务--> [root@centos02 ~]# killall -3 nginx <!--关闭Nginx服务--> [root@centos02 ~]# killall -1 nginx <!--从新启动Nginx--> [root@centos02 ~]# killall -s HUP nginx <!--从新启动Nginx--> [root@centos02 ~]# vim /etc/init.d/nginx <!--编写Nginx服务管理脚本--> #!/bin/bash #chkconfig: 35 90 30 #description:nginx server PROG="/usr/local/nginx/sbin/nginx" PIDF="/usr/local/nginx/logs/nginx.pid" case "$1" in start) $PROG ;; stop) kill -s QUIT $(cat $PIDF) ;; restart) $0 stop $0 start ;; reload) kill 0s HUP $(cat $PIDF) ;; *) echo "Usage:$0 (start|stop|restart|reload)" exit 1 esac exit 0 [root@centos02 ~]# chmod +x /etc/init.d/nginx <!--添加脚本执行权限--> [root@centos02 ~]# chkconfig --add nginx <!--添加为系统服务--> [root@centos02 ~]# chkconfig --level 35 nginx on <!--设置开机自动启动--> [root@centos02 ~]# /etc/init.d/nginx stop<!--脚本中止Nginx服务--> [root@centos02 ~]# /etc/init.d/nginx start<!--脚本启动Nginx服务--> [root@centos02 ~]# /etc/init.d/nginx restart<!--脚本重启Nginx服务-->
客户端配置和Nginx服务器同一块网卡同网段,设置网关便可访问Nginx网站服务器后端
[root@centos02 ~]# ls -ld /usr/local/nginx/conf/nginx.conf -rw-r--r-- 1 root root 2656 11月 28 17:22 /usr/local/nginx/conf/nginx.conf <!--Nginx的主配置文件--> [root@centos02 ~]# ls -ld /usr/local/nginx/sbin/ drwxr-xr-x 2 root root 19 11月 28 17:22 /usr/local/nginx/sbin/ <!--管理Nginx服务程序文件--> [root@centos02 ~]# ls -ld /usr/local/nginx/html/ drwxr-xr-x 2 root root 40 11月 28 17:22 /usr/local/nginx/html/ <!--Nginx的网站根目录--> [root@centos02 ~]# ls -ld /usr/local/nginx/logs/ drwxr-xr-x 2 root root 58 11月 28 17:47 /usr/local/nginx/logs/ <!--Nginx的网站日志记录-->
[root@centos02 ~]# cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak <!--备份主配置文件--> [root@centos02 ~]# vim /usr/local/nginx/conf/nginx.conf <!--编辑主配置文件--> 3 user nginx; <!--运行用户--> 4 worker_processes 1; <!--工做进程数量--> 6 error_log logs/error.log; <!--错误日志文件的位置--> 12 pid logs/nginx.pid; <!--PID文件的位置--> 16 use epoll; <!--使用epoll模型--> 17 worker_connections 1024; <!--每进程处理1024个链接--> 29 #access_log logs/access.log main; <!--访问日志的位置--> 31 sendfile on; <!--开启高效传输文件模式--> 35 keepalive_timeout 65; <!--链接保持超时--> 39 server { <!--server的开始,一个server表示一个虚拟主机--> 40 listen 80; <!--Web服务的监听配置--> 41 server_name localhost; <!--网站名称(FQDN)--> 44 charset utf-8; <!--网页的默认字符集--> 48 location / { <!--根目录配置--> 49 root html; <!--网站根目录的位置,相对于安装目录--> 50 index index.html index.html;<!--默认首页(索引页)--> 51 } 84 } <!--server结束-->
listen:限定端口的同时容许限定IP地址,采用“IP地址:端口号”形式,root语句用来设置特定访问位置的网页文档路径,默认为Nginx安装目录下的html/目录,根据须要可改成/var/www/html等其余路径,但更改后需保证nginx用户对其具备读取权限。centos
worker_processes :表示工做进程的数量,若服务器由多块CPU或者使用多核处理器,能够参考CPU核心总数来指定工做进程数。具体含义在worker_connections配置项中体现出来。浏览器
[root@centos02 ~]# vim /usr/local/nginx/conf/nginx.conf <!--修改nginx配置文件,指定访问位置并打开stub_status配置--> 52 location /status { 53 stub_status on; 54 access_log off; 55 } [root@centos02 ~]# /etc/init.d/nginx restart<!--重启Nginx服务-->
客户端访问状态统计页:缓存
Active connections:表示当前的活动链接数;
- server accepts handled requests:表示已经处理的链接信息,三个数字依次表示已处理的链接数、成功的TCP握手次数、已经处理的请求数。
[root@centos02 ~]# yum -y install bind bind-chroot bind-utils <!--安装DNS--> [root@centos02 ~]# echo "" > /etc/named.conf <!--清空主配置文件--> [root@centos02 ~]# vim /etc/named.conf <!--修改主配置文件--> options { listen-on port 53 { 192.168.100.20; }; directory "/var/named"; } zone "benet.com" IN { type master; file "benet.com.zone"; } zone "accp.com" IN { type master; file "accp.com.zone"; } [root@centos02 ~]# named-checkconf -z /etc/named.conf <!--检查主配置文件是否配置错误--> [root@centos02 ~]# vim /var/named/benet.com.zone <!--配置benet.com的正向解析区域--> $TTL 86400 @ SOA benet.com. root.benet.com( 2019112801 1H 15M 1W 1D ) @ NS centos02.benet.com. centos02 A 192.168.100.20 www A 192.168.100.20 [root@centos02 ~]# chmod +x /var/named/benet.com.zone <!--正向解析区域配置文件添加执行权限--> [root@centos02 ~]# chown named:named /var/named/benet.com.zone <!--修改属主属组--> [root@centos02 ~]# named-checkzone benet.com /var/named/benet.com.zone <!--检查benet.com正向解析区域配置文件是否错误--> zone benet.com/IN: loaded serial 2019112801 OK [root@centos02 ~]# cp /var/named/benet.com.zone /var/named/accp.com.zone <!--复制benet.com正向解析区域到accp.com正向解析区域--> [root@centos02 ~]# vim /var/named/accp.com.zone <!--修改accp.com正向解析区域配置文件--> $TTL 86400 @ SOA accp.com. root.accp.com( 2019112801 1H 15M 1W 1D ) @ NS centos02.accp.com. centos02 A 192.168.100.20 www A 192.168.100.20 [root@centos02 ~]# named-checkzone accp.com /var/named/accp.com.zone <!--检查accp.com正向解析区域配置文件是否错误--> [root@centos02 ~]# vim /etc/sysconfig/network-scripts/ ifcfg-ens32 <!--编辑网卡添加主DNS--> DNS1=192.168.100.20 <!--添加主DNS--> [root@centos02 ~]# systemctl restart network<!--重启网卡服务--> [root@centos02 ~]# systemctl start named <!--启动DNS服务器--> [root@centos02 ~]# systemctl enable named<!--设置开机自动启动--> [root@centos02 ~]# nslookup www.benet.com <!--解析域名测试是否正常--> Server: 192.168.100.20 Address: 192.168.100.20#53 Name: www.benet.com Address: 192.168.100.20 [root@centos02 ~]# nslookup www.accp.com <!--解析域名测试是否正常--> Server: 192.168.100.20 Address: 192.168.100.20#53 Name: www.accp.com Address: 192.168.100.20
Nginx的配置文件使用“http { }”界定标记用于设定HTTP服务器,包括访问日志、http端口、网页目录、默认字符集、链接保持,以及虚拟web主机、php解析等网站全局设置,其中大部分包含在子界定标记 “ server { }”内。“ server { }”表明一个具体的网站设置。
<!--建立虚拟主机网站根目录--> [root@centos02 ~]# mkdir -p /var/www/benetcom [root@centos02 ~]# mkdir -p /var/www/accpcom <!--建立虚拟主机的网站主页--> [root@centos02 ~]# echo "www.benet.com" > /var/www/benetcom/index.html [root@centos02 ~]# echo "www.accp.com" > /var/www/accpcom/index.html [root@centos02 ~]# vim /usr/local/nginx/conf/nginx.conf <!--修改Nginx主配置文件支持虚拟主机--> 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; server { <!--server开始--> listen www.benet.com:80; <!--监听的域名及端口--> server_name www.benet.com; <!--网站名称--> charset utf-8; <!--默认字符集--> access_log logs/www.benet.com.access.log; <!--访问日志位置--> error_log logs/www.benet.com.error.log; <!--错误日志位置--> location / { <!--根目录配置--> root /var/www/benetcom/; <!--网站根目录--> index index.html; <!--默认首页--> } } <!--server结尾--> <!--如下配置请参考以上注释--> server { listen www.accp.com:80; server_name www.accp.com; charset utf-8; access_log logs/www.accp.com.access.log; error_log logs/www.accp.com.error.log; location / { root /var/www/accpcom/; index index.html; } } [root@centos02 ~]# nginx -t <!--检查Nginx是否配置错误--> nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@centos02 ~]# systemctl restart named<!--从新启动DNS服务--> [root@centos02 ~]# /etc/init.d/nginx restart<!--从新启动Nginx服务-->
客户端添加DNS地址,访问域名测试是否成功
—————— 本文至此结束,感谢阅读 ——————