网站建设如何学,网页设计教程电子书,wp怎么做双语网站,嘉兴哪家公司做网站比较好的一.Nginx的概述
Nginx是一个高性能的HTTP和反向代理web服务器#xff0c;同时也提供了IMAP/POP3/SMTP服务。Nginx稳定性高#xff0c;而且系统资源消耗少Nginx相对于Apache主要处理静态请求#xff0c;而apache主要处理动态请求Nginx是一款轻量级的Web 服务器/反向代理服务…一.Nginx的概述
Nginx是一个高性能的HTTP和反向代理web服务器同时也提供了IMAP/POP3/SMTP服务。Nginx稳定性高而且系统资源消耗少Nginx相对于Apache主要处理静态请求而apache主要处理动态请求Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件IMAP/POP3代理服务器在BSD-like 协议下发行。其特点是占有内存少并发能力强事实上nginx的并发能力在同类型的网页服务器中表现较好中国大陆使用nginx网站用户有百度、京东、新浪、网易、腾讯、淘宝等
二.Nginx的安装
Nginx安装文件可以从官方网站 http://www.nginx.org/ 下载安装运行nginx的运行环境
#下载依赖的软件
[rootnginx1 ~]# mkdir -p /apps/nginx
[rootnginx1 ~]# yum -y install gcc pcre-devel openssl-devel zlib-devel
[rootnginx1 ~]# useradd -s /sbin/nologin nginx
[rootnginx1 ~]# cd /usr/local/src/
[rootnginx1 src]# wget https://nginx.org/download/nginx-1.18.0.tar.gz
[rootnginx1 src]# ls
nginx-1.18.0.tar.gz
[rootnginx1 src]# tar xf nginx-1.18.0.tar.gz
[rootnginx1 src]# cd nginx-1.18.0/
[rootnginx1 nginx-1.18.0]# ./configure --prefix/apps/nginx --usernginx --groupnginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
#make编译 make install安装
[rootnginx1 nginx-1.18.0]# make make install
[rootnginx1 nginx-1.18.0]# chown -R nginx.nginx /apps/nginx
[rootnginx1 nginx-1.18.0]# ll /apps/nginx
总用量 0
drwxr-xr-x 2 nginx nginx 333 1月 13 13:34 conf
drwxr-xr-x 2 nginx nginx 40 1月 13 13:34 html
drwxr-xr-x 2 nginx nginx 6 1月 13 13:34 logs
drwxr-xr-x 2 nginx nginx 19 1月 13 13:34 sbin
conf:保存nginx所有的配置文件其中nginx.conf是nginx服务器的最核心的配置文件其中.conf则是用来配置nginx相关的功能
html:目录中保存nginx服务器的web文件但是可以更改为其他目录保存web文件
logs用来保存nginx服务器的访问日志错误日志等
sbin:保存nginx二进制启动脚本可以接受不同的参数实现不同的功能验证版本及编译参数
[rootnginx1 nginx-1.18.0]# ls /apps/nginx/sbin
nginx
[rootnginx1 nginx-1.18.0]# ln -s /apps/nginx/sbin/nginx /usr/bin
[rootnginx1 nginx-1.18.0]# nginx -v
nginx version: nginx/1.18.0
[rootnginx1 nginx-1.18.0]# nginx -V
nginx version: nginx/1.18.0
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC)
built with OpenSSL 1.1.1k FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix/apps/nginx --usernginx --groupnginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module使用下列的命令控制nginx
nginx -t //检查
nginx //启动
killall -1 nginx //重启
killall -3 nginx //停止三.服务控制的优化
主程序Nginx支持标准的进程信号通过kill或者killall命令发送HUP信号表示重载配置发送QUIT信号表示退出进程发送KILL信号表示杀死进程
kill -s HUP nginx //-s HUP等同于 -1,表示重载
kill -s QUIT nginx //-s QUIT等同于 -3表示停止systemctl工具来控制Nginx服务脚本
#复制同一版本的nginx的yum安装生成的service文件
[rootnginx1 ~]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Descriptionnginx - high performance web server //描述
Documentationhttp://nginx.org/en/docs/
Afternetwork-online.target remote-fs.target nss-lookup.target //描述服务类型
Wantsnetwork-online.target
[Service]
Typeforking //后台形式运行
PIDFile/apps/nginx/run/nginx.pid //PID文件位置
ExecStart/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf //启动服务
ExecReload/bin/kill -s HUP $MAINPID //根据PID重载
ExecStop/bin/kill -s TERM $MAINPID //根据PID关闭
LimitNOFILE100000
[Install]
WantedBymulti-user.target
#创建Pid文件存放目录
[rootnginx1 ~]# mkdir /apps/nginx/run/
[rootnginx1 ~]# vim /apps/nginx/conf/nginx.conf
#修改配置文件
pid /apps/nginx/run/nginx.pid;
[rootnginx1 ~]# systemctl daemon-reload
[rootnginx1 ~]# systemctl enable --now nginx
[rootnginx1 ~]# ll /apps/nginx/run
总用量 4
-rw-r--r-- 1 root root 5 1月 13 14:17 nginx.pid四.nginx的配置文件
Nginx的服务器中的主配置文件 /apps/nginx/conf/nginx.conf 中包括全局配置、I/O事件配置和HTTP配置全局配置由各种配置语句组成不使用特定的界定标记。全局配置部分包括Nginx服务的运行用户、工作进程数、错误日志、PID存放位置等基本设置
#user nobody; //运行的用户
worker_processes 1; //工作的进程数 取决于服务器的cpu
#error_log logs/error.log; //错误日志文件的位置
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid /apps/nginx/run/nginx.pid; //PID文件的位置I/O事件配置使用“event{ }”界定标记用来指定Nginx进程的I/O响应模型、每个进程的连接数等设置。对于2.6及以上的版本的内核建议使用epoll模型以提高性能每个进程的连接数根据实际需要来定一般在10000以下默认为1024
events {worker_connections 1024; //每个进程处理1024个连接
}
//Nginx能提供服务的连接数为工作进程数X连接数即1*10241024HTTP配置使用“http{ }”界定标记包括访问日志、http端口、网页目录、默认字符集、连接保持以及后面要讲到的虚拟web主机、PHP解析等一系列设置其中大部分配置语句包含在server{}中。
http {include mime.types;default_type application/octet-stream;sendfile on; //支持文件下载#tcp_nopush on; //保持连接超时时间#keepalive_timeout 0; keepalive_timeout 65;server {listen 80; //监听地址以及端口server_name localhost;location / {root 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_statius_module来启用此模块支持使用nginx -V来查看Nginx中是否包含该模块 要使用Nginx的状态统计功能除了启用内建模块以外还需要修改nginx.conf 配置文件到指定访问位置并添加stub_status配置代码
location /status {stub_status on; ##开启访问状态统计功能access_log off; ##关闭日志记录}重启服务
[rootnginx1 ~]# systemctl restart nginx.service 验证结果
[rootnginx1 ~]# curl 10.1.1.161/status
Active connections: 1 //当前的连接数
server accepts handled requests //表示已处理的连接数1 1 1 //已处理的连接数 //成功的TCP握手次数 //已处理的请求数
Reading: 0 Writing: 1 Waiting: 0