Linux系统nginx(一)
一、Nginx 概述
Nginx 是一款开源、高性能、高可靠的 Web 服务器和反向代理服务器,具备诸多优秀特性,使其在互联网领域得到广泛应用。
1.1 Nginx 特点
- 高并发、高性能,能支持高达 5w 个并发连接数。
- 模块化架构,扩展性极佳。
- 采用异步非阻塞的事件驱动模型(epoll),与 Node.js 相似。
- 高可靠性,可连续运行数月甚至更久无需重启,还能实现热部署、平滑升级。
- 完全开源,生态繁荣,配置使用相对简单。
1.2 Nginx 作用
- http 服务器:可独立提供 http 服务,作为网页静态服务器。
- 虚拟主机:能在一台服务器上虚拟出多个虚拟服务器。
- 反向代理与负载均衡:当网站访问量增大,单台服务器无法满足需求时,可用 Nginx 做反向代理,实现多台服务器集群的负载均衡,避免服务器负载不均。
- 安全管理:可搭建 API 接口网关,对每个接口服务进行拦截。
Nginx 的具体作用还可从以下表格清晰看出:
静态服务 | 代理服务 | 安全服务 | 流行架构 |
---|---|---|---|
浏览器缓存 | 协议类型 | 访问控制 | Nginx+PHP(Fastcgi_pass)LNMP |
防资源盗用 | 正向代理 | 访问限制 | Nginx+Java(Proxy_Pass)LNMT |
资源分类 | 反向代理 | 流量限制 | Nginx+Python(uwsgi_pass) |
资源压缩 | 负载均衡 | 拦截攻击 | |
资源缓存 | 代理缓存 | 拦截异常请求 | |
跨域访问 | 动静分离 | 拦截 SQL 注入 |
1.3 Nginx 工作原理
Nginx 采用特定的工作机制来处理请求,其核心是异步非阻塞的事件驱动模型,能高效地处理大量并发连接,确保服务的稳定运行。
二、Nginx 服务搭建
2.1 Nginx 安装
2.1.1 yum 安装
shell
[root@nginx1~]# yum install -y epel-release ##可选
[root@nginx1~]# yum install -y nginx
##验证安装结果
[root@nginx1~]# rpm -q nginx
nginx-1.20.1-7.el7.x86_64
2.1.2 编译安装
shell
[root@nginx1 ~]# tar xf nginx-1.18.0.tar.gz
##安装依赖###
[root@nginx1 ~]# yum install -y pcre-devel
[root@nginx1 ~]# yum install -y zlib-devel
[root@nginx1 ~]# cd nginx-1.18.0/
[root@nginx1 nginx-1.18.0]# ./configure --prefix=/usr/local/nginx1.8 && make && make install
###命令优化
[root@localhost sbin]# export PATH=$PATH:/usr/local/nginx25/sbin #追加到/erc/profile
[root@localhost sbin]# source /etc/profile
##或者
[root@localhost sbin]# ln -s /usr/local/nginx25/sbin/nginx /usr/sbin/
###启停脚本优化
[root@localhost ~]# cat /etc/init.d/nginx
#!/bin/bash
case $1 in
start)#startnginx -c /usr/local/nginx25/conf/nginx.conf
;;
stop)#stopnginx -s stop
;;
restart)#stop #startnginx -s reopen
;;
reload)#reloadnginx -s reload
;;
status)#reloadnetstat -anptu | grep nginx
;;
*)echo "USAGE: $0 start | stop | restart | reload | status"
;;
esac
2.2 目录结构
2.2.1 yum 安装
shell
/etc/nginx/ ##配置文件目录
/var/lib/nginx ##临时数据文件目录
/var/log/nginx/ ##日志文件目录
/usr/share/nginx/html/ ##访问页面根目录
/etc/nginx/conf.d ##自定义配置文件目录
/etc/nginx/default.d ##默认配置文件目录
2.2.2 编译安装
shell
/usr/local/nginx1.8/conf ##配置文件目录
/usr/local/nginx1.8/conf/conf.d ##自定义配置文件目录
/usr/local/nginx1.8/conf/default.d ##默认配置文件目录
/usr/local/nginx1.8/html ##访问页面根目录
/usr/local/nginx1.8/logs ##日志文件目录
/usr/local/nginx1.8/sbin ##命令存放目录
2.3 核心配置文件
核心配置文件众多,各自有着不同的作用:
配置文件名称 | 配置文件作用 |
---|---|
fastcgi.conf | 包含 FastCGI 相关配置,用于与 FastCGI 进程通信 |
fastcgi.conf.default | fastcgi.conf 的备份副本 |
fastcgi_params | 包含用于 FastCGI 的参数配置,包括传输协议、请求超时时间等 |
fastcgi_params.default | fastcgi_params 的备份副本 |
koi-utf | 包含 UTF-8 编码与 KOI8-R 编码之间的字符转换规则,处理中文文件名等问题 |
koi-win | 包含 Windows 系统的字符转换规则,处理 Windows 系统的文件名问题 |
mime.types | 包含 Nginx 支持的 MIME 类型配置,用于设置相应的 Content-Type 头 |
mime.types.default | mime.types 的备份副本 |
nginx.conf | Nginx 的主要配置文件,包含所有全局配置和访问控制规则,是入口文件 |
nginx.conf.default | nginx.conf 的备份副本 |
scgi_params | 包含用于 SCGI 协议的参数配置 |
scgi_params.default | scgi_params 的备份副本 |
uwsgi_params | 包含用于 uWSGI 协议的参数配置 |
uwsgi_params.default | uwsgi_params 的备份副本 |
win-utf | 包含 Windows 系统的字符转换规则,处理 Windows 系统的文件名问题 |
其中,nginx.conf 是核心中的核心,其配置内容涵盖全局配置、events 配置、http 配置等多个方面,具体如下:
shell
##全局配置,对全局生效##
user nobody nobody; # 指定运行 Nginx 进程的用户为 nobody,组为nobody
pid /var/run/nginx.pid # master主进程的的pid存放在nginx.pid的文件
worker_processes 1; # 指定 Nginx 启动的 worker 子进程数量。
#worker_processes auto; # 与当前cpu物理核心数一致
worker_rlimit_nofile 20480; # 指定 worker 子进程可以打开的最大文件句柄数。
worker_rlimit_core 50M; # 指定 worker 子进程异常终止后的 core 文件,用于记录分析问题。
working_directory /opt/nginx/tmp; # 存放目录
worker_priority -10; # 指定 worker 子进程的 nice 值,调整运行优先级,通常为负值
#Linux 默认进程的优先级值是120,值越小越优先;nice 定范围为 -20 到 +19 。
#应用的默认优先级值是120加上 nice 值等于它最终的值,这个值越小,优先级越高。
worker_shutdown_timeout 5s; #指定 worker 子进程优雅退出时的超时时间。
timer_resolution 100ms; #worker 子进程内部计时器精度,间隔越大系统调用越少,利于性能提升
daemon on; # 指定 Nginx 的运行方式,前台用于调试,后台用于生产,默认on
error_log logs/error.log; # 错误日志文件路径##events:配置影响 Nginx 服务器与用户的网络连接;##
events {use epoll; # 使用epoll的I/O模型(自动选择最适合操作系统的)worker_connections 1024; # 允许的最大并发连接数accept_mutex on; # 打开负载均衡互斥锁,推荐打开
}
##http:配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置;##
http {include mime.types; # 包含 MIME 类型的定义,文件扩展名与类型映射表default_type application/octet-stream; # 默认文件类型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; # 启用零拷贝传输,高效传输模式tcp_nopush on; # 启用 TCP nopush 选项,减少网络报文段的数量keepalive_timeout 0; # 禁用持久连接的超时时间keepalive_timeout 65; # 保持存活连接的超时时间gzip on; # 开启 Gzip 压缩include /etc/nginx/conf.d/*.conf; # 加载自定义配置项##upstream:配置后端服务器具体地址,负载均衡配置不可或缺的部分。##upstream back_end_server{server 192.168.100.33:8081 #定义后端web服务器节点}##server:配置虚拟主机的相关参数,一个 http 块中可以有多个 server 块;##server {listen 80; # 监听端口 80server_name localhost; # 服务器名为 localhostcharset koi8-r; # 字符集设置为 koi8-raccess_log logs/host.access.log main; # 主机访问日志文件及使用的日志格式##location:用于配置匹配的 uri ;##location / {root html; # 指定静态资源目录位置,可在多个配置中设置index index.html index.htm; # 默认的索引文件deny 172.168.22.11; # 禁止访问的ip地址,可以为allallow 172.168.33.44;# 允许访问的ip地址,可以为all}location /image {alias /opt/nginx/static/image/;#指定静态资源目录位置,末尾需加 / ,仅能在location中}
#当用户访问 www.jx.com/image/1.png 时,实际在服务器找的路径是 /opt/nginx/static/image/1.pngerror_page 404 /404.html; # 设置 404 错误页面的位置为 /404.htmlerror_page 500 502 503 504 /50x.html; # 将服务器错误页面重定向到 /50x.htmllocation = /50x.html {root html;}location ~ \.php$ {proxy_pass http://127.0.0.1; # 将 PHP 脚本代理到监听在 127.0.0.1:80 上的 Apache 服务器}location ~ \.php$ {root html; # PHP 脚本位置fastcgi_pass 127.0.0.1:9000; # 向 FastCGI 服务器传递 PHP 脚本fastcgi_index index.php; # 指定 FastCGI 服务器默认的脚本文件名fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # FastCGI 参数配置include fastcgi_params; # 包含 FastCGI 相关的参数配置}location ~ /\.ht {deny all; # 阻止访问 .htaccess 文件}}# 其他 server 配置...
}
2.4 核心命令
命令 | 作用 |
---|---|
systemctl enable nginx | 开机自动启动 |
systemctl disable nginx | 关闭开机自动启动 |
systemctl start nginx | 启动 Nginx |
systemctl stop nginx | 停止 Nginx |
systemctl restart nginx | 重启 Nginx |
systemctl reload nginx | 重新加载 Nginx |
systemctl status nginx | 查看 Nginx 运行状态 |
ps -elf | grep [n]ginx | 查看 Nginx 进程,不显示 grep 本身的进程 |
kill -9 pid | 根据进程号杀死 Nginx 进程,-9 表示强制结束 |
nginx -s reload | 向主进程发送信号,重新加载配置文件,热重启 |
nginx -s reopen | 重启 Nginx |
nginx -s stop | 快速关闭 |
nginx -s quit | 等待工作进程处理完成后关闭 |
nginx -T | 查看当前 Nginx 最终的配置 |
nginx -t | 检查配置是否有问题 |
nginx -c configfilePath | 指定配置文件启动 nginx |
2.5 Nginx 信号
信号名 | 含义 |
---|---|
stop | 直接停止 |
quit | 优雅的退出:有人在访问不会结束进程 |
reopen | 分割日志 |
reload | 重新加载配置文件 |
term | 快速停止 nginx 进程,可能会中断现有连接,与 stop 信号类似。 |
usr1 | 重新打开日志文件,用于日志切割或日志重定向,与 reopen 信号类似。 |
usr2 | 平滑地升级 nginx 可执行文件。 |
hup | 重新加载配置文件,优雅地应用新配置,与 reload 信号类似。 |
winch | 当 nginx 以 master/worker 工作模式运行时,重新生成 worker 进程以适应新的配置。 |
usr3 | 向 worker 进程发送自定义信号。 |
三、配置案例
3.1 单站点配置
安装完毕启动 nginx 服务即可,配置文件示例如下:
shell
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;include /usr/share/nginx/modules/*.conf;events {worker_connections 1024;
}http {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;tcp_nodelay on;keepalive_timeout 65;types_hash_max_size 4096;include /etc/nginx/mime.types;default_type application/octet-stream;include /etc/nginx/conf.d/*.conf;server {listen 80;listen [::]:80;server_name _;root /usr/share/nginx/html;# Load configuration files for the default server block.include /etc/nginx/default.d/*.conf;error_page 404 /404.html;location = /404.html {}error_page 500 502 503 504 /50x.html;location = /50x.html {}}
}
3.2 虚拟主机配置
虚拟主机配置有基于 IP 地址、域名和端口号三种方式。
3.2.1 基于 IP 地址
通过不同的 IP 地址来区分不同的虚拟主机,配置示例如下:
shell
# 部分配置省略
server {listen 192.168.115.111:80;server_name _;# 其他配置...
}
server {listen 192.168.115.114:80;server_name _;# 其他配置...
}
3.2.2 基于域名
通过不同的域名来区分,配置示例:
shell
# 部分配置省略
server {listen 192.168.115.111:80;server_name www1.jx.com;# 其他配置...
}
server {listen 192.168.115.114:80;server_name www2.jx.com;# 其他配置...
}
3.2.3 基于端口号
通过不同的端口来区分,配置示例:
shell
# 部分配置省略
server {listen 192.168.115.111:80;server_name www1.jx.com;# 其他配置...
}
server {listen 192.168.115.114:81;server_name www2.jx.com;# 其他配置...
}