nginx快速入门和复习
- Nginx 是高性能的 HTTP 和反向代理的web服务器,处理高并发能力是十分强大的,能经受高负 载的考验,有报告表明能支持高达 50,000 个并发连接数。
- 其特点是占有内存少,并发能力强,
反向代理
正向代理是通过一个已知的服务器跳转访问网址
反向代理是通过访问反向代理的服务器(未知)来访问网址
负载均衡
通过反向代理的把请求平均分发到各个服务器上。
动静分离
加快网站解析速度,把动态页面和静态页面分开存放到不同的服务器上,降低单个服务器压力。
静态资源包括html,css
动态资源包括jsp,servlet
nginx常用命令
查看版本:./nginx -v
启动nginx: ./nginx
关闭nginx:./nginx -s stop
重新加载nginx: ./nginx -s reload
nginx 配置文件
nginx.conf
三部分组成
全局块
开始到events块之间的内容,设置一些影响nginx服务器整体运行的配置指令。
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
worker_processes 1;
这是 Nginx 服务器并发处理服务的关键配置,worker_processes 值越大,可以支持的并发处理量也越多,但是 会受到硬件、软件等设备的制约。
events块
主要影响nginx服务器与用户的网络连接。
events {
worker_connections 1024;
}
worker_connections 1024;
表示每个 work process 支持的最大连接数为 1024.
http块
http {
include mime.types;
default_type application/octet-stream;
client_max_body_size 1024m;
#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 {
listen 81;
server_name localhost;
#charset koi8-r;
#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;
# }
#}
server {
listen 9001;
server_name localhost;
location ~ /serviceedu/ {
proxy_pass http://localhost:8001;
}
location ~ /eduoss/ {
proxy_pass http://localhost:8002;
}
location ~ /eduvod/ {
proxy_pass http://localhost:8003;
}
location ~ /educms/ {
proxy_pass http://localhost:8004;
}
location ~ /edumsm/ {
proxy_pass http://localhost:8005;
}
location ~ /educenter/ {
proxy_pass http://localhost:8160;
}
location ~ /edeorder/ {
proxy_pass http://localhost:8007;
}
location ~ /staservice/ {
proxy_pass http://localhost:8008;
}
Nginx 服务器配置中最频繁的部分,代理、缓存和日志定义等绝大多数功能和第三方模块的配置都在这里。
http全局块
http全局块配置的指令包括文件引入、MIME-TYPE 定义、日志自定义、连接超时时间、单链接请求数上限等。
server块
全局 server 块
最常见的配置是本虚拟机主机的监听配置和本虚拟主机的名称或IP配置。
listen 9001;
server_name localhost;
location 块
一个 server 块可以配置多个 location 块。
这块的主要作用是基于 Nginx 服务器接收到的请求字符串(例如 server_name/uri-string),对虚拟主机名称 (也可以是IP 别名)之外的字符串(例如 前面的 /uri-string)进行匹配,对特定的请求进行处理。
location 指令说明
该指令用于匹配 URL。
语法如下:
1、= :用于不含正则表达式的 uri 前,要求请求字符串与 uri 严格匹配,如果匹配 成功,就停止继续向下搜索并立即处理该请求。
2、~:用于表示 uri 包含正则表达式,并且区分大小写。
3、~*:用于表示 uri 包含正则表达式,并且不区分大小写。
4、^~:用于不含正则表达式的 uri 前,要求 Nginx 服务器找到标识 uri 和请求字 符串匹配度最高的 location 后,立即使用此 location 处理请求,而不再使用 location 块中的正则 uri 和请求字符串做匹配。
注意:如果 uri 包含正则表达式,则必须要有 ~ 或者 ~*标识。
反向代理配置
location ~ /serviceedu/ {
proxy_pass http://localhost:8001;
}
location ~ /eduoss/ {
proxy_pass http://localhost:8002;
}
负载均衡配置
upstream myserver {
server 208.208.128.122:8081;
server 208.208.128.122:8082;
}
server {
listen 80;
server_name 208.208.128.122;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
proxy_pass http://myserver;
#proxy_pass http://127.0.0.1:8081;
index index.html index.htm;
}
负载均衡-nginx 分配服务器策略
-
轮询(默认)每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器 down 掉,能自动剔除。
-
weight
weight 代表权重, 默认为 1,权重越高被分配的客户端越多
upstream myserver { server 208.208.128.122:8081 weight=10; server 208.208.128.122:8082 weight=10; }
-
ip_hash 每个请求按访问 ip 的 hash 结果分配,这样每个访客固定访问一个后端服务器
upstream myserver {
ip_hash;
server 208.208.128.122:8081 ;
server 208.208.128.122:8082 ;
}
- fair(第三方) 按后端服务器的响应时间来分配请求,响应时间短的优先分配。
upstream myserver {
server 208.208.128.122:8081 ;
server 208.208.128.122:8082 ;
fair;
}
动静分离配置
动静分离从目前实现角度来讲大致分为两种:
- 一种是纯粹把静态文件独立成单独的域名,放在独立的服务器上,也是目前主流推崇的方案;
- 另外一种方法就是动态跟静态文件混合在一起发布,通过 nginx 来分开。
server {
listen 80;
server_name 208.208.128.122;
#charset koi8-r;
#access_log logs/host.access.log main;
location / www/{ #相当于静态资源
root /data/;
}
location /image/{
root /data/;
autoindex on; #列出目录内容
}
}
Nginx 的高可用集群
高可用集群,采用的是一主一备的模式,当主节点Nginx挂掉,备份服务器Nginx立刻跟上,这样就保证了服务的高可用性。需要安装 keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 208.208.128.122
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/usr/local/src/nginx_check.sh"
interval 2 #(检测脚本执行的间隔)
weight 2
}
vrrp_instance VI_1 {
state MASTER # 备份服务器上将 MASTER 改为 BACKUP
interface ens192 //网卡
virtual_router_id 51 # 主、备机的 virtual_router_id 必须相同
priority 90 # 主、备机取不同的优先级,主机值较大,备份机值较小
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
208.208.128.50 // VRRP H 虚拟地址
}
}
nginx原理
mater和worker
- nginx 启动后,是由两个进程组成的。master(管理者)和worker(工作者)。
- 一个nginx 只有一个master。但可以有多个worker
- 请求由master管理,worker进行争抢式的方式去获取请求。
好处
- 可以使用 nginx –s reload 热部署,利用 nginx 进行热部署操作
- 每个 worker 进程来说,独立的进程,不需要加锁。
- 采用独立的进程,可以让互相之间不会 影响
worker 数和服务器的 cpu 数相等是最为适宜
Nginx 同 redis 类似都采用了 io 多路复用机制,每个 worker 都是一个独立的进程,但每个进 程里只有一个主线程,通过异步非阻塞的方式来处理请求, 即使是千上万个请求也不在话 下。每个 worker 的线程可以把一个 cpu 的性能发挥到极致。所以 worker 数和服务器的 cpu 数相等是最为适宜的。设少了会浪费 cpu,设多了会造成 cpu 频繁切换上下文带来的损耗。
参考了文章https://blog.youkuaiyun.com/qq_40036754/article/details/102463099