📚 学习笔记

Nginx 使用指南与服务器搭建记录

🚀 Nginx 基础入门

1. 什么是 Nginx?

Nginx 是一款高性能的 HTTP 和反向代理服务器,以其高并发、低内存占用著称。

核心特点:高并发连接、低内存消耗、配置简单、支持热部署

2. 安装命令

# CentOS/RHEL/Alibaba Cloud Linux yum install -y epel-release yum install -y nginx # 启动服务 systemctl start nginx systemctl enable nginx

3. 常用命令

nginx -t # 测试配置文件 nginx -s reload # 重载配置 systemctl start nginx # 启动 systemctl stop nginx # 停止

⚙️ 配置文件详解

1. 配置文件位置

2. 基本 Server 配置

server { listen 80; server_name example.com; location / { root /var/www/html; index index.html; } }

3. 反向代理配置

location /api { proxy_pass http://localhost:3000; proxy_http_version 1.1; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; }

🌐 多域名配置

通配符子域名

使用正则表达式匹配所有子域名:

server { listen 80; server_name ~^(?<subdomain>.+)\.example\.com$; location / { root /var/www/$subdomain; } }
实际案例:本服务器使用 *.vip.techzb.xyz 通配符配置,每个子域名自动映射到对应目录。

🔧 常见问题解决

1. 中文乱码问题

在 HTML 的 <head> 中添加:

<meta charset="UTF-8">

或在 Nginx 配置中添加:

charset utf-8;

2. 默认 Server 抢占请求

问题:访问子域名返回默认页面

解决:删除或注释 nginx.conf 中的默认 server 块

🏷️ 相关标签

Nginx 反向代理 负载均衡 静态网站 Docker Linux