目录
Nginx:
Apache
优缺点比较:
作为web服务器:
编译安装Nginx
编译安装Nginx
nginx服务命令
添加 Nginx 系统服务
配置文件
I/O 事件配置
http 配置
访问状态统计配置
可以写个shell监控脚本
基于授权密码的访问控制
生成用户密码认证文件
配置文件添加密码模块
基于客户端的访问控制
基于域名的nginx 虚拟主机
基于IP地址
基于端口
Nginx是一个高性能的HTTP和反向代理服务器。
是一款轻量级的web服务器/反向代理服务器/电子邮件(IMAP/POP3)代理服务器
单台物理服务器可支持30 000~50 000个并发请求。
Apache是以进程为基础的结构,进程要比线程消耗更多的系统开支,不太适用于多处理器环境,因此,在一个apache Web站点扩容时,通常是增加服务器或扩充群集节点而不是增加处理器。
(1)nginx相对于apache的优点:
轻量级,同样是web服务,比apache占用更少的内存及资源
抗并发,nginx处理请求是异步非阻塞的,而apache是阻塞型的高并发下,nginx能保持低资源低消耗高性能
高度模块化的设计,编写模块相对简单
(2)apache相对于nginx的优点:
Rewrite比nginx的rewrite强大 ###rewrite的主要功能就是实现统一资源定位符(URL)的跳转
模块多,基本想到的都可以找到
少bug,nginx的bug相对较多
超稳定
存在的理由:一般来说,需要性能的web服务,用nginx。若不需要性能只求稳定,就选用apache。
相比apache,nginx使用更少的资源,支持更多的并发连接,体现更高的效率。
Nginx作为负载均衡服务器:nginx既可以在内部直接支持rails和php程序对外进行服务,也可以支持http代理服务器对外进行服务。
Nginx采用C进行编写,不论是系统资源开销还是CPU使用效率都比较好。
作为邮件代理服务器:最早开发这个产品的目的之一也是作为邮件代理服务器。
3、nginx配置简洁, apache较复杂
4、最核心的区别在于:
apache是同步多进程模型,一个连接对应一个进程,nginx是异步的,多个连接可以对应一个进程。
Nginx处理静态文件好,耗费内存少,只适合静态和反向。
Apache在处理动态有优势,
nginx并发性比较好,CPU占用内存低,如果rewrite频繁,选用apache最佳。
总的来说,apache依然是大部分公司的首选。
systemctl stop firewalld #关闭防火墙
systemctl disable firewalld #开机不自启防火墙
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
setenforce 0 #关闭系统防护
yum -y install pcre-devel zlib-devel gcc gcc-c++ make #安装好依赖关系
[root@localhost ~]# cd /opt/
[root@localhost opt]# ls
nginx-1.12.0 nginx-1.12.0.tar.gz rh
[root@localhost opt]# tar zxvf nginx-1.12.2.tar.gz -C /opt/ #解压
cd nginx-1.12.2/
./configure \
--prefix=/usr/local/nginx \ #安装路径
--user=nginx \ #指定用户名
--group=nginx \ #指定用户组
--with-http_stub_status_module #启用此模块支持状态统计
make && make install #编译安装
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ #做软连接 或者加入系统变量 PATH=$PATH:
cd /opt
useradd -M -s /sbin/nologin nginx #创建用户 useradd -M(不创建家目录) -s(不允许登录)
nginx -t #检查配置文件是否配置正确
nginx #启动
cat /usr/local/nginx/logs/nginx.pid #先查看nginx的PID号
kill -3
kill -s QUIT #停止
killall -3 nginx
killall -s QUIT nginx
kill -1 #重载
kill -s HUP
killall -1 nginx
killall -s HUP nginx #日志分隔,重新打开日志文件
kill -USR1 #平滑升级
kill -USR2
#重载
kill -1
kill -s HUP
killall -1 nginx
killall -s HUP nginx
#日志分割,重新打开日志文件
kill -USER1
#平滑升级
kill -USER2
[root@localhost ~]# killall -s HUP nginx ###选项 -s HUP 等同于 -1 重新加载
[root@localhost ~]# killall -s QUIT nginx ###选项 -s QUIT 等同于 -3 停止服务
nginx –v
#查看版本信息
#新版本升级
tar zxvf nginx-1.xx.xxx.tar.gz
cd nginx-1.xx.xxx
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
--with-http_ssl_module
make
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_old
cp objs/nginx /usr/local/nginx/sbin/nginx
make upgrade
#或者先killall nginx ,再 /usr/local/nginx/sbin/nginx
vim /etc/init
#!/bin/bash
#chkconfig: - 99 20
#description:Nginx Service Control Script
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 -s HUP $(cat $PIDF)
;;
*) echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
vim /etc/init
#!/bin/bash
#chkconfig: 35 99 20
#description:Nginx Service Control Script
cmd="/usr/local/nginx/sbin/nginx"
pid="/usr/local/nginx/logs/nginx.pid"
case $1 in
start)
$cmd
;;
stop)
kill -3 `cat $pid`
;;
reload)
kill -1 `cat $pid`
;;
restart)
$0 stop
$0 start
;;
*)
echo "plaese input start,stop,reload,restart"
exit 1
esac
exit 0
vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx ####描述
After=network.target ####描述服务类别
[Service]
Type=forking ###后台运行形式
PIDFile=/usr/local/nginx/logs/nginx.pid ###PID文件位置
ExecStart=/usr/local/nginx/sbin/nginx ###启动服务
ExecReload=/usr/bin/kill -s HUP $MAINPID ###根据PID重载配置
ExecStop=/usr/bin/kill -s QUIT $MAINPID ###根据PID终止进程
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@localhost nginx-1.12.0]# systemctl daemon-reload #重载服务
[root@localhost nginx-1.12.0]# systemctl start nginx.service #启动服务
由各种配置语句组成,不使用特定的界定标记。全局配置部分包括 Nginx 服务的运行 用户、工作进程数、错误日志、PID 存放位置等基本设置
[root@localhost nginx-1.12.0]# cd /usr/local/nginx/conf/
[root@localhost conf]# ls
fastcgi.conf fastcgi_params.default mime.types nginx.conf.default uwsgi_params
fastcgi.conf.default koi-utf mime.types.default scgi_params uwsgi_params.default
fastcgi_params koi-win nginx.conf scgi_params.default win-utf
[root@localhost conf]# vim nginx.conf
events {
use epoll; #epoll 模型,多路复用机制,进程协程+回调实现了高并发能力,以提高性能
worker_connections 1024; #每个进程处理 4096 个连接,受最大文件打开数和cpu的制约
#如提高每个进程的连接数还需执行“ulimit -n 65535”命令临时修改本地每个进程可以同时打开的最大文件数。
#在Linux平台上,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文件数量的限制(这是因为系统为每个TCP连接都要创建一个socket句柄,每个socket句柄同时也是一个文件句柄)。
#可使用ulimit -a命令查看系统允许当前用户进程打开的文件数限制。
[root@localhost conf]# ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 7168
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 7168
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
[root@localhost conf]# ulimit -n 60000
使用“http { }”界定标记,包括访问日志、HTTP 端口、网页目录、默认字符集、连接保 持,以及后面要讲到的虚拟 Web 主机、PHP 解析等一系列设置,其中大部分配置语句都包 含在子界定标记“server { }”内
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;
#此选项允许或禁止使用socketde TCP_CORK的选项(发送数据包前先缓存数据),此选项仅在使用 sendfile时使用
keepalive_timeout 0;
keepalive_timeout 65;
#连接保持超时时间,单位是秒
gzip on;
#gzip模块设置,设置是否开启gzip压缩输出
###web服务的监听设置
server {
listen 80;
#监听地址及端口
server_name localhost;
#站点域名,可以有多个,用空格隔开
#charset koi8-r;
#网页的默认字符集
#access_log logs/host.access.log main;
location / {
root html;
#网站根目录的位置/usr/local/nginx/html
index index.html index.htm;
#默认首页文件名
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
#内部错误反馈页面
location = /50x.html {
#错误页面设置
root html;
}
nginx 内置了 HTTP_STUB_STATUS 状态统计模块,用来反馈当前的 Web 访问情况, 配置编译参数时可添加--with-http_stub_status_module 来启用此模块支持,可以使用命令
可以使用命令/usr/local/nginx/sbin/nginx –V 查看已安装的 Nginx 是否包含 HTTP_STUB_STATUS 模块。
[root@localhost conf]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.12.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
修改nginx.conf 配置文件,指定访问位置并添加 stub_status配置
[root@localhost conf]#cp nginx.conf nginx.conf.bak #备份一下
[root@localhost conf]#vim nginx.conf
server {
listen 80;
server_name www.kgc.com;
#charset utf-8;
#access_log logs/host.access.log main;
# location / {
# root html;
# index index.html index.htm;
##添加stub_status配置
location /status { #访问位置为/status : www.kgc.com/status
stub_status on; #打开状态统计功能
access_log off; #关闭此位置的日志功能
}
#!/bin/bash
a=$(curl 192.168.159.102/status)
b=$(echo $a|awk '{print $3}')
if [ $b -ge 1000 ]
then
echo "连接数过高"
else
echo "运行良好"
fi
yum install -y httpd-tools #安装密码管理工具 需外部yum仓
[root@localhost nginx-1.12.0]# htpasswd -c /usr/local/nginx/passwd.db zhangsan
创建zhangsan用户 -c生成密码文件 没有就创建 有就覆盖
New password:
Re-type new password:
Adding password for user zhangsan
[root@localhost nginx-1.12.0]# cat /usr/local/nginx/passwd.db #看看密码是否生成
zhangsan:$apr1$b4oumL8q$SPWdB5Rvvu7/fX.krpQsq.
[root@localhost nginx-1.12.0]# vim /usr/local/nginx/conf/nginx.conf
#编辑配置文件
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
# location / {
# root html;
# index index.html index.htm;
# }
##添加认证配置 可以理解为添加密码模块##
location / {
root html;
index index.html index.htm;
auth_basic "secret"; #主页加密码模块
auth_basic_user_file /usr/local/nginx/passwd.db;
}
#error_page 404 /404.html;
[root@localhost 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
#提示配置文件OK
[root@localhost nginx-1.12.0]# chown nginx /usr/local/nginx/passwd.db
#更改属主
[root@localhost nginx-1.12.0]# ll /usr/local/nginx/passwd.db
-rw-r--r--. 1 nginx root 47 11月 10 10:20 /usr/local/nginx/passwd.db
[root@localhost nginx-1.12.0]# chmod 400 /usr/local/nginx/passwd.db
#更改权限 只有可读权限
[root@localhost nginx-1.12.0]# systemctl restart nginx.service
#重启服务
1、基于客户端的访问控制简介
基于客户端的访问控制是通过客户端 IP 地址,决定是否允许对页面访问。Nginx 基于 客户端的访问控制要比 Apache 简单,规则如下:
1)deny IP/IP 段:拒绝某个 IP 或 IP 段的客户端访问。
2)allow IP/IP 段:允许某个 IP 或 IP 段的客户端访问。
3)规则从上往下执行,如匹配则停止,不再往下匹配。
##添加控制规则##
location / {
#auth_basic "secret";
#auth_basic_user_file /usr/local/nginx/passwd.db;
deny 192.168.91.100; #拒绝访问的客户端 IP
allow all;
root html;
index index.html index.htm;
}
利用虚拟主机,不用为每个要运行的网站提供一台单独的 Nginx 服务器或单独运行一 组 Nginx 进程,虚拟主机提供了在同一台服务器,同一组 Nginx 进程上运行多个网站的功 能。跟 Apache 一样,Nginx 也可以配置多种类型的虚拟主机,分别是基于 IP 的虚拟主机、 基于域名的虚拟主机、基于端口的虚拟主机。 使用 Nginx 搭建虚拟主机服务器时,每个虚拟 Web 站点拥有独立的“server{}”配置段, 各自监听的 IP 地址、端口号可以单独指定,当然网站名称也是不同的。
[root@localhost var]#mkdir -p www/html/{kgc,accp}
[root@localhost html]#echo "this is kgc web" > kgc/index.html
[root@localhost html]#echo "this is accp web" > accp/index.html
[root@localhost html]#cat kgc/index.html
this is kgc web
[root@localhost html]#cat accp/index.html
this is accp web
gzip on;
server {
listen 80;
server_name www.kgc.com;
charset utf-8;
access_log logs/accp.access.log;
location / {
root /var/www/html/kgc;
index index.html index.htm;
}
#error_page 404 /404.html;
#
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name www.accp.com;
charset utf-8;
access_log logs/accp.access.log;
location / {
root /var/www/html/accp;
index index.html index.htm;
}
#error_page 404 /404.html;
#
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
[root@localhost html]#vim /usr/local/nginx/conf/nginx.conf
server {
listen 192.168.159.102:80;
server_name www.kgc.com;
server {
listen 192.168.159.103:80;
server_name www.kgc.com;
[root@localhost html]#vim /usr/local/nginx/conf/nginx.conf
server {
listen 192.168.91.102:80;
server_name www.kgc.com;
server {
listen 192.168.91.102:8080;
server_name www.kgc.com;