NGINX 揭秘
Nginx是一款轻量级的Web服务器、反向代理服务器,由于它的内存占用少,启动极快,高并发能力强,在互联网项目中广泛应用。
安装
一、安装编译工具及库文件
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
二、安装 pcre 依赖
[root@boyGdm src]# cd /usr/local/src/
[root@boyGdm src]# wget http://downloads.sourceforge.net/project/pcre/pcre/8.37/pcre-8.37.tar.gz
- 并解压文件
[root@boyGdm src]# tar –xvf pcre-8.37.tar.gz
- 进入安装包目录
[root@boyGdm src]# cd pcre-8.37
- 编译安装
[root@boyGdm pcre-8.37]# ./configure
[root@boyGdm pcre-8.37]# make && make install
- 查看pcre版本
[root@boyGdm pcre-8.35]# pcre-config --version
三、安装AGINX
创建 Nginx 运行使用的用户 www:
[root@boyGdm conf]# /usr/sbin/groupadd www
[root@boyGdm conf]# /usr/sbin/useradd -g www www
安装后,不能访问的,需要对防火墙进行设置
- 关闭防火墙&&防火墙自启
[root@boyGdm nginx ]# systemctl stop firewalld && systemctl disable firewalld
#安装Iptables管理工具&&启动Iptables&&设为Iptables开机自启&&清空Iptables规则&&保存Iptables默认规则
[root@boyGdm nginx ]# yum -y install iptables-services && systemctl start iptables && systemctl enable iptables&& iptables -F && service iptables save
(1) 查看开放的端口
[root@boyGdm nginx ]# firewall-cmd --list-all
(2) 设置开放的端口号
[root@boyGdm nginx ]# firewall-cmd --add-service=http –permanent
[root@boyGdm nginx ]# firewall-cmd --add-port=80/tcp --permanent
(3) 设置之后需要重启防火墙
[root@boyGdm nginx ]# firewall-cmd --reload
配置nginx.conf
[root@boyGdm conf]# cat /usr/local/webserver/nginx/conf/nginx.conf
user www www;
worker_processes 2; #设置值和CPU核心数一致
error_log /usr/local/webserver/nginx/logs/nginx_error.log crit; #日志位置和日志级别
pid /usr/local/webserver/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
use epoll;
worker_connections 65535; # work process 支持的最大连接数为 65535.
}
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';
#charset gb2312;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m;
#下面是server虚拟主机的配置
server
{
listen 80;#监听端口
server_name localhost;#域名
index index.html index.htm index.php;
root /usr/local/webserver/nginx/html;#站点目录
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
{
expires 30d;
# access_log off;
}
location ~ .*\.(js|css)?$
{
expires 15d;
# access_log off;
}
access_log off;
}
}
检查配置文件nginx.conf的正确性命令:
[root@boyGdm conf]# /usr/local/webserver/nginx/sbin/nginx -t
Nginx 操作命令
[root@boyGdm conf]# /usr/local/webserver/nginx/sbin/nginx # 启动命令
[root@boyGdm conf]# /usr/local/webserver/nginx/sbin/nginx -s reload # 重新载入配置文件
[root@boyGdm conf]# /usr/local/webserver/nginx/sbin/nginx -s reopen # 重启 Nginx
[root@boyGdm conf]# /usr/local/webserver/nginx/sbin/nginx -s stop # 停止 Nginx
代理
代理简单来说,就是如果我们想做什么,但又不想直接去做,那么这时候就找另外一个人帮我们去做。那么这个例子里面的中介公司就是给我们做代理服务的,我们委托中介公司帮我们找房子。 正向代理和反向代理的区别
正向代理代理客户端 proxy和client同属于一个域中,隐藏了客户端信息。
反向代理代理服务器 proxy和server同属于一个域中,隐藏了服务端信息。
在config配置
在nginx里面添加以下的信息。
server {
listen 80;
server_name www.example.com;
location / {
proxy_pass http://127.0.0.1:8080;
index index.html index.htm index.jsp;
}
}
server_name 配置项
# 对于name 来说,可以只有一个名称,也可以有多个名称,中间用空格隔开。而每个名字由两段或者三段组成,每段之间用'.'隔开。
server_name example.com www.example.com
# 可以使用通配符'*',但通配符只能用在由三段字符组成的首段或者尾端,或者由两端字符组成的尾端。
server_name *.example.com www.example.*
# 还可以使用正则表达式,用'~'作为正则表达式字符串的开始标记。
server_name ~^www\d+\.example\.com$;
location 配置
location [ = | ~ | ~* | ^~] uri {}
-
=
:用于不含正则表达式的 uri 前,要求请求字符串与 uri 严格匹配,如果匹配成功,就停止继续向下搜索并立即处理该请求。 -
~
:用于表示 uri 包含正则表达式,并且区分大小写。 -
~*
:用于表示 uri 包含正则表达式,并且不区分大小写。 -
^~
:用于不含正则表达式的 uri 前,要求 Nginx 服务器找到标识 uri 和请求字符串匹配度最高的 location 后,立即使用此 location 处理请求,而不再使用 location 块中的正则 uri 和请求字符串做匹配。
注意:如果 uri 包含正则表达式,则必须要有 ~ 或者 ~* 标识。
大致可以将规则简化为如下优先级:(location = uri ) > (location ^~ uri) > (location *| uri) > (location uri)
负载均衡
Nginx 服务器是介于客户端和服务器之间的中介,通过上一篇博客讲解的反向代理的功能,客户端发送的请求先经过 Nginx ,然后通过 Nginx 将请求根据相应的规则分发到相应的服务器。
nginx 分配服务器策略
第一种 轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器 down 掉,能自动剔除。
第二种 weight
weight 代表权重默认为 1,权重越高被分配的客户端越多
upstream server_pool {
server 192.168.2.153 weight 10;
server 192.168.2.155 weight 15;
}
第三种 ip_hash
每个请求按访问 ip 的 hash 结果分配,这样每个访客固定访问一个后端服务器
upstream server_pool {
ip_hash;
server 192.168.2.153;
server 192.168.2.155;
}
第四种 fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
upstream server_pool {
server 192.168.2.153;
server 192.168.2.155;
fair;
}
动静分离
Nginx 动静分离简单来说就是把动态跟静态请求分开,不能理解成只是单纯的把动态页面和静态页面物理分离
location /www/ {
root /data/;
index index.html index.htm;
}
location /image/ {
root /data/;
outoIndex on;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)