前言
nginx 是一款高性能的 web 服务器,就像网站的“大门守卫”——不仅能高效接收用户的访问请求,还能灵活分配资源、保障服务稳定。对于运维和开发来说,学会 nginx 的配置和虚拟主机搭建,是实现多站点部署、优化服务性能的关键。
这篇文章会从 nginx 的核心配置文件入手,一步步拆解每个参数的作用,再详细讲解虚拟主机的三种搭建方案和实用安全功能。内容通俗易懂,每个步骤都附带具体操作和效果说明,无论是新手还是有基础的用户,都能跟着落地实践。
一、nginx 配置文件
nginx 的所有功能都依赖主配置文件 nginx.conf,它的路径通常是 /usr/local/nginx/conf/nginx.conf。我们可以用 vim /usr/local/nginx/conf/nginx.conf 命令编辑它,核心配置分为五大模块,每个模块各司其职。
1.1 全局配置
全局配置是 nginx 的“整体设置”,位于配置文件最外层,不包含在任何 http“server”或“location”块内,直接影响服务器的整体运行。
#user nobody; # 运行用户,编译时未指定则默认是 nobody,可改为 nginx worker_processes 4; # 工作进程数量,建议设为服务器内核数×2,访问量小时设1即可 #error_log logs/error.log; # 错误日志存放路径,默认在 logs 目录下 #pid logs/nginx.pid; # pid 文件路径,记录 nginx 进程 id
- worker_processes:相当于“干活的工人数量”,数量越多,能同时处理的请求越多。比如服务器是 2 核 cpu,设为 4 就很合适,既不浪费资源也不拥挤。
- error_log:记录 nginx 运行中的错误,比如配置出错、服务异常,排查问题时全靠它。
- pid:记录 nginx 主进程的 id,方便管理服务(比如重启、停止)。
1.2 i/o 事件配置
i/o 事件配置负责优化 nginx 处理网络连接的方式,直接决定服务的并发能力,核心是 events 块内的参数。
events {
use epoll; # 使用 epoll 模型,linux 2.6 及以上内核优先选它
worker_connections 4096; # 每个工作进程最多处理 4096 个连接
}- use epoll:epoll 是 linux 系统的高效 i/o 模型,能在大量并发连接中快速响应活跃请求,比默认模型节省 cpu 资源。
- worker_connections:每个“工人”能同时接待的“客户数”。如果有 8 个工作进程,总并发连接数就是 4096×8=32768(实际还要看服务器硬件和带宽)。
- 补充操作:要让并发连接数生效,需执行
ulimit -n 65535临时修改系统限制(每个 tcp 连接对应一个文件句柄,默认限制可能不够)。
1.3 http 配置
http 配置是 nginx 的“核心功能区”,用 http { } 包裹,控制所有 web 服务的基础行为,大部分配置会嵌套在子块 server { } 中。
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; # 开启高效文件传输模式,提升下载速度
#tcp_nopush on; # 配合 sendfile 使用,发送前先缓存数据
keepalive_timeout 65; # 连接保持超时时间,65秒内无请求则断开
#gzip on; # 开启 gzip 压缩,减小传输数据量(可提升页面加载速度)
}- include mime.types:告诉 nginx 不同后缀的文件是什么类型,比如
.html是网页文件,.jpg是图片文件,避免浏览器识别错误。 - sendfile on:相当于“快递加急”,直接通过内核传输文件,不用经过应用层,传输速度更快。
- keepalive_timeout:用户打开网页后,短时间内再次点击链接不用重新建立连接,节省时间和资源。
1.4 web 服务的监听配置
每个 server { } 块代表一个独立的 web 服务(或虚拟主机),负责监听特定的端口和域名,处理对应的请求。
server {
listen 80; # 监听的端口(默认 http 端口是 80)
server_name www.kgc.com; # 绑定的域名,可填多个(用空格分隔)
charset utf-8; # 网页默认字符集,避免中文乱码
# 核心请求处理规则
location / {
root html; # 网页文件存放的根目录(默认是 /usr/local/nginx/html)
index index.html index.php; # 默认首页文件名,优先加载 index.html
}
# 错误页面配置(500/502/503/504 是服务器错误)
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html; # 错误页面存放目录
}
}- listen 80:用户访问网站时不用输端口(浏览器默认访问 80 端口),如果改写成 8080,访问时就要加
:8080(比如http://www.kgc.com:8080)。 - server_name:相当于网站的“门牌号”,比如
www.kgc.com,用户输入这个域名就能找到对应的服务。 - location /:匹配所有请求,
root指定网页文件存在哪里,index是打开网站时默认显示的文件。
1.5 其他配置(扩展)
这部分是实用扩展配置,主要包括日志格式详解和 location 核心指令区别,帮你解决实际配置中的“坑”。
日志格式变量说明
日志格式中的每个变量都有特定含义,解开 log_format 注释后,访问日志会记录这些信息:
$remote_addr:客户端的 ip 地址(比如用户的电脑 ip)。$remote_user:访问网站的用户名(如果开启了认证,会记录登录名)。$time_local:访问时间和时区(比如10/oct/2025:13:55:36 +0800)。$request:用户的请求信息(比如get /index.html http/1.1,表示请求首页)。$status:请求状态码(比如 200 是成功,404 是找不到页面)。$http_user_agent:用户的浏览器信息(比如 chrome、firefox)。
location 三大指令区别
root、alias、proxy_pass 是 location 中最常用的指令,用法差异很大,用例子说明更清楚:
- root(根路径配置):比如
root /var/www/html,当用户请求www.kgc.com/test/1.html时,nginx 会去/var/www/html/test/1.html找文件(拼接请求路径)。 - alias(别名配置):比如
alias /var/www/html,当用户请求www.kgc.com/test/1.html时,nginx 会直接去/var/www/html/1.html找文件(不拼接路径)。 - proxy_pass(反向代理配置):比如
proxy_pass http://127.0.0.1:8080,会把用户的请求转发到本地 8080 端口的服务(比如 tomcat),适合动态页面处理。
二、nginx 虚拟主机
虚拟主机就像“服务器里的隔间”,能让一台物理服务器同时运行多个网站(比如同时部署 www.yjs.com 和 www.simoncwh.com),不用额外买服务器,节省资源。nginx 支持三种虚拟主机搭建方式,下面逐一详细讲解。
2.1 访问状态统计配置
通过 http_stub_status 模块,能实时查看 nginx 的运行状态(比如活跃连接数、处理的请求数),方便监控服务。
2.1.1 查看已安装的所有模块
首先确认 nginx 是否安装了统计模块,执行命令:
/usr/local/nginx/sbin/nginx -v
如果输出中包含 --with-http_stub_status_module,说明模块已安装;也可以用 cat /opt/nginx-1.20.2/auto/options | grep yes 查看所有已安装模块。
2.1.2 修改 nginx.conf 配置文件
- 先备份配置文件(防止改错):
cd /usr/local/nginx/conf cp nginx.conf nginx.conf.bak
- 编辑配置文件,在
server { }块中添加统计配置:
vim /usr/local/nginx/conf/nginx.conf
http {
server {
listen 80;
server_name localhost;
charset utf-8;
location / {
root html;
index index.html index.php;
}
# 添加状态统计配置
location /status {
stub_status on; # 开启统计功能
access_log off; # 关闭该路径的日志记录(减少冗余)
}
}
}2.1.3 重载服务,访问测试
- 重载 nginx 配置(不用重启服务,不影响运行):
systemctl reload nginx
- 测试访问:在浏览器输入
http://192.168.10.110/status(把 ip 换成你的服务器 ip),会看到以下信息:
active connections: 2 # 当前活跃连接数 server accepts handled requests: 100 98 200 # 已处理连接数、成功握手数、已处理请求数 reading: 0 writing: 1 waiting: 1 # 读取请求数、发送响应数、等待连接数
也可以用命令 curl -s http://192.168.10.110/status 查看,方便脚本监控。
2.2 基于授权的访问控制
给网站加“密码锁”,用户访问时需要输入用户名和密码才能进入,适合内部网站或敏感资源。
2.2.1 生成用户密码认证文件
- 安装生成密码的工具(httpd-tools):
yum install -y httpd-tools
- 生成密码文件(创建用户
zhangsan,密码文件存放在/usr/local/nginx/passwd.db):
htpasswd -c /usr/local/nginx/passwd.db zhangsan
执行后会提示输入密码,重复输入即可(密码不会显示,放心输入)。
3. 修改密码文件权限(保证 nginx 能读取,且其他人无法修改):
chown nginx /usr/local/nginx/passwd.db chmod 400 /usr/local/nginx/passwd.db
2.2.2 修改主配置文件相对应目录,添加认证配置项
编辑 nginx.conf,在需要认证的 location / { } 中添加认证参数:
vim /usr/local/nginx/conf/nginx.conf
server {
location / {
root html;
index index.html index.php;
auth_basic "请输入用户名密码"; # 登录提示框文字
auth_basic_user_file /usr/local/nginx/passwd.db; # 密码文件路径
}
}2.2.3 重启服务,访问测试
- 检查配置文件是否正确(避免语法错误):
nginx -t
输出 syntax is ok 和 test is successful 说明没问题。
2. 重启 nginx:
systemctl restart nginx
- 测试:访问网站时,浏览器会弹出登录框,输入用户名
zhangsan和设置的密码,才能看到页面;密码错误会提示“401 未授权”。
2.3 基于客户端ip的访问控制
设置“黑白名单”,只允许特定 ip 访问,拒绝其他 ip,适合限制内部服务只对公司内网开放。
访问控制规则很简单:
allow ip/ip 段:允许某个 ip 或 ip 段访问。deny ip/ip 段:拒绝某个 ip 或 ip 段访问。- 规则从上到下执行,匹配到就停止(比如先允许再拒绝,以允许为准)。
配置步骤:
- 编辑
nginx.conf,在location / { }中添加规则:
vim /usr/local/nginx/conf/nginx.conf
server {
location / {
root html;
index index.html index.php;
allow 192.168.10.123; # 允许这个 ip 访问
deny all; # 拒绝其他所有 ip
}
}- 重启 nginx:
systemctl restart nginx
- 测试:
- 用
192.168.10.123访问:能正常打开页面。 - 用其他 ip 访问:会提示“403 禁止访问”。
2.4 基于域名的 nginx 虚拟主机
多个域名指向同一台服务器的同一个 ip,通过域名区分不同网站(比如 www.yjs.com 和 www.simoncwh.com 都指向 192.168.10.110,但显示不同内容)。
2.4.1 为虚拟主机提供域名解析
让服务器识别域名,编辑 /etc/hosts 文件(本地解析,不用改 dns):
echo "192.168.10.110 www.yjs.com www.simoncwh.com" >> /etc/hosts
执行后,服务器会把这两个域名映射到 192.168.10.110。
2.4.2 为虚拟主机准备网页文档
创建两个网站的网页目录,分别存放不同的首页:
# 创建目录 mkdir -p /usr/local/nginx/html/yjs mkdir -p /usr/local/nginx/html/simoncwh # 生成首页内容 echo "<h1>欢迎访问 www.yjs.com</h1>" > /usr/local/nginx/html/yjs/index.html echo "<h1>欢迎访问 www.simoncwh.com</h1>" > /usr/local/nginx/html/simoncwh/index.html
2.4.3 修改 nginx.conf 配置文件
在 http { } 块中添加两个 server { } 块,分别对应两个域名:
vim /usr/local/nginx/conf/nginx.conf
http {
# 第一个虚拟主机:www.yjs.com
server {
listen 80;
server_name www.yjs.com; # 绑定域名
charset utf-8;
access_log logs/www.yjs.access.log; # 单独的访问日志
location / {
root /usr/local/nginx/html/yjs; # 网页目录
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
# 第二个虚拟主机:www.simoncwh.com
server {
listen 80;
server_name www.simoncwh.com; # 绑定域名
charset utf-8;
access_log logs/www.simoncwh.access.log; # 单独的访问日志
location / {
root /usr/local/nginx/html/simoncwh; # 网页目录
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}2.4.4 重启服务,访问测试
- 重启 nginx:
systemctl restart nginx
- 测试:
- 浏览器输入
http://www.yjs.com:显示“欢迎访问 www.yjs.com”。 - 浏览器输入
http://www.simoncwh.com:显示“欢迎访问 www.simoncwh.com”。
两个域名共用一个 ip 和 80 端口,却展示不同内容,虚拟主机配置成功。
2.5 基于ip 的 nginx 虚拟主机
给服务器配置多个 ip,每个 ip 对应一个网站(比如 192.168.10.110 对应 www.yjs.com,192.168.10.40 对应 www.simoncwh.com)。
2.5.1 配置 server1
给服务器添加第二个 ip(临时生效,重启网卡后消失;想永久生效可修改网卡配置文件):
ifconfig ens33:0 192.168.10.40 netmask 255.255.255.0
执行后,服务器有两个 ip:192.168.10.110(原 ip)和 192.168.10.40(新 ip)。
编辑 nginx.conf,添加第一个 server { } 块:
vim /usr/local/nginx/conf/nginx.conf
http {
server {
listen 192.168.10.110:80; # 监听第一个 ip 的 80 端口
server_name www.yjs.com;
charset utf-8;
access_log logs/www.yjs.access.log;
location / {
root /usr/local/nginx/html/yjs;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}2.5.2 配置 server2
在 http { } 块中添加第二个 server { } 块,对应新 ip:
http {
# 第二个虚拟主机
server {
listen 192.168.10.40:80; # 监听第二个 ip 的 80 端口
server_name www.simoncwh.com;
charset utf-8;
access_log logs/www.simoncwh.access.log;
location / {
root /usr/local/nginx/html/simoncwh;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}2.5.3 重启服务,测试访问
- 重启 nginx:
systemctl restart nginx
- 测试:
- 访问
http://192.168.10.110:显示www.yjs.com的首页。 - 访问
http://192.168.10.40:显示www.simoncwh.com的首页。
不同 ip 对应不同网站,实现基于 ip 的虚拟主机。
2.6 基于端口的 nginx 虚拟主机
同一 ip 下,用不同端口区分不同网站(比如 192.168.10.110:8080 对应 www.yjs.com,192.168.10.110:8888 对应 www.simoncwh.com)。
2.6.1 配置端口1
编辑 nginx.conf,添加第一个 server { } 块,监听 8080 端口:
vim /usr/local/nginx/conf/nginx.conf
http {
server {
listen 192.168.10.110:8080; # 监听 8080 端口
server_name www.yjs.com;
charset utf-8;
access_log logs/www.yjs.access.log;
location / {
root /usr/local/nginx/html/yjs;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}2.6.2 配置端口2
添加第二个 server { } 块,监听 8888 端口:
http {
server {
listen 192.168.10.110:8888; # 监听 8888 端口
server_name www.simoncwh.com;
charset utf-8;
access_log logs/www.simoncwh.access.log;
location / {
root /usr/local/nginx/html/simoncwh;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}2.6.3 重启服务,测试访问
- 重启 nginx:
systemctl restart nginx
- 测试:
- 访问
http://192.168.10.110:8080:显示www.yjs.com的首页。 - 访问
http://192.168.10.110:8888:显示www.simoncwh.com的首页。
同一 ip 不同端口对应不同网站,适合测试环境或不需要域名的场景。
总结
一、nginx 核心配置解析
nginx 的配置文件 nginx.conf 是功能实现的核心,关键要掌握三点:
- 全局配置:
worker_processes匹配服务器内核,决定并发处理的“基础能力”。 - i/o 事件配置:
epoll模型 +worker_connections组合,优化并发连接处理效率。 - http 与 location 配置:
root/alias/proxy_pass区分清楚,server块绑定端口和域名,是 web 服务的“核心规则”。
二、虚拟主机与实用功能
虚拟主机让一台服务器“变身”多台,三种方式各有适用场景:
- 基于域名:最常用,适合生产环境(比如多个业务网站共用服务器)。
- 基于 ip:适合需要独立 ip 的场景(比如 ssl 证书绑定 ip)。
- 基于端口:适合测试或内部服务(不用申请域名,直接用端口访问)。
此外,访问状态统计、ip 授权、密码认证等功能,能帮你监控服务、保障安全。掌握这些配置后,就能灵活应对大部分 web 服务部署需求。
到此这篇关于nginx配置文件解析与虚拟主机搭建过程的文章就介绍到这了,更多相关nginx配置文件与虚拟主机搭建内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论