原文:https://blog.51cto.com/11134648/2130987html
Nginx专为性能优化而开发,最知名的优势是它的稳定性和低系统资源消耗,以及对HTTP并发链接的高处理能力,单个物理服务器可支持30000-50000个并发请求。nginx
Nginx的安装文件能够从官方网站http://www.nginx.org/下载,下面以Nginx1.12版本为例,基于CentOS7,部署Nginx网站服务。c++
Nginx的配置及运行须要gcc 、 gcc-c++ 、 make 、 pcre、pcre-devel、zlib-devel软件包的支持,以便提供相应的库和头文件,确保Nginx安装顺利。shell
建立yum仓库的步骤详细步骤请参考 Linux下经过rdesktop远程登录Windows系统vim
yum install gcc gcc-c++ make pcre pcre-devel zlib-devel -y
若是是在有网络的状况下,CentOS7无需建立yum仓库,直接执行yum list命令更新一下yum源,稍微等待一下子。centos
yum list //更新yum源 yum install gcc gcc-c++ make pcre pcre-devel zlib-devel -y
Nginx服务程序默认以nobody身份运行,建议为其建立专门的用户帐号,以便更准确的控制其访问权限,增长灵活性,下降安全风险。浏览器
useradd -M -s /sbin/nologin nginx //建立一个名为nginx用户,不创建宿主文件夹,禁止登陆到shell环境
tar xzvf nginx-1.12.0.tar.gz -C /opt //解压Nginx软件至opt目录下
cd /opt/nginx-1.12.0/ //切换到Nginx目录下
根据实际须要配置Nginx的具体选项,配置前可参考“./configure --help”给出的说明。安全
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_moduleruby
make //生成二进制文件 make install //编译安装
建立Nginx主程序的连接文件是为了方便管理员直接“nginx”命令就能够调用Nginx的主程序。性能优化
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
Nginx的主程序提供了“-t”选项来对配置文件进行检查,以便找出不当或错误的配置。
[root@centos7-1 nginx-1.12.0]# 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
直接运行Nginx便可启动Nginx服务器
[root@centos7-1 nginx-1.12.0]# nginx [root@centos7-1 nginx-1.12.0]# killall -1 nginx //重启nginx服务 [root@centos7-1 nginx-1.12.0]# killall -3 nginx //中止nginx服务
为了使nginx服务的启动、中止、重载等操做更加方便,能够编写nginx服务脚本,并使用chkconfig和systemctl工具来进行管理,这更加符合系统的管理习惯。
[root@centos7-1 nginx-1.12.0]# vim /etc/init.d/nginx #!/bin/bash # chkconfig: - 99 20 # description: Nginx Service Control Script PROG="/usr/local/nginx/sbin/nginx" //主程序路径 PIDF="/usr/local/nginx/logs/nginx.pid" //PID存放路径 case "$1" in start) $PROG ;; stop) kill -s QUIT $(cat $PIDF) //根据PID停止nginx进程 ;; restart) $0 stop $0 start ;; reload) kill -s HUP $(cat $PIDF) //根据进程号重载配置 ;; *) echo "Usage: $0 {start|stop|restart|reload}" exit 1 esac exit 0
[root@centos7-1 nginx-1.12.0]# chmod +x /etc/init.d/nginx [root@centos7-1 nginx-1.12.0]# chkconfig --add nginx //添加为系统服务 [root@centos7-1 nginx-1.12.0]# systemctl start nginx.service
经过检查Nginx程序的监听状态,或者在浏览器中访问此Web服务,默认页面将显示“Welcome to nginx!”
[root@centos7-1 nginx-1.12.0]# netstat -antp | grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 54386/nginx: master [root@centos7-1 nginx-1.12.0]# yum install elinks -y [root@centos7-1 nginx-1.12.0]# elinks http://localhost //使用elinks浏览器
Nginx内置了HTTP_STUB_STATUS状态统计模块,用来反馈当前的Web访问状况。要使用Nginx的状态统计功能,除了启用内建模块之外,还须要修改nginx.conf配置文件,指定访问位置并添加stub_status配置代码。
[root@centos7-1 nginx-1.12.0]# cd /usr/local/nginx/conf [root@centos7-1 conf]# mv nginx.conf nginx.conf.back [root@centos7-1 conf]# grep -v "#" nginx.conf.back > nginx.conf //过滤配置文件#号注释的信息
[root@centos7-1 conf]# vim nginx.conf server { listen 80; server_name localhost; charset utf-8; location / { root html; index index.html index.htm; } //在"server"这里插入的这4行的信息 location ~ /status { //访问位置为/status stub_status on; //打开状态统计功能 access_log off; //关闭此位置的日志记录 } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
新的配置生效后,在浏览器中访问nginx服务器的/status网站位置,能够看到当前的状态统计信息。
systemctl reload nginx.service //从新加载nginx服务 systemctl stop firewalld.service //关闭防火墙 systemctl disable firewalld.service //禁用防火墙
其中,“Active connections”表示当前的活动链接数;而“server accepts handled requests”表示已经处理的链接信息。三个数字依次表示已处理的链接数、成功的TCP握手次数、已处理的请求数。
(1).使用htpasswd生成用户认证文件,若是没有该命令,可以使用yum安装httpd-tools软件包,用法与Apache认证时方式同样,在/usr/local/nginx/目录生成passwd.db文件,用户名是test,密码输入2次。
yum install httpd-tools -y //安装httpd-tools软件包
[root@centos7-1 ~]# htpasswd -c /usr/local/nginx/passwd.db test New password: //设置test用户密码 Re-type new password: Adding password for user test [root@centos7-1 ~]# cat /usr/local/nginx/passwd.db //查看生成的用户认证文件 test:$apr1$WfkC0IdB$sMyjqJzg2tcqcIe1mJ8LI/
(2).修改密码文件的权限为400,将全部者改成nginx,设置nginx的运行用户可以读取。
[root@centos7-1 ~]# chmod 400 /usr/local/nginx/passwd.db [root@centos7-1 ~]# chown nginx /usr/local/nginx/passwd.db [root@centos7-1 ~]# ll -d /usr/local/nginx/passwd.db -r--------. 1 nginx root 43 6月 20 14:45 /usr/local/nginx/passwd.db
(3).修改主配置文件nginx.conf,添加相应认证配置项。
[root@centos7-1 ~]# vim /usr/local/nginx/conf/nginx.conf location / { auth_basic "secret"; //添加认证配置 auth_basic_user_file /usr/local/nginx/passwd.db; root html; index index.html index.htm; }
(4).检测语法、重启服务
[root@centos7-1 ~]# 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@centos7-1 ~]# systemctl restart nginx.service
(5).用浏览器访问网址,检验控制效果。
须要输入用户名和密码进行访问,验证经过才能进行访问。
Nginx基于客户端的访问控制要比Apache的简单,规则以下:
(1).修改主配置文件nginx.conf,添加相应认证配置项。
[root@centos7-1 ~]# vim /usr/local/nginx/conf/nginx.conf location / { deny 192.168.113.132; //客户端IP allow all; root html; index index.html index.htm; }
deny 192.168.113.132表示这个ip地址访问会被拒绝,其余IP客户端正常访问。
(2).重启服务器访问网址,页面已经访问不到。
[root@centos7-1 ~]# systemctl restart nginx.service
要注意的是若是是用域名访问网页,须要配置DNS域名解析服务器,详细步骤参考使用Bind部署DNS域名解析服务器之正向解析