目录
概念:
Nginx是一个高性能的HTTP和反向代理服务器,特点是占用内存少,并发能力强,事实上Nginx的并发能力确实在同类型的网页服务器中表现好。有报告表明能支持高达50000个并发连接数。
常用命令
找到nginx路径: which nginx 或 whereis nginx 如:/usr/sbin/nginx
进入/usr/sbin执行{上述命令找的路径} ./nginx -v 查看版本
./nginx -s stop 快速停止或关闭 ./nginx -s quit 正常停止或关闭【进程完成后关闭】
./nginx 自启动
./nginx -s reload 重启加载nginx[一般改完nginx.conf配置文件后]
netstat -ntlp 查看nginx服务是否允许
/usr/sbin/nginx -t 或 ./nginx -t 查看配置文件所在目录 nginx -t 也可以[本质都是找到nginx执行命令]
nginx -c /usr/local/nginx/conf/nginx.conf 利用配置文件启动nginx 【看自己的conf文件路径】
service nginx restart 重启服务
ps -ef | grep nginx 是否安装了nginx
tail -f /data/log/nginx/access.log
打印实时日志
systemctl 为linux系统应用管理工具systemd的主命令,用来管理系统
systemctl start nginx # 启动 Nginx
systemctl stop nginx # 停止 Nginx
systemctl restart nginx # 重启 Nginx
systemctl reload nginx # 重新加载 Nginx,用于修改配置后
systemctl enable nginx # 设置开机启动 Nginx
systemctl disable nginx # 关闭开机启动 Nginx
systemctl status nginx # 查看 Nginx 运行状态
nginx配置文件
三部分组成
1.全局块 全局生效的配置区
worker_processes 1; # 服务器并发处理能力,值越大并发能力越强(受自身配置限制)
2.events块 影响nginx和用户网络连接
worker_connections 1024; #最大连接数1024个,需灵活配置
3.http块 重点区域,配置频繁 [代理、缓存、第三方等]
可以嵌套多个server(每个server为一个站点),配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。
结构图概述
main # 全局配置,对全局生效
├── events # 配置影响 Nginx 服务器或与用户的网络连接
├── http # 配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置
│ ├── upstream # 配置后端服务器具体地址,负载均衡配置不可或缺的部分
│ ├── server # 配置虚拟主机的相关参数,一个 http 块中可以有多个 server 块
│ ├── server
│ │ ├── location # server 块可以包含多个 location 块,location 指令用于匹配 uri
│ │ ├── location
│ │ └── ...
│ └── ...
└── ...语法:
每条指令以
;
分号结尾,指令与参数间以空格符号分隔指令块以
{}
大括号将多条指令组织在一起
include
语句允许组合多个配置文件以提升可维护性;
#
符号添加注释,提高可读性$
符号使用变量
配置详解【了解】
### 每个指令必须有分号结束,用#号注释, 注释部分为可选项,未注释的为必须的 ###
# main全局块:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。
# 全局块
------------------------------------------------------------------------------
#user nobody;
worker_processes 1; # #允许生成的进程数,默认为1, 最大为cpu核数或者cup核数的两倍
#制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid; #指定nginx进程运行文件存放地址
#最大文件打开数(连接),可设置为系统优化后的ulimit -HSn的结果
#worker_rlimit_nofile 51200;
#cpu亲和力配置,让不同的进程使用不同的cpu
#worker_cpu_affinity 0001 0010 0100 1000 0001 00100100 1000
------------------------------------------------------------------------------
# events块
events {
worker_connections 1024; #单个后台worker process进程的最大并发链接数
#use epoll; #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能
#accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on
#multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
}
# http块
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; #允许sendfile方式传输文件,默认为on,表示高效文件传输模式,可以在http块,server块,location块
#sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限
#tcp_nopush on; #激活tcp_nopush参数可以允许把httpresponse header和文件的开始放在一个文件里发布,积极的作用是减少网络报文段的数量
#keepalive_timeout 0; #设置长连接超时时间,默认为75s,可以在http,server,location块
keepalive_timeout 65;
#gzip on; 开启压缩
#gzip_min_length 1k; #gzip on开启才有效,设置允许压缩的页面最小字节数,页面字节数从header头的Content-Length中获取。默认值是0,表示不管页面多大都进行压缩
# gzip_http_version 1.1; 只针对http1.1压缩
# gzip_types text/plain; 压缩类型为文本
# gzip_comp_level 9;压缩等级为9
#upstream表示负载服务器池 --反向代理使用
# upstream mytest{
# server 118.24.241.124 [weight=1 max_fails=2 fail_timeout=30s];
# server 106.12.2.195:8081 [weight=1 max_fails=2 fail_timeout=30s];
# server 106.12.2.195:8082 [weight=1 max_fails=2 fail_timeout=30s];
# server 106.12.2.195:8083 [weight=1 max_fails=2 fail_timeout=30s];
# }
------------------------------------------------------------------------------
# server块 一个虚拟主机==一个server块【一个http块可多个server块】
server { # 全局server块 + location块
# server全局块
listen 80; # 监听的端口号
server_name localhost; # 监听的主机名称 如:147.147.2.236
server_name data_system; # 此为我们访问的域名[开发中一般用域名非实际ip]
#charset koi8-r;
#access_log logs/host.access.log main;
# location块
location / { # 默认首页
root html;
index index.html index.htm;
proxy_pass http://192.168.8.123:6060;# 域名转发后,实际访问的ip+port
# 注意:服务器不解析data_system/域名,需添加到DNS服务器,可加入/etc/hosts文件【如下内容】
# 147.147.2.236 data_system
# 测试:curl 'http://data_system/anus/demo'
}
location / {
# proxy_pass http://mytest; proxy_pass #请求转向mytest定义的服务器列表, 用于负载均衡,如果开启了,那么此处自己的站点就不能访问了
root /home/wwwroot/qinser;
index index.php 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;
#}
}
# 可以配置多个server块
}
#每个站点都需要配置一个server模块,为了方便管理,这里把每个站点的配置文件统一放入到./vhost/目录下,并统一使用 .conf为后缀。
include /usr/local/nginx/conf/test/*.conf 【不想多个server在一个nginx.conf中,配置多个后此方法类似spring中配置文件】
注意:配置完成 重新加载 ./nginx -s reload 或者直接 绝对路径XX/nginx -s reload
Nginx配置文件(nginx.conf)配置详解_nginx配置文件详解_Java高知社区的博客-CSDN博客
配置正向代理
配置反向代理
正向反向区别【本质都是A/B中间夹个C】
正向代理即是客户端代理, 代理客户端, 服务端不知道实际发起请求的客户端. 黄牛帮你买票
反向代理即是服务端代理, 代理服务端, 客户端不知道实际提供服务的服务端 中介帮你租房
负载均衡
单个服务器解决不了,我们增加服务器的数量,然后将请求分发到各个服务器上 即:将原先请求集中到单个服务器上的情况改为将请求分发到多个服务器上,将负载分发到不同的服务器。
负载均衡的分配策略
1.轮询 按照请求时间逐一分配,默认。服务器挂了一台,会自动剔除,不再转发请求
2.weight /权重 值越大权重越大,分配到的几率就大 【后端服务器性能不均时,考虑】
upstream mytest{
server 127.0.0.1:8080 weight=5;
server 127.0.0.1:8081 weight=10; # 容易获取更多请求
}
3.ip_hash 请求ip的hash值分配。客户一直访问一个后端服务器,解决session问题
upstream mytest{
ip_hash; #就加个它
server 127.0.0.1:8080;
server 127.0.0.1:8081;
}
4.fair/第三方 后端响应快的/时间短 就获取请求多
upstream mytest{
server 127.0.0.1:8080;
server 127.0.0.1:8081;
fair;
}
动静分离
动态请求和静态请求分开,不是将动态页面和静态页面物理分离,可以理解为nginx处理静态页面,tomcat处理动态页面
大概两种
一、纯粹将静态文件放在独立的服务器上,也是目前主流方案;
二、将动态跟静态文件混合在一起发布,通过nginx分开。
location有两种匹配规则:
匹配URL类型,有四种参数可选,当然也可以不带参数。
location [ = | ~ | ~* | ^~ ] uri { … }
命名location,用@标识,类似于定于goto语句块。
location @name { … }
*** 参数解释
(1) “=” ,精确匹配
location = /abc/ {
…
}只匹配http://abc.com/abc
#http://abc.com/abc [匹配成功]
#http://abc.com/abc/index [匹配失败]
location /images
和location /images/
是有区别前者:以
/images
开头的所有请求 匹配 /images/example.jpg后者:以
/images结尾
的所有请求 匹配 XX/images(2) “~”,执行正则匹配,区分大小写。
location ~ /Abc/ {
…
}
#http://abc.com/Abc/ [匹配成功]
#http://abc.com/abc/ [匹配失败](3)“~*”,执行正则匹配,忽略大小写
location ~* /Abc/ {
…
}则会忽略 uri 部分的大小写
#http://abc.com/Abc/ [匹配成功]
#http://abc.com/abc/ [匹配成功](4)“^~”,表示普通字符串匹配上以后不再进行正则匹配。
location ^~ /index/ {
…
}
#以 /index/ 开头的请求,都会匹配上
#http://abc.com/index/index.page [匹配成功]
#http://abc.com/error/error.page [匹配失败](5)不加任何规则时,默认是大小写敏感,前缀匹配,相当于加了“~”与“^~”
location /index/ {
…
}
#http://abc.com/index [匹配成功]
#http://abc.com/index/index.page [匹配成功]
#http://abc.com/test/index [匹配失败]
#http://abc.com/Index [匹配失败]匹配到所有uri
(6)“@”,nginx内部跳转
location /index/ {
error_page 404 @index_error;
}
location @index_error {
…
}
#以 /index/ 开头的请求,如果链接的状态为 404。则会匹配到 @index_error 这条规则上。5.4 匹配顺序
= > ^~ > ~ | ~* > 最长前缀匹配 > /
静态实际举例:
location /images { root /var/www; try_files $uri /fallback.jpg; gzip_static on; } # 路径为 /images/example.jpg 时 # Nginx将会寻找 /var/www/images/example.jpg # 文件不存在,则会返回 /fallback.jpg # 启用 gzip 压缩
nginx开启压缩提高页面加载速度
Nginx开启Gzip压缩大幅提高页面加载速度的方法_nginx_脚本之家
nginx的location规则
nginx的配置详解_nginx 配置详解_头发保卫者的博客-CSDN博客
推荐一个博主Nginx 从入门到实践,万字详解! - 掘金