linux中级_Nginx配置文件详解

TJCcc 发布于 2025-12-03 23 次阅读


Nginx 详解笔记


一、服务架构简介

常见的 Web 服务架构有以下几种:

架构名称组成说明适用场景
LNMPLinux + Nginx + MySQL + PHPPHP 动态网站
LNMTLinux + Nginx + MySQL + TomcatJava Web 应用
LAMPLinux + Apache + MySQL + PHP传统 PHP + Apache 环境
LNMPLinux + Nginx + MySQL + Python(如 Django/Flask)Python Web 应用

Nginx 常作为反向代理服务器静态资源服务器,具有高并发、低内存占用的特点。


二、Nginx 安装方式

1. 编译安装

  • 灵活,可自定义模块
  • 步骤较复杂,适合定制化需求

2. Yum 安装(EPEL 仓库)

  • 版本较旧(如 1.20)
  • 配置不易读,不建议生产使用

3. 官方仓库安装(推荐)

步骤:

# 1. 配置 Nginx 官方仓库
cat > /etc/yum.repos.d/nginx.repo << EOF

[nginx-stable]

name=nginx stable repo baseurl=http://nginx.org/packages/centos/\$releasever/\$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key EOF # 2. 安装 yum -y install nginx # 3. 查看版本 nginx -v # nginx version: nginx/1.24.0


三、Nginx 配置文件解析

主配置文件:/etc/nginx/nginx.conf

核心区块

user nginx;                      # 运行 Nginx 的用户
worker_processes auto;           # 工作进程数,建议设为 CPU 核数
error_log /var/log/nginx/error.log notice;  # 错误日志
pid /var/run/nginx.pid;          # 进程 PID 文件

事件模块

events {
    worker_connections 25532;    # 每个进程的最大连接数
}

HTTP 模块

http {
    include /etc/nginx/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 /var/log/nginx/access.log main;

    sendfile on;
    keepalive_timeout 65;
    # gzip on;  # 可开启压缩

    include /etc/nginx/conf.d/*.conf;  # 包含业务配置文件
}

四、配置一个简单的虚拟主机

1. 创建业务配置文件

/etc/nginx/conf.d/ 下创建 default.conf

server {
    listen 80;
    server_name www.game.com;

    location / {
        root /code;
        index index.html index.htm;
    }
}

2. 创建网站目录与测试页面

mkdir -p /code
echo "Hello, Nginx!" > /code/index.html

3. 检查语法并重启

nginx -t                    # 检查语法
systemctl restart nginx    # 重启服务

4. 验证服务

netstat -tnulp | grep :80
curl http://localhost

五、多业务配置方式

1. 多 IP 方式

  • 为服务器绑定多个 IP
  • 每个 IP 对应一个业务

示例:

server {
    listen 10.0.0.7:80;
    root /code;
}
server {
    listen 10.0.0.8:80;
    root /data;
}

2. 多端口方式

  • 同一 IP,不同端口对应不同业务

示例:

server {
    listen 80;
    root /code;
}
server {
    listen 81;
    root /data;
}

3. 多域名方式(企业常用)

  • 同一 IP 和端口,通过域名区分业务

示例:

server {
    listen 80;
    server_name www.game.com;
    root /code;
}
server {
    listen 80;
    server_name www.xbw.com;
    root /data;
}

若访问未配置的域名,Nginx 默认返回第一个配置的 server 块内容。


六、Nginx 启动与管理

两种启动方式:

1. 使用 systemctl(推荐)

systemctl start nginx
systemctl stop nginx
systemctl restart nginx
systemctl reload nginx   # 平滑重载配置
systemctl enable nginx   # 开机自启

2. 使用二进制文件

/usr/sbin/nginx              # 启动
/usr/sbin/nginx -s stop      # 停止
/usr/sbin/nginx -s reload    # 重载
/usr/sbin/nginx -t           # 检查语法

注意:同一时间只能使用一种方式管理 Nginx。


七、拓展知识

Nginx 性能优化建议:

  • 调整 worker_processes 为 CPU 核数
  • 适当增加 worker_connections
  • 启用 gzip 压缩静态资源
  • 使用 expires 设置缓存头,减少请求

日志管理:

  • 访问日志:access_log
  • 错误日志:error_log
  • 可配置日志轮转,避免文件过大

安全建议:

  • 隐藏 Nginx 版本号(在 http 块中加 server_tokens off;
  • 限制请求频率与连接数
  • 使用 HTTPS 并配置 SSL/TLS

八、常见问题与解决

1. 端口占用

netstat -tnulp | grep :80
# 如果被占用,可修改 listen 端口或停止冲突服务

2. 权限问题

  • 确保 root 目录有正确权限(如 755
  • 运行用户 nginx 需有读取权限

3. 配置生效问题

  • 每次修改配置后,执行 nginx -t 检查语法
  • 使用 systemctl reload nginx 平滑重载