栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

nginx

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

nginx

一.网页的笼统介绍

web服务包括:nginx, apacheck(httpd), tomcat

网页也分为:静态网页(.html,.htm.xml)

                      动态网页(.php,.jsp)

nginx和apache默认解析静态网页

二.为什摸要用nginx? nginx和Apache的区别?

1.nginx使用的是epoll模型,是异步非阻塞;

Apache使用的是select模型,是同步阻塞

异步非阻塞主进程开多个子进程(异步),CPU在工作,,默认每个进程可以有1024个连接数
同步阻塞子进程运行时,其他数据需要排队

2.nginx支持负载均衡(既支持4层,又支持7层的)反向代理。配置比Apache简单

3.nginx处理静态要比Apache块,但动态处理能力就不出Apache

三.安装nginx步骤

1.1 安装nginx可以进官网进行下载也可以通过wget命令进行下载

http://nginx.org/download/nginx-1.20.2.tar.gz

[root@localhost ~]# wget http://nginx.org/download/nginx-1.20.2.tar.gz

 1.2 下载完成后开始对包的一系列操作

[root@localhost]# yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
[root@localhost ~]# ls
123.txt  anaconda-ks.cfg  nginx-1.20.2.tar.gz
[root@localhost ~]# tar zxf nginx-1.20.2.tar.gz -C /usr/local/nginx #将包解压到/usr/local/nginx
 
[root@localhost ~]# cd /usr/local/nginx/nginx-1.20.2/   #进入解压包
[root@localhost nginx-1.20.2]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_realip_module                               #对包进行一些列配置
[root@localhost nginx-1.20.2]# make && make install     #编译&&安装

  配置包的解释说明

gcc可以编译,c,c++,Ada,Object,c和java等语言
pcre  pcre-devel模块可以使用pcre来解析正则表达式
zlib    zlib-devel提供了多种压缩模式与解压方式
openssl   openssl-devel相当于https加密

 1.3 然后编写启动脚本

vi /etc/init.d/nginx

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig:   - 85 15
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse 
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /usr/local/nginx/conf/nginx.conf
# pidfile:     /usr/local/nginx/logs/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

lockfile=/var/lock/subsys/nginx

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    start
}

reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}

force_reload() {
    restart
}

configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

1.4  将文件加执行权限  chmod +x

       完成之后检查一下:/sbin/chkconfig nginx on

2.1  然后就可以访问nginx界面了:

四.nginx配置文件

1.nginx重要配置文件有:

confignginx配置文件
htmlnginx的网页文件
logs日志文件
sbinnginx启动文件

2.nginx的配置文件部分

        全局部分:Worker_Process  1;  ##子进程的古树,最好不要大于主机cpu的内核数量

                       还可以添加:         PID文件                启动用户组

        events配置部分:写的是每个进程最大能处理的客户端连接数

                                可以指定epoll模型

        http部分:http例可以包含多个server{        一个server可以包含多个location{

}         }(一个server可以代表一个主机)

        

include   mine.types可以解析的类型,后面是文件名,文件里面是各种类型
default_type默认类型
sendfile  no;零拷贝
keepalive_time 超时时间,默认65秒断开
listen   80默认监听端口号
server name localhost一般为域名
location 匹配文件夹中根下找index等文件
error_page500 502 503 504,状态码,返回这些值就找/50x.html

3.nginx访问日志

head -1 access_log先拿一行最新的访问日志

显示:

183.202.144.5 - - [13/Apr/2020:14:55:46 +800] "GEI/HTTP/1.1"200 30" -" Moailla/5.0(windows 
  
NT 6.1;win64;x64;rv;74.0)Gecko/20100101 Firefox/74.0
#客户端ip  
第一个"-"是邮箱编号 
第二个"-"是用什么用户登录的
后面是时间:2020年14时55分46秒  +800在东八区
GET意思是用户登录是为了下载
HTTP请求的协议
"200 30" 200是状态码,30为包的大小
后面就是Windows系统 win64位
Firefox使用火狐访问的

4.如果想查看那些地区访问量最高,进行一个前十名的统计

cat access_log | awk '$1' |sotr -rn |uniq -c |sotr -rn |head -10

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/871528.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号