Nginx 简介
Nginx是 高性能Web服务器
- 静态资源服务器:html,css,js,img,视频,音频
- 反向代理:代理客户访问 **内部 **(本机、局域网的服务器)服务
- 负载均衡:将请求(流量)分发给集群中的多台服务器,避免单点压力过大
- 网站(后端API)入口:根据访问请求的路径参数,进行分发实现动静分离
- Nginx架构:多进程+单线程架构、适合多核处理器
- 一个Master进程,N个Work进程
- Master监控所有的Work进程,旧的Work进程终止时,随时可以启动新的进程
- Work进程使用单线程接受请求处理请求,避免了线程切换带来的消耗
- IO多路复用技术:异步非阻塞架构,同时处理更多的请求
- 异步非阻塞是“任务完成了通知你”,IO 多路复用是“同时盯多个任务,看谁先完成”—— 两者结合,就是高性能服务器的核心逻辑。
Nginx安装
- apt/yum安装
#乌班图安装
apt update
apt install nginx
- 源码编译安装
Centos7安装
#1.rpm包安装yum -y install wget vim
# 下载nginx源
$ wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
# 安装nginx源
rpm -ivh nginx-*.rpm
# 正式安装nginx
yum -y install nginx
# 设置防火墙
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload
# 启动nginx
systemctl start nginx
# 开机启动nginx
systemctl enable nginx#2.安装依赖
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel# nginx依赖pcre库
wget http://192.168.57.200/Software/pcre-8.45.tar.bz2
或者 wget http://downloads.sourceforge.net/project/pcre/pcre/8.45/pcre-8.45.tar.gz
#解压
tar -xvf pcre-8.45.tar.bz2cd pcre-8.45/./configure
#make 负责将源代码编译成可用的程序或库,而 make install 负责将编译好的软件部署到系统中合适的位置,以便用户能够方便地使用和调用。
make && make install# 检验是否安装
pcre-config --version# 下载编译nginx
cd ..
wget http://192.168.57.200/Software/nginx-1.21.4.tar.gztar -xvf nginx-1.21.4.tar.gzcd nginx-1.21.4./configure --with-http_stub_status_module --with-http_ssl_module --with-pcre=../pcre-8.45make && make install# 安装成功后直接执行nginx启动
/usr/local/nginx/sbin/nginx
nginx 的常用命令
- nginx 启动nginx
- nginx -s stop 快速关闭Nginx,可能不保存相关信息,并迅速终止web服务。
- nginx -s quit 平稳关闭Nginx,保存相关信息,有安排的结束web服务。
- nginx -s reload 重新加载配置文件;因改变了Nginx相关配置,需要重新加载配置而重载。
- nginx -s reopen 重新打开日志文件。
- nginx -c filename 为 Nginx 指定一个配置文件,来代替缺省的。
- nginx -t 不运行,仅仅测试配置文件。nginx 将检查配置文件的语法的正确性,并尝试打开配置文件中所引用到的文件。
- nginx -v 显示 nginx 的版本。
- nginx -V 显示 nginx 的版本,编译器版本和配置参数。
使用系统服务管理工具管理nginx
vim /usr/lib/systemd/system/nginx.service[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true[Install]
WantedBy=multi-user.target#重新启动加载配置
systemctl daemon-reload
systemctl reload nginx
Nginx基本配置
nginx.conf
-
全局配置
#启动work进程的用户 user www-data; #启动worler进程的数量,auto 自动获取数量 worker_processes auto; #全局错误日志 error_lpg logs/error.log; #pid 进程号的路径 pid /run/nginx.pid;
- events模块配置
#性能优化配置 events { use epoll; # 使用 epoll 事件模型(Linux) worker_connections 10240; # 每个 worker 最大连接数 multi_accept on; # 一次性接受所有新连接 accept_mutex on; # 启用连接接收互斥锁 }
- http配置:所有HTTP请求与响应的配置
- sever配置:网站配置;可以配置多个(虚拟主机)
- location配置:网站路径配置
- sever配置:网站配置;可以配置多个(虚拟主机)
http {include /etc/nginx/mime.types; #引入加载外部模块,mime.types是文件格式;default_type application/octet-stream; #二进制流 MIME 类型,表示 “未知的二进制数据”。通常会将其视为 “需要下载的文件”#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; # 启用内核级文件传输,减少 CPU 消耗和延迟tcp_nopush on; # 配合 sendfile,合并小包发送keepalive_timeout 65; # 长连接超时时间 65 秒#gzip on; # 启动压缩,节省带宽,省流量,但是会占用cpu资源server {listen 80; #监听的端口server_name localhost; #绑定域名,一台服务器可以使用多个域名#charset koi8-r; #设置默认字符集#access_log logs/host.access.log main; #设置独立的访问日志路径 location / { #设置网站的根路径root html; #设置网站根目录对应的系统目录,默认nginx的安装路径下的html路径index index.html index.htm; #指定首页的名称}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}#location ~ .php$ {# proxy_pass http://127.0.0.1; #反向代理,将请求#}} }
-
搭建文件下载服务器
autoindex on;#不显示文件大小
autoindex_exact_size off;#显示文件时间
autoindex_localtime on;
- 虚拟主机
- 基于IP的虚拟主机
- 基于域名的虚拟主机
- 基于端口号的虚拟主机
- 代理与反向代理
- 负载均衡
- 优化配置
- LNMP环境搭建
- 动静分离
- Tomcat基础
- LNMT环境搭建