Nginx简介、安装及部署
简介
企业常见web服务器
Apache : http://apache.orgNginx: http://nginx.orgISS: http://www.iss.netWeblogic:https://www.oracle.com/java/weblogic/
web服务器排名rankhttps://w3techs.com/technologies/cross/web_server/ranking
-
https://nginx.org/en/download.html Nginx的优势及特点
高性能,处理并发能力强(IO模型,Nginx =epoll模型, apache =select模型)具备高拓展性:Nginx模块化的程序,Nginx可以根据需要进行模块拓展高可靠性:高可用,Nginx具备高可用机制,保证服务三个9,4个9,五个9的运维指标支持热部署:Nginx平滑升级,不影响用户访问
Nginx应用场景
web服务器:实现用户访问请求处理负载均衡:实现集群化管理,请求分发代理缓存:可以将动态请求的资源在本地进行预缓存,提升访问速度安全服务:针对用户访问进行权限控制,https加密处理的安全认证,结合lua语言实现wal防护(cc攻击,sql注入)
Nginx访问网站请求方式
1.基于ip访问2.基于域名访问3.基于ip+端口4.基于域名+端口
浏览器输入域名:www.xufary.com
浏览器将请求添加协议
针对域名进行解析(DNS解析:将域名解析为IP的过程)
发起TCP请求,发送http请求报文
服务端nginx通过端口收到http请求报文后,根据server模块的server_name匹配
交由location匹配uri,处理响应规则
调用内核处理后返回给用户
安装
安装依赖
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
解压缩
tar -zxvf nginx-1.20.1.tar.gz //解压缩
执行配置
cd nginx-1.20.1/gzip压缩 ./configure PREFIX=/usr/local/nginx --with-http_ssl_module --with-http_stub_module --with-http_gzip_static_module# 编译安装(默认安装在/usr/local/nginx)makemake install
修改nginx.Config主配置文件
location / {root /zhfi/football/apps/www/dist;index index.html index.htm;}location ^~/prod-api/ {rewrite ^/prod-api/(.*)$ /$1 break; # 重写路径将 api 替换为空proxy_pass http://localhost:8080;}
常见命令
./usr/local/nginx/sbin/nginx # 启动./usr/local/nginx/sbin/nginx -t # 校验主配置文件是否正确./usr/local/nginx/sbin/nginx -s reload # 重新加载主配置文件./usr/local/nginx/sbin/nginx -V # 查看版本./usr/local/nginx/conf.d # nginx子配置w文件目录./usr/local/nginx/conf.d/default.conf # nginx子配置默认文件
yum自动安装
sudo yum install yum-utils# 创建文件 /etc/yum.repos.d/nginx.repo 填充如下内容[nginx-stable]name=nginx stable repobaseurl=http://nginx.org/packages/centos/$releasever/$basearch/gpgcheck=1enabled=1gpgkey=https://nginx.org/keys/nginx_signing.keymodule_hotfixes=true[nginx-mainline]name=nginx mainline repobaseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/gpgcheck=1enabled=0gpgkey=https://nginx.org/keys/nginx_signing.keymodule_hotfixes=true
sudo yum-config-manager --enable nginx-mainlinesudo yum install nginx
Nginx主配置文件详解
#user nobody; #指定nginx服务运行用户worker_processes 1; #指定nginx的worker进程数量#pid logs/nginx.pid; #指定nginx PID进程号文件events {worker_connections 1024; #指定nginx当前一个worker进程同事可以处理最大的连接数量}http {include mime.types; #include引用某文件的内容default_type application/octet-stream; #当nginx无法识别当前访问页面内容时触发下载动作# 日志格式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; #指定nginx访问日志的位置sendfile on;#tcp_nopush on;keepalive_timeout 65; # nginx建立TCP连接后,多长时间没动作,自动断开时间#gzip on;server {listen 80; #指定监听端口server_name localhost; # 指定当前网站访问域名 www.football.com 或配置upstream名称 实现轮询负载均衡#charset koi8-r; #字符集#access_log logs/host.access.log main;location / { # 匹配uri# autoindex on; 开始list列表浏览下载功能root /zhfi/football/apps/www/dist; #匹配后的资源路径index index.html index.htm; #默认返回资源路径下的页面}# 开启443端口,https浏览器默认443端口,# 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;# }#}}
location匹配原则
= 进行uri的精确匹配~ 进行uri的模糊匹配(区分大小写)~* 进行uri的模糊匹配(不区分大小写)^~ 进行uri的优先匹配匹配顺序 ^~ = 其他
Alias 别名使用,目的访问/a 实际上访问的是/b
location /a {alias /b;}
配置网页跳转功能
return code; 跳转状态码return url;
rewrite
应用场景:https跳转、类似alias、安全、针对seo的url优化
rewirte跳转方式:
break 将访问的url直接跳转,不会重新发起请求last 会重新发起请求,再次匹配locationpermanent 301 永久跳转 会将跳转过程保存至浏览器中redirect 302 临时跳转 不会保存跳转过程
302会用到http跳转https 因为后缀不会变化,基本上uri变化都不使用302
证书 自签发证书 生成秘钥对
(umask 077;openssl genrsa -out private.pem 4096)openssl rsa in private.pem -pubout -out key.pemopenssl req -new -x599 -key private.pem -out ca.crt -days 36500
配置ssl如下
server {listen 443 ssl;server_name www.xufary.com;root /html/xufary;index index.html;ssl_certificate /ect/nginx/pk/ca.crt;ssl_certificate_key /ect/nginx/pk/private.prm;}
反向代理
| 支持的协议 |
提供的功能 |
所需的功能模块 |
| TCP、HTTP、HTTPS、SOCKET | 通过proxy_pass代理到http_server 通过fastcgi_pass代理到php_server(LNMP) 通过uwsgi_pass代理到uwsgi服务器 |
ngx_http_proxy_module ngx_http_fastcgi_module ngx_http_uwsgi_module |
| GRPC |
通过grpc |
ngx_http_grpc_module |
| pop3/imap,rtmp |
实现邮件、流媒体 |
Nginx负载均衡实现
语法结构
upstream backend{server backend1.temp.com:10000 weight=5;server backend2.temp.com:10000 ;server backup1.temp.com:10000 backup;server backup2.temp.com:10000 backup;}server{listen 10000;proxy_pass backend;}
