网站信息化建设什么意思,新版wordpress谷歌字体,微信服务号开发,网站域名过期怎么做文章目录一、Nginx概述1.1、Nginx的特点1.2、Nginx编译安装1.3、Nginx运行控制1.4、Nginx和Apache的区别二、编译安装Nginx服务的操作步骤2.1、关闭防火墙#xff0c;将安装nginx所需软件包传到/opt目录下2.2、安装依赖包2.3、创建运行用户、组#xff08;Nginx 服务程序默认…
文章目录一、Nginx概述1.1、Nginx的特点1.2、Nginx编译安装1.3、Nginx运行控制1.4、Nginx和Apache的区别二、编译安装Nginx服务的操作步骤2.1、关闭防火墙将安装nginx所需软件包传到/opt目录下2.2、安装依赖包2.3、创建运行用户、组Nginx 服务程序默认以 nobody 身份运行建议为其创建专门的用户账号以便更准确地控制其访问权限2.4、编译安装nginx2.5、检查、启动、重启、停止 nginx服务2.6、添加nginx系统服务三、实例操作编译安装Nginx服务3.1、关闭防火墙将安装nginx所需软件包传到/opt目录下3.2、安装依赖包3.3、创建运行用户、组3.4 、编译安装Nginx3.5、检查、启动、重启、停止Nginx服务3.6、添加nginx 系统服务四、认识Nginx服务的主配置文件4.1、全局配置4.2、I/O事件配置五、访问状态统计配置5.1、访问状态统计配置的操作步骤5.2、实例操作访问状态统计配置六、基于授权的访问控制6.1、基于授权的访问控制的操作步骤6.2、实例操作基于授权的访问控制6.3、基于客户端访问控制一、Nginx概述
1.1、Nginx的特点
一款高性能、轻量级web服务
• 稳定性高
• 系统资源消耗低高
• 对HTTP并发连接的处理能力
单台物理服务器可支持30000~50000个并发请求
1.2、Nginx编译安装
• 安装支持软件
• 创建运行用户、组
• 编译安装Nginx
1.3、Nginx运行控制
• 检查配置文件
• 启动、重载配置、停止Nginx
1.4、Nginx和Apache的区别
4.1 nginx相对于apache的优点∶ 轻量级同样起web服务,比apache占用更少的内存及资源
抗并发nginx处理请求是异步非阻塞的而apache是阻塞型的在高并发下nginx能保持低资源低消耗高性能
高度模块化的设计编写模块相对简单
4.2 apache相对于nginx的优点∶ Rewrite比nginx的rewrite强大 rewrite的主要功能就是实现统一资源定位符URL的跳转
模块多基本想到的都可以找到
少bug nginx的bug相对较多
超稳定
总结一般来说需要性能的web服务用nginx。 若不需要性能只求稳定就选用apache
二、编译安装Nginx服务的操作步骤
2.1、关闭防火墙将安装nginx所需软件包传到/opt目录下
systemctl stop firewalld
systemctl disable firewalld
setenforce 0# 将压缩包传入到/opt目录下
nginx-1.12.0.tar.gz2.2、安装依赖包
nginx的配置及运行需要pcre、zlib等软件包的支持因此需要安装这些安装的开发包以便提供相应的库和头文件
yum install -y pcre-devel zlib-devel gcc gcc-c make2.3、创建运行用户、组Nginx 服务程序默认以 nobody 身份运行建议为其创建专门的用户账号以便更准确地控制其访问权限
useradd -M -s /sbin/nologin nginx2.4、编译安装nginx
cd /opt
tar zxvf nginx-1.12.0.tar.gz./configure \--prefix/usr/local/nginx \ #指定nginx的安装路径--usernginx \ #指定用户名--groupnginx \ #指定组名--with-http_stub_status_module #启用 http_stub_status_module模块以变持状态线计make make installln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ ##让系统识别nginx的操作命令2.5、检查、启动、重启、停止 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号新版本升级∶
tar -zxvf nginx-1.xx.xX. tar.gz
cd nginx-1.xx. xx
./configure \
--prefix/usr/local/nginx \
--usernginx \
--groupnginx \
--with-http_stub_status_module \
--with-http_ssl_modulemake
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/nginx2.6、添加nginx系统服务
方法一使用脚本
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 1esac
exit 0chmod 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]
Descriptionnginx
Afternetwork.target
[Service]
Typeforking
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
PrivateTmptrue
[Install]
WantedBymulti-user.targetchmod 754 /lib/systemd/ system/nginx.service
systemctl start nginx.service
systemctl enable nginx.service【Unit】∶服务的说明 Description∶ 描述服务 After∶依赖 当依赖的服务启动之后再启动自定义的服务
【Service】服务运行参数的设置 Typeforking是后台运行的形式使用此启动类型应同时指定PIDFile以便systemd能够跟踪服务的主进程。 ExecStart为服务的具体运行命令 ExecReload为重启命令 ExecStop为停止命令 PrivateTmpTrue表示给服务分配独立的临时空间 注意∶ 启动、重启、停止命令全部要求使用绝对路径
【Install】服务安装的相关设置可设置为多用户
三、实例操作编译安装Nginx服务
3.1、关闭防火墙将安装nginx所需软件包传到/opt目录下
[rootcm ~]# cd /opt
[rootcm opt]# rz -E
rz waiting to receive.
[rootcm opt]# ls
nginx-1.12.2.tar.gz rh
[rootcm opt]# systemctl stop firewalld
[rootcm opt]# systemctl disable firewalld
[rootcm opt]# setenforce 0
setenforce: SELinux is disabled
[rootcm opt]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemonLoaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)Active: inactive (dead)Docs: man:firewalld(1)3.2、安装依赖包
[rootcm opt]# yum -y install pcre-devel zlib-devel gcc gcc-c make3.3、创建运行用户、组
[rootcm opt]# useradd -M -s /sbin/nologin nginx
[rootcm opt]# id nginxuid1000(nginx) gid1000(nginx) 组1000(nginx)3.4 、编译安装Nginx
3.4.1 解压Nginx软件包
[rootcm opt]# ls
nginx-1.12.2.tar.gz rh
[rootcm opt]# tar zxvf nginx-1.12.2.tar.gz3.4.2 安装相关模块
[rootcm opt]# ls
nginx-1.12.2 nginx-1.12.2.tar.gz rh
[rootcm opt]# cd nginx-1.12.2/
[rootcm nginx-1.12.2]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[rootcm nginx-1.12.2]# ./configure \--prefix/usr/local/nginx \--usernginx \--groupnginx \--with-http_stub_status_module 3.4.3 编译安装
[rootcm nginx-1.12.2]# make -j2 make install3.4.4 将Nginx链接到/user/local/sbin下
[rootcm nginx-1.12.2]# cd /usr/local/nginx/sbin/
[rootcm sbin]# ls
nginx
[rootcm sbin]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/3.5、检查、启动、重启、停止Nginx服务
3.5.1 检查和启动
[rootcm sbin]# 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
[rootcm sbin]# netstat -natp | grep :80
[rootcm sbin]# nginx
[rootcm sbin]# netstat -natp | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 13520/nginx: master 3.5.2 停止、重启nginx服务
5.2.1 停止nginx服务
[rootcm sbin]# cd /usr/local/nginx/logs/
[rootcm logs]# ls
access.log error.log nginx.pid
[rootcm logs]# cat nginx.pid
13520
[rootcm logs]# pgrep nginx -l
13520 nginx
13521 nginx
[rootcm logs]# lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 13520 root 6u IPv4 76974 0t0 TCP *:http (LISTEN)
nginx 13521 nginx 6u IPv4 76974 0t0 TCP *:http (LISTEN)
[rootcm logs]# ps -ef | grep nginx
root 13520 1 0 20:55 ? 00:00:00 nginx: master process nginx
nginx 13521 13520 0 20:55 ? 00:00:00 nginx: worker process
root 13618 8613 0 21:01 pts/0 00:00:00 grep --colorauto nginx
[rootcm logs]# ss -natp | grep nginx
LISTEN 0 128 *:80 *:* users:((nginx,pid13521,fd6),(nginx,pid13520,fd6))
[rootcm logs]# netstat -natp | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 13520/nginx: master
[rootcm logs]# kill -3 13520
[rootcm logs]# netstat -natp | grep :80
[rootcm logs]# nginx
[rootcm logs]# netstat -natp | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 13650/nginx: master
[rootcm logs]# kill -s QUIT 13650
[rootcm logs]# netstat -natp | grep :80
[rootcm logs]# nginx
[rootcm logs]# netstat -natp | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 13665/nginx: master
[rootcm logs]# killall -3 nginx
[rootcm logs]# netstat -natp | grep :80
[rootcm logs]# nginx
[rootcm logs]# netstat -natp | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 13683/nginx: master
[rootcm logs]# killall -s QUIT nginx
[rootcm logs]# netstat -natp | grep :805.2.2 重载服务
[rootcm logs]# netstat -natp | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 13772/nginx: master
[rootcm logs]# kill -1 13772
[rootcm logs]# netstat -natp | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 13772/nginx: master
[rootcm logs]# kill -s HUP 13772
[rootcm logs]# netstat -natp | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 13772/nginx: master
[rootcm logs]# killall -1 nginx
[rootcm logs]# killall -s HUP nginx
[rootcm logs]# netstat -natp | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 13772/nginx: master3.6、添加nginx 系统服务
6.1 创建脚本文件
[rootcm ~]# vim /etc/init.d/nginx#!/bin/bash
#chkconfig 35 99 20
#Ngnix server scriptCOM/usr/local/nginx/sbin/nginx
PID/usr/local/nginx/logs/nginx.pidcase $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 06.2 赋予权限 并添加到系统服务内
[rootcm ~]# chmod x /etc/init.d/nginx
[rootcm ~]# chkconfig --add nginx6.3 nginx启动服务服务测试
[rootcm ~]# systemctl stop nginx
Warning: nginx.service changed on disk. Run systemctl daemon-reload to reload units.
[rootcm ~]# systemctl daemon-reload
[rootcm ~]# systemctl stop nginx
[rootcm ~]# netstat -natp | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 13772/nginx: master
[rootcm ~]# kill -3 13772
[rootcm ~]# systemctl start nginx
[rootcm ~]# netstat -natp | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 15369/nginx: master
[rootcm ~]# systemctl stop nginx
[rootcm ~]# netstat -natp | grep :80四、认识Nginx服务的主配置文件
vim /usr/local/nginx/conf/nginx.conf4.1、全局配置
#user nobody; #运行用户若编译时未指定则默认为 nobody
worker_processes 1; #工作进程数量可配置成服务器内核数 * 2如果网站访问量不大一般设为1就够用了
#error_log logs/error.log; #错误日志文件的位置
#pid logs/nginx.pid; #PID 文件的位置4.2、I/O事件配置
events {use epoll; #使用epoll模型2.6及以上版本的系统内核建议使用epoll模型以提高性能worker_connections 4096; #每个进程处理 4096个连接
}如提高每个进程的连接数还需执行ulimit -n 65535命令临时修改本地每个进程可以同时打开的最大文件数。 在Linux平台上 在进行高并发TCP连接处理时 最高的并发数量都要受到系统对用户 单—一进程同时可打开文件数量的限制这是因为系统为每个TCP连接都要创建一个socket句柄每个socket句柄同时也是一个文件句柄。 可使用ulimit -a命令查看系统允许当前用户进程打开的文件数限制。 3、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; ##支持文件发送下载##此选项允许或禁止使用socket的TCP cORK的选项发送数据包前先缓存数据此选项仅在使用sendfile的时候使用#tcp_nopush on;##连接保持超时时间单位是秒#keepalive_timeout 0;keepalive_timeout 65;#gzip on; ##gzip模块设置设置是否开启gzip压缩输出server {listen 80; ##监听地址及端口server_name www.clj.com; ##站点域名可以有多个用空格隔开#charset utf-8; #网页的默认字符集#access_log logs/host.access.log main; location / { ##根目录配置root html; ##网站根目录的位置/usr/local/nginx/htmlindex 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;}日志格式设定∶ $ remote_addr与$http x forwarded for用以记录客户端的ip地址; $ remote user∶ 用来记录客户端用户名称; $ time local∶ 用来记录访问时间与时区;$request∶用来记录请求的url与http协议; $status∶ 用来记录请求状态;成功是200 $body bytes sent ∶ 记录发送给客户端文件主体内容大小; $http referer∶ 用来记录从哪个页面链接访问过来的; $http user agent∶记录客户浏览器的相关信息;
通常web服务器放在反向代理的后面这样就不能获取到客户的IP地址了通过Sremote_add拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中可以增加x_forwarded_for信息用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。
location常见配置指令 root、alias、proxy_ pass root 根路径配置∶ 请求ww.clj.com/test/1.jpg会返回文件/usr/local/nginx/html/test/1.jpg alias 别名配置∶请求www.clj.com/test/1.jpg会返回文件/usr/local/nginx/html/1.jpg 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
五、访问状态统计配置
5.1、访问状态统计配置的操作步骤
1.1.先使用命令/usr/local/nginx/ sbin/nginx -V查看E安装的Nginx 是否包含HTTP_STUB_STATUS模块 1.2.修改 nginx.conf 配置文件指定访问位置并添加 stub_status 配置修改之前进行备份
cd /usr/local/nginx/conf/
cp nginx.conf nginx.conf.bak
vim nginx.confserver {listen 80;server_name www.clj.com;charset utf-8;#access_log logs/host.access.log main;location / {root html;index index.html index.htm;}##添加 stub_status 配置location /status { ##访问位置为/statusstub_status on; ##打开状态统计功能access_log off; ##关闭此位置的日志记录}1.3、重启服务访问测试
systemctl restart nginx浏览器访问 http;//192.168.229.60/status
Active connections ∶ 表示当前的活动连接数; server accepts handled requests∶表示已经处理的连接信息三个数字依次表示已处理的连接数、成功的TCP握手次数已处理的请求数。 可curl http∶//192.168.80.10/status 结合 awk与if 语句进行性能监控
5.2、实例操作访问状态统计配置
2.1 使用命令/usr/local/nginx/sbin/nginx -V查看已安装的 Nginx 是否包含HTTP_STUB STATUS模块
[rootcm ~]# cd /usr/local/nginx/sbin/
[rootcm sbin]# nginx -V
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
configure arguments: --prefix/usr/local/nginx --usernginx --groupnginx --with-http_stub_status_module
[rootcm sbin]# nginx -v
nginx version: nginx/1.12.22.2 修改nginx.conf 配置文件指定访问位置并添加stub_status 配置
[rootcm sbin]# cd /usr/local/nginx/conf/
[rootcm 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
[rootcm conf]# cp nginx.conf{,.bak}
[rootcm conf]# ls
fastcgi.conf fastcgi_params.default mime.types nginx.conf.bak scgi_params.default win-utf
fastcgi.conf.default koi-utf mime.types.default nginx.conf.default uwsgi_params
fastcgi_params koi-win nginx.conf scgi_params uwsgi_params.default
[rootcm conf]# vim nginx.confserver {listen 80;server_name www.ly.com;charset utf-8;#access_log logs/host.access.log main;location / {root html;index index.html index.htm;}location /status {stub_status on;access_log off;}2.3 重启服务后进行访问测试
六、基于授权的访问控制
6.1、基于授权的访问控制的操作步骤
1.1 生成用户密码认证文件
yum install -y httpd-tools
htpasswd -c /usr/local/nginx/passwd.db zhangsan
chown nginx /usr/local/nginx/passwd.db
chmod 400 /usr/local/nginx/passwd.db1.2 修改主配置文件相对应目录添加认证配置项
vim /usr/local/nginx/conf/nginx.conf
.....server {location / {.....##添加认证配置##auth basic secret; #设置密码提示框文字信息auth_basic_user_file /usr/local/nginx/passwd.db;}
}1.3 重启服务访问测试
nginx -t
systemctl restart nginx.service浏览器访问 http∶//192.168.229.60
6.2、实例操作基于授权的访问控制
2.1 生成用户密码认证文件 bash
[rootcm conf]# yum -y install httpd-tools
[rootcm conf]# htpasswd -c /usr/local/nginx/pass.db ly
New password:
Re-type new password:
Adding password for user ly
[rootcm conf]# cat /usr/local/nginx/pass.db
ly:$apr1$oEukgZd/$CTNKH6BuuEMaImk56/TqD/
[rootcm conf]# chown nginx /usr/local/nginx/pass.db
[rootcm conf]# chmod 400 /usr/local/nginx/pass.db
[rootcm conf]# ll /usr/local/nginx/pass.db
-r-------- 1 nginx root 41 8月 10 23:15 /usr/local/nginx/pass.db2.2 修改主配置文件相对应目录添加认证配置项
[rootcm conf]# vim /usr/local/nginx/conf/nginx.confserver {listen 80;server_name www.ly.com;charset utf-8;#access_log logs/host.access.log main;location / {root html;index index.html index.htm;auth_basic secret;auth_basic_user_file /usr/local/nginx/pass.db;}location /status {stub_status on;access_log off;} 2.3 重启服务访问测试
[rootcm conf]# 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
[rootcm conf]# systemctl restart nginx6.3、基于客户端访问控制
3.1 基于客户端访问控制的操作步骤 访问控制规则如下:
• deny IP/IP段: 拒绝某个IP或IP段的客户端访问
• allow IP/IP 段: 允许某个IP或IP段的客户端访问
• 规则从上往下执行如匹配则停止不再往下匹配 bash
vim /usr/local/nginx/conf/nginx.conflocation / {root html;index index.html index.htm;auth_basic secret;auth_basic_user_file /usr/local/nginx/passwd.db;# 添加控制规则deny 192.168.80.77; #拒绝访问的客户端IPallow all; #允许其他所有客户端访问}systemctl restart nginx3.2 实例操作基于客户端访问控制
3.2.1 在主配置文件中添加控制规则
[rootcm conf]# vim /usr/local/nginx/conf/nginx.conf
server {listen 80;server_name www.ly.com;charset utf-8;#access_log logs/host.access.log main;location / {root html;index index.html index.htm;auth_basic secret;auth_basic_user_file /usr/local/nginx/pass.db;deny 192.168.229.100;allow all;}location /status {stub_status on;access_log off;}3.2.2 重启服务并访问测试
[rootcm conf]# 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
[rootcm conf]# systemctl restart nginx使用被拒绝的客户端进行访问测试 使用其他客户端进行访问测试