HTTP和反向代理工作图
Nginx安装
Linux常用的安装方式
- yum install /apt
- 压缩包安装
- 源码构建(可以定制配置属性)
RHEL/CentOS
Install the prerequisites:sudo yum install yum-utils
To set up the yum repository, create the file named /etc/yum.repos.d/nginx.repo
with the following contents:
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
By default, the repository for stable nginx packages is used. If you would like to use mainline nginx packages, run the following command:sudo yum-config-manager --enable nginx-mainline
To install nginx, run the following command:sudo yum install nginx
When prompted to accept the GPG key, verify that the fingerprint matches 573B FD6B 3D8F BC64 1079 A6AB ABF5 BD82 7BD9 BF62
, and if so, accept it.
控制nginx
- 启动Nginx
nginx [ -c configpath]
当我们启动nginx时:
[root@VM-244-7-centos ~]# ps -ef|grep nginx
root 16215 1 0 10:13 ? 00:00:00 nginx: master process nginx
nginx 16216 16215 0 10:13 ? 00:00:00 nginx: worker process
root 16230 2352 0 10:13 pts/0 00:00:00 grep --color=auto nginx
可以看到nginx有两个进程,一个是主进程,一个是工作进程,有主从关系。
当我们kill工作进程时,可以发现会立马起一个工作进程补上。
[root@VM-244-7-centos ~]# ps -ef|grep nginx
root 16215 1 0 10:13 ? 00:00:00 nginx: master process nginx
nginx 16216 16215 0 10:13 ? 00:00:00 nginx: worker process
root 16230 2352 0 10:13 pts/0 00:00:00 grep --color=auto nginx
[root@VM-244-7-centos ~]# kill 16216
[root@VM-244-7-centos ~]# ps -ef|grep nginx
root 16215 1 0 10:13 ? 00:00:00 nginx: master process nginx
nginx 16571 16215 0 10:15 ? 00:00:00 nginx: worker process
root 16577 2352 0 10:15 pts/0 00:00:00 grep --color=auto nginx
- 信息查看
nginx -v
看版本 nginx -V
查看所有配置信息
查看nginx信息:
[root@VM-244-7-centos ~]# nginx -v
nginx version: nginx/1.19.2
[root@VM-244-7-centos ~]# nginx -V
nginx version: nginx/1.19.2
built by gcc 8.3.1 20190507 (Red Hat 8.3.1-4) (GCC)
built with OpenSSL 1.1.1c FIPS 28 May 2019 (running with OpenSSL 1.1.1 FIPS 11 Sep 2018)
TLS SNI support enabled
configure arguments:
--prefix=/etc/nginx
--sbin-path=/usr/sbin/nginx
--modules-path=/usr/lib64/nginx/modules
--conf-path=/etc/nginx/nginx.conf
--error-log-path=/var/log/nginx/error.log
--http-log-path=/var/log/nginx/access.log
--pid-path=/var/run/nginx.pid
--lock-path=/var/run/nginx.lock
--http-client-body-temp-path=/var/cache/nginx/client_temp
--http-proxy-temp-path=/var/cache/nginx/proxy_temp
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp # 可以看到nginx和uwsgi有深度合作,所以Django选择使用uwsgi作为服务器
--http-scgi-temp-path=/var/cache/nginx/scgi_temp
--user=nginx
--group=nginx
--with-compat
--with-file-aio
--with-threads
--with-http_addition_module
--with-http_auth_request_module
--with-http_dav_module
--with-http_flv_module
--with-http_gunzip_module
--with-http_gzip_static_module
--with-http_mp4_module
--with-http_random_index_module
--with-http_realip_module
--with-http_secure_link_module
--with-http_slice_module
--with-http_ssl_module
--with-http_stub_status_module
--with-http_sub_module
--with-http_v2_module
--with-mail
--with-mail_ssl_module
--with-stream
--with-stream_realip_module
--with-stream_ssl_module
--with-stream_ssl_preread_module
--with-cc-opt='-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -fPIC'
--with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
- 控制Nginx
nginx -s signal
stop 快速关闭
直接按电源键关记quit 优雅的关闭
从开始菜单点击关记按钮(推荐)reload 重新加载配置
可以用来更新
- 通过系统管理(不推荐使用,因为类似于Nginx这种服务会有配置文件,所以使用系统管理会导致出现不可知的错误)
systemctl status nginx 查看nginx状态
systemctl start nginx 启动nginx服务
systemctl stop nginx 关闭nginx服务
systemctl enable nginx 设置开机自启
systemctl disable nginx 禁止开机自启
Nginx配置文件(核心)
Nginx配置文件包含指定指令控制的模块。
指令分为简单指令和块指令
- 一个简单指令由名称和参数组成,以空格分隔,并以分号结尾
- 一个块指令和简单指令具有相同的结构,但不是以分号结束,而是以一个大括号包围的一堆附 加指令结束
- 如果一个大括号内可以有其他的指令,它就被称为一个上下文,比如(events,http,server,location)
指令nginx -t 不运行,仅测试配置文件
测试配置文件的语法是否正确nginx -c configpath 从指定路径加载配置文件
nginx -t -c configpath 测试指定配置文件
配置文件结构
main 全局设置
events{ 工作模式,连接配置
...
}
http{ http的配置
...
upstream xxx{ 负载均衡配置
...
}
server{ 主机设置
...
location xxx{ URL匹配
...
}
}
}
查看实际部署时的配置文件vim nginx.conf
:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
user nginx
[root@VM-244-7-centos ~]# ps -ef | grep nginx
root 16215 1 0 10:13 ? 00:00:00 nginx: master process nginx
nginx 16571 16215 0 10:15 ? 00:00:00 nginx: worker process
root 19914 2352 0 10:37 pts/0 00:00:00 vim nginx.conf
root 20364 20297 0 10:41 pts/1 00:00:00 grep --color=auto nginx
可以看到第一列就为用户信息
worker_processes
第2行工作进程数量
指定Nginx开启的子进程数,多核CPU建议设置和CPU数量一样的进程数
error_log
- 第4行错误日志存放目录
用来定义全局错误日志文件,通常放在var中,level有 debug,info,notice, warn,error,crit
pid
- 第5行进程id
指定进程id的存储文件位置
查看进程id文件:
[root@VM-244-7-centos ~]# ps -ef | grep nginx
root 16215 1 0 10:13 ? 00:00:00 nginx: master process nginx
nginx 16571 16215 0 10:15 ? 00:00:00 nginx: worker process
root 19914 2352 0 10:37 pts/0 00:00:00 vim nginx.conf
root 21485 20297 0 10:49 pts/1 00:00:00 grep --color=auto nginx
[root@VM-244-7-centos ~]# cat /var/run/nginx.pid
16215
这么做的原因是,当关闭进程服务时,会调用系统指令kill,那么直接读取该文件就可以让nginx得知自己的进程id为多少。
events
- 指定工作模式和以及连接上限
默认使用epoll 的高效事件模型
事件,通常指定两种:
连接数量
- worker_connections 一个工作进程默认子进程最大连接数
正向代理 连接数 进程数
反向代理 连接数 进程数 / 4 (效率低75%,因为反向代理转发给uwsgi时在等待响应时需要保持长连接,而长连接需要socket,最下方有详细讲解)
linux系统限制最多能同时打开65535个文件,默认上限就是65535,可解除 ulimit -n 65535
工作模式
- use 指定nginx工作模式
- epoll 高效工作模式,linux
- kqueue 高效工作模式, bsd
- poll 标准模式
- select 标准模式
HTTP
mime.types
里面存放的是允许传输的数据类型,根据文件类型打开文件,如果注释掉,不管打开任何数据类型的文件都为下载。
default_type
log_format
access_log
sendfile
keepalive_timeout
gzip
/etc/nginx/conf.d/*.conf
Server
server {
listen 80; # 监听80端口
server_name localhost; # 服务器域名或者ip
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/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 /usr/share/nginx/html;
}
location / {
root /usr/share/nginx/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 /usr/share/nginx/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;
#}
}
- listen 80; 指定虚拟主机监听的端口
- server_namelocalhost; 指定ip地址或域名,多个域名使用空格隔开
- charset utf-8; 指定网页的默认编码格式
- error_page 500 502 /50x.html 指定错误页面
- access_log xxx main; 指定虚拟主机的访问日志存放路径
- error_log xxx main; 指定虚拟主机的错误日志存放路径
- root xxx; 指定这个虚拟主机的根目录
Location(核心中的核心)
核心中的核心,以后的主要配置都在这
- 主要功能:(相当于路由器)定位url,解析url,支持正则匹配,还能支持条件,实现动静分离
语法:
location [modifier] uri{ ... }
modifier 修饰符
- = 使用精确匹配并且终止搜索
- ~ 区分大小写的正则表达式
- ~* 不区分大小写的正则表达式
- ^~ 最佳匹配,不是正则匹配,通常用来匹配目录
- 常用指令
alias 别名,定义location的其他名字,在文件系统中能够找到,如果location指定了正则表达式,alias将会引用正则表达式中的捕获,alias替代lication中匹配的部分,没有匹配的部分将会在文件系统中搜索
补充var目录
一般用来存放越用越大的文件
socket工作流程图
所以长连接需要4个线程来维护,所以效率不高。
此处评论已关闭