nginx应用与配置

一、Nginx简介

1.1 概述

1.Nginx是一款高性能、轻量级Web服务软件
2.稳定性高
3.系统资源消耗低
4.对HTTP并发连接的处理能力高,单台物理服务器可支持30000-50000个并发请求

1.2 Nginx与Apache的差异

Nginx Apache
基于事件 基于流程
所有请求由一个线程处理 单个线程处理单个请求
避免子进程 基于子进程
在内存消耗和连接方面更好 一般
性能和可伸缩性不依赖于硬件 依赖于CPU和内存等硬件
支持热部署 不支持热部署
对于静态文件处理更具效率 一般
在反向代理场景更具优势 一般

二、编译安装Nginx服务

2.1 关闭防火墙,将安装nginx所需软件包传到/opt目录下

systemctl stop firewalld
systemctl  disable firewalld
setenforce 0



2.2 安装依赖包

yum install -y pcre-devel zlib-devel gcc gcc-c++ make    #Nginx的配置及运行需要pcre、zlib等软件包的支持,因此需要安装这些安装的开发包,以便提供相应的库和头文件 

2.3 创建运行用户、组

useradd -M -s /sbin/nologin nginx       #Nginx服务程序默认以nobody身份运行,建议为其创建专门的用户账号,以便更准确地控制其访问权限

2.4 编译安装nginx

cd /opt
tar zxvf nginx-1.12.0.tar.gz

cd nginx-1.12.0/
./configure \
> --prefix=/usr/local/nginx \      #指定nginx的安装路径
> --user=nginx \                   #指定用户名
> --group=nginx \                  #指定组名
> --with-http_stub_status_module   #启用 http_stub_status_module模块以变持状态线计
 
make && make install
 
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/  #让系统识别nginx的操作命令



2.5 检查、启动、重启、停止nginx服务

1.nginx -t                #检查配置文件是否配置正确
2.nginx                   #启动
3.停止
 1)cat /usr/local/nginx/logs/nginx.pid #先查看nginx的PID号
 2)kill -3 <PID号>       #直接杀死
 3)kill -s QUIT <PID号>  #优雅的杀死
 4)killall -3 nginx
 5)killall -s QUIT nginx    
4.重载
kill -1 <PID号>
kill -s HUP <PID号>
killall -1 nginx
killall -s HUP nginx
5.日志分割,重新打开日志文件
kill -USR1 <PID号>
6.平滑升级
kill -USR2 <PID号>
7.新版本升级∶
1)tar -zxvf nginx-1.xx.xX. tar.gz
2)cd nginx-1.xx. xx
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module \
--with-http_ssl_module
3)make
4)mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_old
5)cp objs/nginx /usr/local/nginx/sbin/nginx
6)make upgrade        #或者先 killall nginx ,再/usr/local/nginx/sbin/nginx




2.6 添加nginx系统服务

方法一:使用shell脚本

vim /etc/init.d/nginx           #创建脚本文件内容如下:
 
#!/bin/bash
#chkconfig: - 99 20
#description:Nginx Server Control Script
COM="/usr/local/nginx/sbin/nginx"
PID="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$COM
;; 
stop)
kill -s QUIT $(cat $PID)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PID)
;; 
*)
echo "Usage:$0 {start|stop|restart|reload}"
exit 1 
esac
exit 0
 
chmod +x /etc/init.d/nginx
chkconfig --add nginx
systemctl daemon-reload         #磁盘上的ngin服务更改,运行systemctl daemon-reload重新加载单元
systemctl start nginx
systemctl stop nginx


方法二:

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 SMAINPID
ExecrStop=/bin/kill-s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
 
chmod 754 /lib/systemd/ system/nginx.service
systemctl start nginx.service
systemctl enable nginx.service





注:
1.【Unit】∶服务的说明 Description∶ 描述服务 After∶依赖, 当依赖的服务启动之后再启动自定义的服务
2.【Service】:服务运行参数的设置
1)Type=forking是后台运行的形式,使用此启动类型应同时指定PIDFile=,以便systemd能够跟踪服务的主进程
2)ExecStart为服务的具体运行命令 ExecReload为重启命令 ExecStop为停止命令
3)PrivateTmp=True表示给服务分配独立的临时空间 注意∶ 启动、重启、停止命令全部要求使用绝对路径
3.【Install】:服务安装的相关设置,可设置为多用户

2.7 测试Nginx服务

三、认识nginx服务的主配置文件

vim /usr/local/nginx/conf/nginx.conf

3.1 全局配置

1.#user nobody                 #运行用户,若编译时未指定则默认为nobody
2.worker_processes 1           #工作进程数量,可配置成服务器内核数 * 2,如果网站访问量不大,一般设为1就够用了
3.#error_log logs/error.log    #错误日志文件的位置
4.#pid logs/nginx.pid          #PID文件的位置

3.2 I/O事件配置

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

1.如提高每个进程的连接数还需执行"ulimit -n 65535"命令临时修改本地每个进程可以同时打开的最大文件数
2.在Linux平台上, 在进行高并发TCP连接处理时, 最高的并发数量都要受到系统对用户 单—一进程同时可打开文件数量的限制(这是因为系统为每个TCP连接都要创建一个socket句柄,每个socket句柄同时也是一个文件句柄)
3.可使用ulimit -a命令查看系统允许当前用户进程打开的文件数限制

3.3 HTTP配置

http {
1.include mime.types;                      #文件扩展名与文件类型映射表
2.default_type application/octet-stream;   #默认文件类型
3.日志格式设定
 1)#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
 2)# '$status $body_bytes_sent "$http_referer" '
 3)# '"$http_user_agent" "$http_x_forwarded_for"';
 
 4)#access_log logs/access.log main;       #日志格式设定
 
4.sendfile on;                             #支持文件发送(下载)
5.#tcp_nopush on;                          #此选项允许或禁止使用socket的TCP_CORK的选项(发送数据包前先缓存数据),此选项仅在使用sendfile的时候使用
6.#keepalive_timeout 0;
keepalive_timeout 65;                      #连接保持超时时间,单位是秒
7.#gzip on;                                #gzip模块设置,设置是否开启gzip压缩输出 
8.server {
 1)listen 80;                               #监听地址及端口
 2)server_name www.gxd.com;                 #站点域名,可以有多个,用空格隔开
 3)#charset utf-8;                          #网页的默认字符集
 4)#access_log logs/host.access.log main;
 location / {                               #根目录配置
 root html;                                 #网站根目录的位置/usr/local/nginx/html
 index index.html index.htm;                #默认首页文件名
 }
 5)#error_page 404 /404.html;
 6)# redirect server error pages to the static page /50x.html
 7)#
 error_page 500 502 503 504 /50x.html;      #内部错误的反馈页面
 location = /50x.html {                     #错误页面配置
 root html;
}
补充
1.日志格式设定∶
$remote_addr与$http x forwarded for用以记录客户端的ip地址;
$remote user∶ 用来记录客户端用户名称;
$time local∶ 用来记录访问时间与时区;$request∶用来记录请求的url与http协议;
$status∶ 用来记录请求状态;成功是200,
$body bytes sent ∶ 记录发送给客户端文件主体内容大小;
$http referer∶ 用来记录从哪个页面链接访问过来的;
$http user agent∶记录客户浏览器的相关信息;
2.通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过Sremote_add拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中,可以增加x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。
3.location常见配置指令, root、alias、proxy_ pass
 1)root (根路径配置)∶ 请求ww.gxd.com/test/1.jpg,会返回文件/usr/local/nginx/html/test/1.jpg
 2)alias (别名配置)∶请求www.gxd.com/test/1.jpg,会返回文件/usr/local/nginx/html/1.jpg
 3)proxy_pass (反向代理配置)∶
 proxy_pass http://127.0.0.1:8080/; ------------- 会转发请求到http∶//127.0.0.1∶8080/1.jpg
 proxy_pass http://127.0.0.1:8080; --------------会转发请求到http∶//127.0.0.1∶8080/test/1.jpg

四、访问状态统计配置

4.1 检查是否安装STUB_STATUS模块

nginx -V

4.2 修改nginx.conf配置文件,指定访问位置并添加stub_status配置

1.cd /usr/local/nginx/conf
cp nginx.conf nginx.conf.bak
2.vim /usr/local/nginx/conf/nginx.conf
......
http {
......
	server {
		listen 80;
		server_name www.gxd.com;
		charset utf-8;
		location / {
			root html;
			index index.html index.php;
		}
		##添加 stub_status 配置##
		location /status { 					#访问位置为/status
			stub_status on; 				#打开状态统计功能
			access_log off; 				#关闭此位置的日志记录
		}
	}
}
3.systemctl restart nginx.service             #重启服务


4.3 浏览器测试

http://192.168.80.11/status    


注:
1.Active connections ∶ 表示当前的活动连接数;
2.server accepts handled requests∶表示已经处理的连接信息,三个数字依次表示已处理的连接数、成功的TCP握手次数已处理的请求数。
3.可curl -s http∶//192.168.80.11/status 结合 awk与if 语句进行性能监控

五、基于授权的访问控制

5.1 生成用户密码认证文件

yum -y install httpd-tools
htpasswd -c /usr/local/nginx/passwd.db gxd
chown nginx /usr/local/nginx/passwd.db
chmod 400 /usr/local/nginx/passwd.db 


5.2 修改主配置文件相应的目录,添加认证配置

1.vim /usr/local/nginx/conf/nginx.conf
......
	server {
		location / {
			......
			##添加认证配置##
			auth_basic "secret";
			auth_basic_user_file /usr/local/ngin/passwd.db;
		}
	}


2.nginx -t                            #验证配置
3.systemctl restart nginx             #重启服务


5.3 浏览器测试

http://192.168.80.11或www.gxd.com

六、基于客户端访问控制

6.1 配置要求

访问控制规则如下:
deny IP/IP段:拒绝某个IP或IP段的客户端访问        #黑名单
allow IP/IP段:允许某个IP或IP段的客户端访问       #白名单
规则从上往下执行,如果匹配到则停止,不会再往下继续匹配
 
vim /usr/local/nginx/conf/nginx.conf
......
	server {
		location / {
			......
			##添加控制规则##
			deny 192.168.80.12; 					#拒绝访问的客户端 IP
			allow all;								#允许其它IP客户端访问
		}
	}

systemctl restart nginx

6.2 浏览器测试

1.主机ip:192.168.80.12测试


2.其他主机测试

七、构建Nginx虚拟web主机

7.1 基于域名的虚拟主机

7.1.1 为虚拟主机提供域名解析

1.部署DNS域名解析服务器
2.echo "192.168.80.11 www.aaa.com www.bbb.com" >> /etc/hosts

7.1.2 为虚拟主机准备网页文档

mkdir -p /var/www/html/aaa
mkdir -p /var/www/html/bbb
echo "<h1>wuyifanaaa</h1>" > /var/www/html/aaa/index.html
echo "<h1>fanyiwubbb</h1>" > /var/www/html/bbb/index.html

7.1.3 添加虚拟主机配置

vim /usr/local/nginx/conf/nginx.conf
......
http {
......
	server {
		listen 80;
		server_name www.aaa.com;					#设置域名www.aaa.com
		charset utf-8;
		access_log logs/www.aaa.access.log; 
		location / {
			root /var/www/html/aaa;				    #设置www.aaa.com 的工作目录
			index index.html index.php;
		}
		error_page 500 502 503 504 /50x.html;
		location = 50x.html{
			root html;
		}
	}
	
	server {
		listen 80;
		server_name www.bbb.com;					#设置域名www.bbb.com
		charset utf-8;
		access_log logs/www.bbb.access.log; 
		location / {
			root /var/www/html/bbb;                 #设置www.bbb.com 的工作目录
			index index.html index.php;
		}
		error_page 500 502 503 504 /50x.html;
		location = 50x.html{
			root html;
		}
	}	
}

7.1.4 验证配置并重启服务

nginx -t
systemctl restart nginx

7.1.5 浏览器测试


7.2 基于IP地址的虚拟主机

7.2.1 添加一张网卡

ifconfig ens33:0 192.168.80.110/24   #这边添加虚拟网卡,也可添加物理网卡

7.2.2 添加虚拟主机配置

vim /usr/local/nginx/conf/nginx.conf
......
http {
......
	server {
		listen 192.168.80.11:80;                    #设置80.11ip
		server_name www.aaa.com;					#设置域名www.aaa.com
		charset utf-8;
		access_log logs/www.aaa.access.log; 
		location / {
			root /var/www/html/aaa;				    #设置www.aaa.com 的工作目录
			index index.html index.php;
		}
		error_page 500 502 503 504 /50x.html;
		location = 50x.html{
			root html;
		}
	}
	
	server {
		listen 192.168.80.110:80;                   #设置80.110ip
		server_name www.bbb.com;					#设置域名www.bbb.com
		charset utf-8;
		access_log logs/www.bbb.access.log; 
		location / {
			root /var/www/html/bbb;                 #设置www.bbb.com 的工作目录
			index index.html index.php;
		}
		error_page 500 502 503 504 /50x.html;
		location = 50x.html{
			root html;
		}
	}	
}

7.2.3 验证配置并重启服务

nginx -t
systemctl restart nginx

7.2.4 浏览器测试


7.3 基于端口的虚拟主机

7.3.1 添加虚拟主机配置

vim /usr/local/nginx/conf/nginx.conf
......
http {
......
	server {
		listen 192.168.80.11:888;                   #设置888端口
		server_name www.aaa.com;					#设置域名www.aaa.com
		charset utf-8;
		access_log logs/www.aaa.access.log; 
		location / {
			root /var/www/html/aaa;				    #设置www.aaa.com 的工作目录
			index index.html index.php;
		}
		error_page 500 502 503 504 /50x.html;
		location = 50x.html{
			root html;
		}
	}
	
	server {
		listen 192.168.80.11:999;                   #设置999端口
		server_name www.bbb.com;					#设置域名www.bbb.com
		charset utf-8;
		access_log logs/www.bbb.access.log; 
		location / {
			root /var/www/html/bbb;                 #设置www.bbb.com 的工作目录
			index index.html index.php;
		}
		error_page 500 502 503 504 /50x.html;
		location = 50x.html{
			root html;
		}
	}	
}

7.3.2 验证配置并重启服务

nginx -t
systemctl restart nginx

7.3.3 浏览器测试


posted @ 2021-08-11 11:33  落寞1111  阅读(76)  评论(0编辑  收藏  举报