一篇小文章带你了解nginx

一、nginx的官方文档

nginx documentation

二、nginx是什么?

nginx是一个做网站服务器的软件,是静态的网站

三、shell里一键部署安装nginx的脚本

#!/bin/bash

#解决软件的依赖关系,需要安装的软件包
yum install epel-release -y
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc gcc-c++ autoconf automake make psmisc net-tools lsof vim geoip geoip-devel wget -y

#新建luogan用户和组
id  sclilin99 || useradd sclilin99 -s /sbin/nologin

#下载nginx软件
mkdir  /sclilin99 -p
cd /sclilin99
wget  https://nginx.org/download/nginx-1.21.4.tar.gz

#解压软件
tar xf nginx-1.21.4.tar.gz 
#进入解压后的文件夹
cd nginx-1.21.4

#编译前的配置
./configure --prefix=/usr/local/sclilin99  --user=sclilin99 --group=sclilin99  --with-http_ssl_module   --with-threads  --with-http_v2_module  --with-http_stub_status_module  --with-stream  --with-http_geoip_module --with-http_gunzip_module

#如果上面的编译前的配置失败,直接退出脚本
if (( $? != 0));then
	exit
fi
#编译
make -j 2
#编译安装
make  install

#修改PATH变量
echo  "PATH=$PATH:/usr/local/sclilin99/sbin" >>/root/.bashrc
#执行修改了环境变量的脚本
source /root/.bashrc


#firewalld and selinux

#stop firewall和设置下次开机不启动firewalld
service firewalld stop
systemctl disable firewalld

#临时停止selinux和永久停止selinux
setenforce 0
sed  -i '/^SELINUX=/ s/enforcing/disabled/' /etc/selinux/config

#开机启动
chmod +x /etc/rc.d/rc.local
echo  "/usr/local/sclilin99/sbin/nginx" >>/etc/rc.local

#修改nginx.conf的配置,例如:端口号,worker进程数,线程数,服务域名

sed  -i '/worker_processes/ s/1/2/' /usr/local/sclilin99/conf/nginx.conf
sed  -i  '/worker_connections/ s/1024/2048/' /usr/local/sclilin99/conf/nginx.conf
sed  -i -r '36c \\tlisten  80;' /usr/local/sclilin99/conf/nginx.conf
sed  -i -r '37c \\tserver_name www.sclilin99.com;' /usr/local/sclilin99/conf/nginx.conf

#killall nginx进程
killall -9 nginx

#启动nginx
/usr/local/sclilin99/sbin/nginx

四、nginx安装

1、yum安装    到nginx或者centos的官方去下载nginx的软件包安装
                        nginx.*.tar.gz --->制作成--》nginx.*.rpm结尾

2、编译安装    nginx是使用c语言编写的,将源码编译成二进制程序,然后安装
                        需要自己解决软件之间的依赖关系,还需要指定很多的配置,难度大,可以可定                              制开启需要的功能--》可以定制功能和指定安装的路径

编译安装经典三部曲:

1、编译前的配置文件  例:./configure --prefix=/usr/local/sclilin99  --user=lilin --group=lilin  --with-http_ssl_module   --with-threads  --with-http_v2_module  --with-http_stub_status_module  --with-stream  --with-http_gunzip_module

2、编译(理解为讲源代码编译成二进制程序) make

3、编译安装(将已经编译好的二进制程序安装(cp)到指定的路径) make install

五、安装nginx后里面几个文件的作用

[root@localhost ~]# cd /usr/local/sclilin99/
[root@localhost sclilin99]# ls
client_body_temp  fastcgi_temp  logs        sbin       uwsgi_temp
conf              html          proxy_temp  scgi_temp
[root@localhost sclilin99]# cd html
[root@localhost html]# ls
50x.html  index.html
[root@localhost html]# 

conf-----》存放nginx的配置文件

log-------》存放日志

sbin------》存放nginx的启动程序

html------》存放网站的网页的目录

html里的index.html指的是首页-------》进入某个网站看到的第一个页面,想要去其他页面需要通过首页里的链接进行跳转

六、如何判断nginx是否启动

1、看端口 netstat -anplut lsof -i:80 ss -anplut

2、看进程 ps aux|grep nginx

3、直接访问

4、看日志 tail -f(动态监控)

七、关闭nginx

1、nginx -s stop

[root@localhost html]# ps -aux|grep nginx
root      77302  0.0  0.1  46372  2008 ?        Ss   17:28   0:00 nginx: master process nginx
lilin     77367  0.0  0.1  46812  2044 ?        S    17:29   0:00 nginx: worker process
root      83126  0.0  0.0 112824   988 pts/0    S+   19:27   0:00 grep --color=autonginx
[root@localhost html]# nginx -s stop
[root@localhost html]# ps -aux|grep nginx
root      83148  0.0  0.0 112824   984 pts/0    R+   19:28   0:00 grep --color=autonginx

2、nginx -s quit(推荐)

[root@localhost html]# ps -aux|grep nginx
root      83256  0.0  0.0  46232  1160 ?        Ss   19:30   0:00 nginx: master process nginx
lilin     83257  0.0  0.1  46692  1916 ?        S    19:30   0:00 nginx: worker process
root      83261  0.0  0.0 112824   984 pts/0    R+   19:30   0:00 grep --color=autonginx
[root@localhost html]# nginx -s quit
[root@localhost html]# ps -aux|grep nginx
root      83289  0.0  0.0 112824   988 pts/0    R+   19:30   0:00 grep --color=autonginx

3、nginx -s reload(重点)

修改了nginx的配置文件,相当于刷新服务(启用新的配置),不会中断业务

[root@localhost ~]# cd /usr/local/sclilin99/
[root@localhost sclilin99]# cd conf/
[root@localhost conf]# ls
fastcgi.conf            koi-win             scgi_params
fastcgi.conf.default    mime.types          scgi_params.default
fastcgi_params          mime.types.default  uwsgi_params
fastcgi_params.default  nginx.conf          uwsgi_params.default
koi-utf                 nginx.conf.default  win-utf
[root@localhost conf]# vim nginx.conf
# worker_processes  1      ----》改成   worker_processes  4
[root@localhost conf]# nginx
[root@localhost conf]# nginx -s reload
[root@localhost conf]# ps -aux|grep nginx
root      83534  0.0  0.1  46372  2008 ?        Ss   19:35   0:00 nginx: master process nginx
lilin     83542  0.0  0.1  46812  2044 ?        S    19:35   0:00 nginx: worker process
lilin     83543  0.0  0.1  46812  2044 ?        S    19:35   0:00 nginx: worker process
lilin     83544  0.0  0.1  46812  2044 ?        S    19:35   0:00 nginx: worker process

4、kill(直接杀死)

使用kill的话得先杀死master,如果先杀掉worker的话,master会创建一个新的worker来取代

也可以理解为master是老板不需要干活,只需要监督worker;worker是员工老板手底下干活的,

新来的员工取代了老员工,取之不尽用之不竭,所以必须先把老板master杀死,再杀员工。

例:杀掉所有的nginx进程

[root@localhost conf]# ps -aux|grep nginx
root      83534  0.0  0.1  46372  2008 ?        Ss   19:35   0:00 nginx: master process nginx
lilin     83542  0.0  0.1  46812  2044 ?        S    19:35   0:00 nginx: worker process
lilin     83543  0.0  0.1  46812  2044 ?        S    19:35   0:00 nginx: worker process
lilin     83544  0.0  0.1  46812  2044 ?        S    19:35   0:00 nginx: worker process
lilin     83545  0.0  0.1  46812  2044 ?        S    19:35   0:00 nginx: worker process
[root@localhost conf]# killall -9 nginx
[root@localhost conf]# ps -aux|grep nginx
root      84189  0.0  0.0 112824   984 pts/0    R+   19:48   0:00 grep --color=autonginx

杀老板(master)再杀员工(worker)  kill -9 进程号       -9-------》强制杀死

[root@localhost conf]# ps -aux|grep nginx
root      84351  0.0  0.1  46372  2008 ?        Ss   19:52   0:00 nginx: master process nginx
lilin     84356  0.0  0.1  46812  2044 ?        S    19:52   0:00 nginx: worker process
root      84360  0.0  0.0 112824   988 pts/0    S+   19:52   0:00 grep --color=autonginx
[root@localhost conf]# kill -9 84351
[root@localhost conf]# ps -aux|grep nginx
lilin     84356  0.0  0.1  46812  2044 ?        S    19:52   0:00 nginx: worker process
root      84392  0.0  0.0 112824   988 pts/0    S+   19:52   0:00 grep --color=autonginx
[root@localhost conf]# kill -9 84356
[root@localhost conf]# ps -aux|grep nginx
root      84428  0.0  0.0 112824   988 pts/0    R+   19:53   0:00 grep --color=autonginx

老板员工一起杀死(kill -3 老板进程号)  -3 ---》相当于信号quit

[root@localhost conf]# ps -aux|grep nginx
root      92315  0.0  0.1  46372  2008 ?        Ss   10:42   0:00 nginx: master process nginx
lilin     92336  0.0  0.1  46812  2284 ?        S    10:42   0:00 nginx: worker process
root      92797  0.0  0.0 112824   988 pts/0    R+   10:52   0:00 grep --color=auto nginx
[root@localhost conf]# kill -3 92315
[root@localhost conf]# ps -aux|grep nginx
root      92828  0.0  0.0 112824   988 pts/0    S+   10:52   0:00 grep --color=auto nginx

八、进程和进程之间通信的方式与信号

1、进程和进程之间通信的方式
    1.信号
    2.信号量
    3.管道
    4.共享内存
    5.socket
    6.消息队列

2、信号

[root@localhost conf]# kill -l
 1) SIGHUP	 2) SIGINT	 3) SIGQUIT	 4) SIGILL	 5) SIGTRAP
 6) SIGABRT	 7) SIGBUS	 8) SIGFPE	 9) SIGKILL	10) SIGUSR1
11) SIGSEGV	12) SIGUSR2	13) SIGPIPE	14) SIGALRM	15) SIGTERM
16) SIGSTKFLT	17) SIGCHLD	18) SIGCONT	19) SIGSTOP	20) SIGTSTP
21) SIGTTIN	22) SIGTTOU	23) SIGURG	24) SIGXCPU	25) SIGXFSZ
26) SIGVTALRM	27) SIGPROF	28) SIGWINCH	29) SIGIO	30) SIGPWR
31) SIGSYS	34) SIGRTMIN	35) SIGRTMIN+1	36) SIGRTMIN+2	37) SIGRTMIN+3
38) SIGRTMIN+4	39) SIGRTMIN+5	40) SIGRTMIN+6	41) SIGRTMIN+7	42) SIGRTMIN+8
43) SIGRTMIN+9	44) SIGRTMIN+10	45) SIGRTMIN+11	46) SIGRTMIN+12	47) SIGRTMIN+13
48) SIGRTMIN+14	49) SIGRTMIN+15	50) SIGRTMAX-14	51) SIGRTMAX-13	52) SIGRTMAX-12
53) SIGRTMAX-11	54) SIGRTMAX-10	55) SIGRTMAX-9	56) SIGRTMAX-8	57) SIGRTMAX-7
58) SIGRTMAX-6	59) SIGRTMAX-5	60) SIGRTMAX-4	61) SIGRTMAX-3	62) SIGRTMAX-2
63) SIGRTMAX-1	64) SIGRTMAX	

signal 信号
 1) SIGHUP     
         nohup可以用来屏蔽hup信号
通常是在终端的控制进程结束时, 通知同一session内的各个作业停止运行
登录Linux时,系统会分配给登录用户一个终端(Session)。在这个终端运行的所有程序,包括前台进程组和后台进程组,一般都 属于这个 Session。当用户退出Linux登录时,前台进程组和后台有对终端输出的进程将会收到SIGHUP信号。这个信号的默认操作为终止进程,因此前台进 程组和后台有终端输出的进程就会中止。
    nohup  bash while.sh  &     &---》指放到后台去运行

2) SIGINT 
程序终止(interrupt)信号, 在用户键入INTR字符(通常是Ctrl+C)时发出,用于通知前台进程组终止进程。

3) SIGQUIT    
其实可以理解为中断程序的正常运行,同时也会给子进程也终止

9) SIGKILL
 用来立即结束程序的运行. 本信号不能被阻塞、处理和忽略。
 告诉linux 内核去强制杀死进程--》理解为内核派出锦衣卫去杀死某个人

15) SIGTERM
程序结束(terminate)信号, 与SIGKILL不同的是该信号可以被阻塞和处理。通常用来要求程序自己正常退出,shell命令kill缺省产生这个信号。

trap 屏幕各种信号,唯独9这种信号不能被屏蔽

[root@localhost sclilin99]# cat while.sh 
trap "echo i am busy"  15 3 2 1 9
i=1
while :
do
	echo $i
	((i++))
	sleep 1

九、日志级别及作用(loglevel)

日志:记录程序在运行过程中发生的事情

        时间 + 地点 + 人物 +事件

日志的级别:

0 EMERG.  emergency 紧急  --》系统不能使用了
1 ALERT.  告警
2 CRIT.   严重 Critical
3 ERR.     程序因为某个错误导致不能运行,只要是error以上的级别都会导致程序启动或者是运行失败
4 WARNING. 警告 ,不会影响程序的正常运行,只是提醒而已 
5 NOTICE.  正常的记录,有点影响力的事件
6 INFO.    普通的信息都记录
7 DEBUG.  调试模式: (程序执行的整个过程都记录,方便开发者调试程序)   什么都记录

日志的作用:排错、解决故障、调试优化

十、了解nginx的配置文件

1、配置文件的作用:传参,修改nginx进程里变量的值

2、了解nginx.conf的内容

[root@cbh conf]# cat nginx.conf

#user  nobody;            (默认使用nobody用户去启动nginx)
worker_processes  1;      (worker进程的数量;一般建议工作进程的数量和cpu核心一致)

#error_log  logs/error.log;   (错误日志)
#error_log  logs/error.log  notice;   (错误日志的级别在notice以上)
#error_log  logs/error.log  info;     (错误日志的级别在info以上)

#pid        logs/nginx.pid;       (nginx里master的pid)


events {                        events(事件)
    worker_connections  1024; (并发数量,同时可以允许多少人同时访问nginx--》同时1024个人访问)
}


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;

    #keepalive_timeout  0;
    keepalive_timeout  65;          (65秒后nginx会主动断开连接)

    #gzip  on;      (启用压缩功能)

    #制定了访问的次数的政策1秒钟允许访问一次,统计的依据是客户机的ip地址
    limit_req_zone $binary_remote_addr zone=perip:10m rate=1r/s;

    server {                           (虚拟主机)
        listen       80;               (监听80端口)
        server_name  localhost;        (网站服务的域名)

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {                (location——>指路由)访问网站的根目录
            root   html;            (html是存放网页的根目录 -》/usr/local/sclilin99/html)
            index  index.html index.htm;
        }

        #error_page  404              /404.html;   (访问的页面不存在,调到404页面)
        # redirect server error pages to the static page /50x.html
        
        error_page   500 502 503 504  /50x.html;  (50x的状态码,代表是nginx的内部错误)
        location = /50x.html {
            root   html;
        }
	location ~ /feng
        {
                proxy_pass http://www.taobao.com;
        }
        location /hejin
        {
                #root html/hejin;
                index  he.html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {              (模糊匹配以php结尾的)   
        #    proxy_pass   http://127.0.0.1; (将上面的通过这个网址解析出来)proxy--》代理
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    server { 
		listen 80;
		server_name www.song.com;
		location / {
			root html/song;
			index  index.html  index.htm  shouye.html;
		}
        	access_log  logs/song.access.log  main;
		error_log  logs/song.error.log  notice;
	}

    server { 
		listen 8080;
		server_name www.huang.com;
		location / {
			root html/huang;
			index  index.html  index.htm  shouye.html;
		}
        	error_page  404              /404.html;
        	access_log  logs/huang.access.log  main;
		    error_log  logs/huang.error.log  notice;
       		error_page   500 502 503 504  /50x.html; (默认会到server里的root指定的 html/huang目录下查找)
		limit_req zone=perip burst=5 nodelay;
        	location = /50x.html {
            	root   html;          (指定路由,访问50x.html页面的时候到html目录下查找)
        	}
	}
    server { 
		listen 80;
		server_name www.peng.com;
		location / {
			root html/peng;
			index  index.html  index.htm  shouye.html;
		}
        	access_log  logs/peng.access.log  main;
		    error_log  logs/peng.error.log  notice;
	}
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

3、mime类型

详细介绍:nginx中的MIME.types的作用_会飞的鱼的博客-CSDN博客_mime.types nginx

 十一、IO多路复用

多路是指网络连接,复用(重复使用)指的是同一个线程

 

 2、调度算法

select(select 基于轮训机制)

epoll(epoll基于操作系统支持的I/O通知机制 epoll支持水平触发和边沿触发两种模式)事件

poll(类似于select)

3、解决大并发问题方法

其实是nginx借用内核的(select、epoll、poll)这三种算法去解决大并发的问题,

一般默认是epoll

4、解析select和epoll

深入了解请看:一文搞懂select、poll和epoll区别 - 知乎

举例:

假设你在大学读书,住的宿舍楼有很多间房间,你的朋友要来找你。 select版宿管大妈就会带着你的朋友挨个房间去找,直到找到你为止。 而epoll版宿管大妈会先记下每位同学的房间号, 你的朋友来时,只需告诉你的朋友你住在哪个房间即可,不用亲自带着你的朋友满大楼找人。 如果来了10000个人,都要找自己住这栋楼的同学时,select版和epoll版宿管大妈,谁的效率更高,不言自明。 同理,在高并发服务器中,轮询I/O是最耗时间的操作之一,select和epoll的性能谁的性能更高,同样十分明了。

十二、用户反映访问不了我们的“www.song.com”这个网站,你如何排查?

客户的问题:客户的机子是否能上网

我们的问题-- (由外--》里)

    1.服务器是否还是开机?还是运行的
    2.服务器的网络是否正常
        ping
    3.检查nginx是否运行?
            ps -aux|grep nginx
    4.检查端口号是否开放?--》查看防火墙
        netstat -anplut
        lsof -i:80
        ss  -anplut
    5.看nginx的日志
        access.log
        error.log

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值