Nginx服务-基础与控制

目录

前言

一、Nginx服务基础

1.关闭防火墙相关服务

2.安装依赖包

3.编译安装Nginx

4.检查、启动、停止nginx服务

5.添加Nginx系统服务

二、Nginx配置文件

1.备份

2.修改域名配置

3.配置本地映射

4.全局配置

5.I/O事件配置

6.HTTP配置

三、访问状态统计 

四、访问控制

1.生成用户密码认证文件

2.修改权限

3.修改主配置文件

4.检测语法、重启服务

5.基于客户端访问控制

总结


前言

Nginx专为性能优化而开发,其最知名的优点是它的稳定性和低系统资源消耗,以及对HTTP并发连续的高处理能力(单台物理服务服务器可支持30000~50000个并发请求)。正因为如此,大量提供社交网络、新闻资讯、电子商务及虚拟机等服务的企业纷纷选择Nginx来提供Web服务。

一、Nginx服务基础

Nginx安装文件可以从官方网站http://www.nginx.org/下载。

1.关闭防火墙相关服务

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# setenforce 0

[root@localhost ~]# vim /etc/resolv.conf
nameserver 114.114.114.114

2.安装依赖包

Nginx的配置及运行需要pcre、zlib等软件包的支持,因此应预先安装这些软件的开发包,以便提供相应的库和头文件,确保Nginx的安装顺利完成。

[root@localhost ~]# yum install -y gcc gcc-c++ pcre-devel zlib-devel make

3.编译安装Nginx

[root@localhost ~]# tar zxvf nginx-1.12.2.tar.gz -C /opt/

[root@localhost ~]# cd /opt/nginx-1.12.2/
[root@localhost nginx-1.12.2]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module

[root@localhost nginx-1.12.2]# make && make install

为了使Nginx服务器运行更加方便,可以为主程序nginx创建软链接文件,以便直接调用Nginx的服务

[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/

创建一个名为nginx的用户,不建立宿主文件夹,也禁止登录到Shell环境

[root@localhost ~]# useradd -M -s /sbin/nologin nginx

4.检查、启动、停止nginx服务

[root@localhost ~]# 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@localhost ~]# nginx     #表示直接启动Nginx
nginx -t								   #检查配置文件是否配置正确
nginx									   #启动		
cat /usr/local/nginx/logs/nginx.pid		   #先查看nginx的PID号
kill -3 <PID号>
kill -s QUIT <PID号>					   #停止
killall -3 nginx
killall -s QUIT nginx
kill -1 <PID号>						       #重载
kill -s HUP <PID号>
killall -1 nginx
killall -s HUP nginx					   #日志分隔,重新打开日志文件
kill -USR1 <PID号>				           #平滑升级
kill -USR2 <PID号>
[root@localhost ~]# netstat -natp | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      12687/nginx: master 
[root@localhost ~]# kill -3 12687      #先要杀死进程

5.添加Nginx系统服务

[root@localhost ~]# vim /lib/systemd/system/nginx.service

[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecrReload=/bin/kill -s HUP $MAINPID
ExecrStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@localhost ~]# chmod 754 /lib/systemd/system/nginx.service	 #赋权,除了root以外的用户都不能修改
[root@localhost ~]# systemctl start nginx.service
[root@localhost ~]# systemctl enable nginx.service

二、Nginx配置文件

在Nginx服务器中主配置文件在/usr/local/nginx/conf/nginx.conf中。

1.备份

[root@localhost ~]# cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak

2.修改域名配置

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

...
server_name www.zch.com;
...

3.配置本地映射

[root@localhost ~]# vim /etc/hosts
...
192.168.196.151 www.zch.com

4.全局配置

#user nobody; 					#运行用户,若编译时未指定则默认为 nobody
worker_processes 1; 			#工作进程数量,可配置成服务器内核数 * 2
#error_log logs/error.log; 		#错误日志文件的位置
#pid logs/nginx.pid; 			#PID 文件的位置

5.I/O事件配置

events {
    use epoll; 					#使用 epoll 模型,2.6及以上版本的系统内核,建议使用epoll模型以提高性能
    worker_connections 1024; 	#每个进程处理1024个连接(默认)

6.HTTP配置

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;                     ##支持文件发送(下载)
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;                  ##连接保持超时
    #gzip  on;                              ##gzip模块设置,设置是否开启gzip压缩输出
server {                                    ##Web 服务的监听配置                              
	listen 80;                              ##监听地址及端口   
	server_name www.zch.com;                ##站点域名,可以有多个,用空格隔开
	charset utf-8;                          ##网页的默认字符集
	location / {                            ##根目录配置
		root html;                          ##网站根目录的位置/usr/local/nginx/html
		index index.html index.htm;         ##默认首页文件名
	}
	error_page 500 502 503 504 /50x.html;   ##内部错误的反馈页面
	location = /50x.html {                  ##错误页面配置
		root html;
	}
}
}

三、访问状态统计 

使用命令/usr/local/nginx/sbin/nginx -V 查看已安装的 Nginx 是否包含 HTTP_STUB_STATUS 模块。

要使用Nginx的状态统计功能,除了启用内建模块以外,还需要修改nginx.conf配置文件,指定访问位置并添加stub_status配置代码

vim /usr/local/nginx/conf/nginx.conf
......
http {
......
	server {
		listen 80;
		server_name www.zch.com;
		charset utf-8;
		location / {
			root html;
			index index.html index.php;
		}
		##添加 stub_status 配置##
		location /status { 					#访问位置为/status
			stub_status on; 				#打开状态统计功能
			access_log off; 				#关闭此位置的日志记录
		}
	}
}
[root@localhost ~]# systemctl restart nginx

四、访问控制

基于授权的访问控制,Nginx与Apache一样,可以实现基于用户授权的访问控制,当客户端想要访问相应的网站或者目录时,要求用户输入用户名和密码才能正常访问配置步骤与Apache基本一致。

1.生成用户密码认证文件

[root@localhost ~]# yum install -y httpd-tools    #安装所需的认证的htpasswd工具的软件包

[root@localhost ~]# htpasswd -c /usr/local/nginx/passwd.db zhangsan
New password: 
Re-type new password: 
Adding password for user zhangsan

2.修改权限

[root@localhost ~]# chmod 400 /usr/local/nginx/passwd.db      #修改密码文件的权限为400,设置只能读的权限
[root@localhost ~]# chown nginx /usr/local/nginx/passwd.db    #将所有者改为nginx
[root@localhost ~]# ll /usr/local/nginx/
-r--------. 1 nginx root   47 10月  7 00:10 passw.db

3.修改主配置文件

[root@localhost ~]# 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@localhost ~]# 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@localhost ~]# systemctl restart nginx.service 

5.基于客户端访问控制

deny IP/IP 段:拒绝某个 IP 或 IP 段的客户端访问。
allow IP/IP 段:允许某个 IP 或 IP 段的客户端访问。
规则从上往下执行,如匹配则停止,不再往下匹配。

vim /usr/local/nginx/conf/nginx.conf
......
	server {
		location / {
			......
			##添加控制规则##
			deny 192.168.196.151; 					#拒绝访问的客户端 IP
			allow all;								#允许其它IP客户端访问
		}
	}

[root@localhost ~]# systemctl restart nginx

总结

LNMP平台的N代表Nginx,是一款高性能的轻量级Web服务器软件,在稳定性、并发响应方面变现出色。

Nginx内建的访问统计功能由stub_status模块提供,需要在编译时启用“--with-http_stub_status_module”选项。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值