摘要:nginx


web方向

​ 经典提问:

​ 在浏览器里输入一个www.baidu.com回车,到显示出内容,背后发生哪些事情?

​ 应用层 --> 传输层 --> 网络层 --> 数据链路层 --> 物理层

​ nginx --> 详细的使用

​ flask --> web服务器 --> python写的 --> 自己写的web服务器软件

​ mvc, usgi等

​ nginx --> web服务器 --> C语言

​ go语言 --> bingo

​ http/https协议


nginx

​ 1.安装 --> 最新版本 --> 编写脚本 --> 一键安装

​ 2.nginx的配置文件深入讲解 --> 经典的功能实现

​ 3.一定要使用一个云服务器 --> 购买域名 --> 备案 --> 上线

​ 4.http协议


1. nginx是什么?

nginx [engine x] is an HTTP and reverse proxy server, a mail proxy server, and a generic TCP/UDP proxy server, originally written by Igor Sysoev.

http://nginx.org/

​ http是什么?

​ HTTP是Hyper Text Transfer Protocol(超文本传输协议)的缩写

​ 工作:应用层

​ www --> World Wide Web 万维网

​ 协议:其实就规矩,方便双方沟通使用

​ http协议是:浏览器和web服务器之间使用的

​ 谁能读懂http协议?

​ 所有的浏览器,所有web服务器,爬虫库(requests)等其他


2. nginx的安装

centos8.2 nginx-1.19.5

nginx最新的 nginx-1.19.5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@cPen ~]# cat /etc/centos-release	#注:查看版本
CentOS Linux release 7.6.1810 (Core)
1.下载
mkdir -p /nginx
cd /nginx
curl -O http://nginx.org/download/nginx-1.19.5.tar.gz
2.解压
[root@cPen_B nginx]# tar xf nginx-1.19.5.tar.gz
[root@cPen_B nginx]# cd nginx-1.19.5
[root@cPen_B nginx-1.19.5]#
编译安装经典3步:
1.编译前的配置:配置安装到哪里,启用哪些功能等
./configure
2.编译,将c的代码编译成二进制文件
make
3.编译安装,将编译好的二进制文件复制到第一步里我们指定的路径
make install

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
[root@cPen_B nginx-1.19.5]# ./configure --help  查看哪些功能可以开启,哪些功能可以禁用
prefix 前缀

--prefix=/usr/local/nginx 指定安装路径
--with-select_module 启用,默认没有启用
--without-select_module 禁用,默认是启用

--with-http_ssl_module 启用https功能
--with-http_realip_module --》修改http协议,里面增加一个字段realip-->nginx服务器做反向代理服务器的时候使用

============
解决依赖关系
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc gcc-c++ autoconf automake make

编译前的配置
[root@cPen_B nginx-1.19.5]# ./configure --prefix=/usr/local/nginx --user=cPen_nginx --group=cPen_nginx --build=scweb_server --with-threads --with-file-aio --with-http_v2_module --with-http_ssl_module --with-stream

编译
make -j 2

编译安装
make install

启动nginx
[root@cPen_B nginx-1.19.5]# cd /usr/local/nginx
[root@cPen_B nginx]# ls
conf html logs sbin
conf 存放配置文件
html 存放网页文件
logs 存放日志
sbin 存放启动nginx的程序
[root@cPen_B nginx]# cd sbin/
[root@cPen_B sbin]# ls
nginx
[root@cPen_B sbin]# ./nginx
[root@cPen_B sbin]# lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 23548 root 9u IPv4 66009 0t0 TCP *:http (LISTEN)
nginx 23549 cPen_nginx 9u IPv4 66009 0t0 TCP *:http (LISTEN)
[root@cPen_B sbin]# ps aux|grep nginx
root 23548 0.0 0.0 41072 836 ? Ss 19:09 0:00 nginx: master process ./nginx
cPen_ng+ 23549 0.0 0.2 74636 4852 ? S 19:09 0:00 nginx: worker process
#注:master是领导,worker真正干活的
master process 父进程:管理进程
worker process 子进程:被管理的进程

#注:pstree 查看进程树 -p选项看到进程号

#修改PATH变量,方便启动nginx和停止nginx
[root@cPen_B sbin]# PATH=/usr/local/nginx/sbin/:$PATH
[root@cPen_B sbin]# echo $PATH
/usr/local/nginx/sbin/:/lianxi/sc:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/nginx/sbin:/root/bin:/usr/local/nginx5/sbin:/root/bin

#关闭防火墙和selinux
[root@cPen nginx]# service firewalld stop #注:立马关闭防火墙
Redirecting to /bin/systemctl stop firewalld.service
[root@cPen nginx]# systemctl disable firewalld #注:设置防火墙开机不要启动
#关闭selinux
setenforce 0 #临时关闭
sed -i '/^SELINUX/ s/enforcing/disabled/' /etc/sysconfig/selinux
sed -i '/^SELINUX/ s/enforcing/disabled/' /etc/selinux/config

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
开机启动nginx的问题
脚本的方式
思考:如何让编译安装的nginx开机启动?
1./etc/rc.local
[root@sc-nginx sbin]# echo "/usr/local/nginx/sbin/nginx" >>/etc/rc.local
[root@sc-nginx sbin]# chmod +x /etc/rc.d/rc.local
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
------------------------------------------------------------------------
[root@cPen_B ~]# chmod +x /etc/rc.d/rc.local
#注:这是给源文件可执行权限
[root@cPen_B ~]# chmod +x /usr/local/nginx/sbin/nginx
[root@cPen_B ~]# vim /etc/rc.local #注:启动的时候会执行这个文件里的脚本
#注:添加 /usr/local/nginx/sbin/nginx (最好是绝对路径)
#on boot
echo "/usr/local/nginx/sbin/nginx" >>/etc/rc.local
chmod +x /etc/rc.d/rc.local

配置文件

​ 路径 /usr/local/nginx/conf/

​ nginx.conf #注:主配置文件

面试:请你说说nginx配置文件里有哪些常见的配置 (讲它的结构)

​ 日志

​ access.log 记录正常的访问

​ error.log 访问出错的信息

​ listen

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
[root@cPen conf]# cat nginx.conf
#user nobody; #注:指定启动nginx的用户
worker_processes 1; #注:工作进程的数量 需要和cpu核心的数量一致

#error_log logs/error.log; #注:日志文件放在哪里,日志级别
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;

#注:块 {}
events {
worker_connections 1024; #注:表示nginx可以同时支持的并发数,同时支持多少人访问,具体需要考虑机器的cpu,内存,磁盘IO,网络带宽 --》压力测试
}


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;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

server { #注:提供http服务的,一个server对应一个网站
#listen 80; #注:监听的端口
listen 8080;
#server_name localhost; #注:为哪个网站(域名)提供网站的
server_name www.cpen.top;

#charset koi8-r;
charset utf-8;

#access_log logs/host.access.log main;

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;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# 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;
#}
}


# 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;
# }
#}

}

配置文件里相关配置

image-20221011201523665


防火墙

1
iptables -A INPUT -p tcp --dport 8099 -j ACCEPT

nginx的首页文件

[root@cPen html]# cd /usr/local/nginx/html/

[root@cPen html]# vim index.html #注:修改首页

#注:修改内容自己定义

#注:修改网页内容不需要刷新nginx服务

​ 只有修改nginx的配置文件才需要刷新nginx服务


使用域名访问:

​ 阿里云:没有备案的域名不允许访问

​ 腾讯云:没有备案的域名不允许访问

购买域名–》绑定公网ip–》搞一个首页–》去备案


3. shell编程语法

shell编程语法

1
2
3
4
5
6
7
8
9
10
\  续行
cmd1 && cmd2
cmd1 || cmd2
cmd1 && cmd2 || cmd3

test -d '/usr/local/nginx8/logs'
[ -d /usr/local/nginx8/logs ] 判断/usr/local/nginx8/logs这个目录是否存在

test -f '/usr/local/nginx8/conf/nginx.conf'
[ -f /usr/local/nginx8/conf/nginx.conf ] 判断/usr/local/nginx8/conf/nginx.conf这个文件是否存在

1
2
3
4
5
[root@sc-nginx nginx-1.19.5]# which killall
/usr/bin/killall
[root@sc-nginx nginx-1.19.5]# rpm -qf /usr/bin/killall
psmisc-23.1-5.el8.x86_64
#注:killall -9 nginx 杀死所有nginx进程

1
2
#注:vim编辑器 全部替换
:%s/nginx8/nginx18/

4. web

1
2
3
4
5
6
7
8
9
10
11
12
13
nginx
官方网站 http://nginx.org/en/
http://nginx.org/en/docs/

安装 编译

网站
静态页面 纯html,css,js(没有和数据库交互)
nginx的强项是解析静态页面
动态页面 需要使用动态编程语言:python,go,java,c#,php等
和数据库交互 --> 读写数据
图解

image-20221011202103199
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
模块
--with-http_realip_module
--with-http_ssl_module
--with-stream 让nginx具有负载均衡,支持4层
enables building the stream module for generic TCP/UDP proxying and load balancing.
启动nginx
./nginx
启动
./nginx -s stop 停止
nginx -s reload 重启
kill
killall
pstree yum install psmisc
lsof -i:80
netstat -anplut
PATH变量的修改 PATH=/usr/local/nginx/sbin/:$PATH 临时修改
永久修改
vim /etc/profile 在末尾添加 PATH=/usr/local/nginx/sbin:$PATH


http协议

​ 1.0

​ 1.1

​ 2.0

​ http2.0比http1.1好在哪里?

​ http和https的区别


设置xshell小键盘可用

image-20221011202303724

#注:属性 --》 终端 --》 VT模式 --》 设置为普通


–with-http_realip_module --》修改http协议,里面增加一个字段realip–>nginx服务器做反向代理服务器的时候使用

image-20221011202334741

5. nginx安装脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
[root@cPen nginx]# cat onekey_install_nginx.sh 
#!/bin/bash

#time:2020-12-12
#author: cPen
#company: cPen
#mail:1533431376@qq.com

#新建用户,用来启动nginx
useradd -s /sbin/nologin cPen_nginx
#解决依赖关系
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc gcc-c++ autoconf automake make psmisc lsof net-tools vim

#download file
mkdir -p /nginx
cd /nginx
curl -O http://nginx.org/download/nginx-1.19.5.tar.gz
#解压文件
tar xf nginx-1.19.5.tar.gz
cd nginx-1.19.5
#编译前的配置
./configure --prefix=/usr/local/nginx --user=cPen_nginx --group=cPen_nginx --build=scweb_server --with-threads --with-file-aio --with-http_v2_module --with-http_ssl_module --with-stream

#编译,-j 2 启动2个进程去编译,需要你有2个核心的cpu
make -j 2

#编译安装
make install

#修改PATH变量
PATH=/usr/local/nginx/sbin:$PATH
echo 'PATH=/usr/local/nginx/sbin:$PATH' >>/etc/profile

#关闭防火墙
service firewalld stop
systemctl disable firewalld

#关闭selinux
setenforce 0 #临时关闭
sed -i '/^SELINUX/ s/enforcing/disabled/' /etc/sysconfig/selinux
sed -i '/^SELINUX/ s/enforcing/disabled/' /etc/selinux/config

#启动nginx
nginx

#on boot
echo "/usr/local/nginx/sbin/nginx" >>/etc/rc.local
chmod +x /etc/rc.d/rc.local