Nginx专为性能优化而开发,其最知名的优点是它的稳定性和低系统资源消耗,以及对HTTP并发连接的高处理能力(单台物理服务器可支持30000~50000个并发请求)。正因为如此,大量提供社交网络、新闻资讯、电子商务及虚拟主机等服务的企业纷纷选择Nginx来提供Web服务。
专业从事企业网站建设和网站设计服务,包括网站建设、国际域名空间、网页空间、企业邮箱、微信公众号开发、微信支付宝成都微信小程序、手机APP定制开发、软件开发、等服务。公司始终通过不懈的努力和以更高的目标来要求自己,在不断完善自身管理模式和提高技术研发能力的同时,大力倡导推行新经济品牌战略,促进互联网事业的发展。
Nginx是一个很牛的高性能Web和反向代理服务器,它具有有很多非常优越的特性:
- 高并发连接:官方测试能支撑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
提取码:qszt
[root@centos02 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos02 ~]# cp /mnt/nginx-1.6.0.tar.gz /usr/src/
[root@centos02 ~]# umount /mnt/ <!--卸载光盘-->
[root@centos02 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos02 ~]# cp /mnt/* /usr/src/
[root@centos02 ~]# umount /mnt/ <!--卸载光盘-->
[root@centos02 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos02 ~]# rm -rf /etc/yum.repos.d/CentOS-*
[root@centos02 ~]# yum -y install pcre-devel zlib-devel
[root@centos02 ~]# useradd -M -s /sbin/nologin nginx
[root@centos02 ~]# tar zxvf /usr/src/nginx-1.6.0.tar.gz -C /usr/src/
[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
[root@centos02 nginx-1.6.0]# make && make install
[root@centos02 ~]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
[root@centos02 ~]# nginx
[root@centos02 ~]# netstat -anptu | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4663/nginx: master
[root@centos02 ~]# killall -s QUIT nginx
[root@centos02 ~]# killall -3 nginx
[root@centos02 ~]# killall -1 nginx
[root@centos02 ~]# killall -s HUP nginx
[root@centos02 ~]# vim /etc/init.d/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
[root@centos02 ~]# /etc/init.d/nginx start
[root@centos02 ~]# /etc/init.d/nginx restart
客户端配置和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
[root@centos02 ~]# ls -ld /usr/local/nginx/sbin/
drwxr-xr-x 2 root root 19 11月 28 17:22
/usr/local/nginx/sbin/
[root@centos02 ~]# ls -ld /usr/local/nginx/html/
drwxr-xr-x 2 root root 40 11月 28 17:22
/usr/local/nginx/html/
[root@centos02 ~]# ls -ld /usr/local/nginx/logs/
drwxr-xr-x 2 root root 58 11月 28 17:47
/usr/local/nginx/logs/
[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;
16 use epoll;
17 worker_connections 1024;
29 #access_log logs/access.log main;
31 sendfile on;
35 keepalive_timeout 65;
39 server {
40 listen 80;
41 server_name localhost;
44 charset utf-8;
48 location / {
49 root html;
50 index index.html index.html;
51 }
84 }
listen:限定端口的同时允许限定IP地址,采用“IP地址:端口号”形式,root语句用来设置特定访问位置的网页文档路径,默认为Nginx安装目录下的html/目录,根据需要可改为/var/www/html等其他路径,但更改后需保证nginx用户对其具有读取权限。
worker_processes :表示工作进程的数量,若服务器由多块CPU或者使用多核处理器,可以参考CPU核心总数来指定工作进程数。具体含义在worker_connections配置项中体现出来。
[root@centos02 ~]# vim /usr/local/nginx/conf/nginx.conf
52 location /status {
53 stub_status on;
54 access_log off;
55 }
[root@centos02 ~]# /etc/init.d/nginx restart
客户端访问状态统计页:
Active connections:表示当前的活动连接数;
- server accepts handled requests:表示已经处理的连接信息,三个数字依次表示已处理的连接数、成功的TCP握手次数、已经处理的请求数。
[root@centos02 ~]# yum -y install bind bind-chroot bind-utils
[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
$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
zone benet.com/IN: loaded serial 2019112801
OK
[root@centos02 ~]# cp /var/named/benet.com.zone
/var/named/accp.com.zone
[root@centos02 ~]# vim /var/named/accp.com.zone
$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
[root@centos02 ~]# vim /etc/sysconfig/network-scripts/
ifcfg-ens32
DNS1=192.168.100.20
[root@centos02 ~]# systemctl restart network
[root@centos02 ~]# systemctl start named
[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
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 {
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 {
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: 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
[root@centos02 ~]# /etc/init.d/nginx restart
客户端添加DNS地址,访问域名测试是否成功
—————— 本文至此结束,感谢阅读 ——————