插件窝 干货文章 Linux环境下怎么安装及使用Nginx

Linux环境下怎么安装及使用Nginx

Nginx sudo 配置 安装 968    来源:    2025-04-27

Linux环境下安装及使用Nginx指南

一、安装Nginx

1. 基于Debian/Ubuntu系统安装

# 更新软件包列表
sudo apt update

# 安装Nginx
sudo apt install nginx

# 启动Nginx服务
sudo systemctl start nginx

# 设置开机自启
sudo systemctl enable nginx

2. 基于RHEL/CentOS系统安装

# 添加EPEL仓库(CentOS/RHEL 7及以下)
sudo yum install epel-release

# 安装Nginx
sudo yum install nginx

# 启动Nginx服务
sudo systemctl start nginx

# 设置开机自启
sudo systemctl enable nginx

3. 从源码编译安装

# 安装依赖
sudo apt install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev

# 下载源码(以1.25.3版本为例)
wget https://nginx.org/download/nginx-1.25.3.tar.gz
tar -zxvf nginx-1.25.3.tar.gz
cd nginx-1.25.3

# 配置编译选项
./configure --prefix=/usr/local/nginx \
            --with-http_ssl_module \
            --with-http_v2_module \
            --with-http_realip_module

# 编译并安装
make && sudo make install

# 启动Nginx
/usr/local/nginx/sbin/nginx

二、基本使用

1. 服务管理命令

# 启动Nginx
sudo systemctl start nginx

# 停止Nginx
sudo systemctl stop nginx

# 重启Nginx
sudo systemctl restart nginx

# 重新加载配置(不中断服务)
sudo systemctl reload nginx

# 查看状态
sudo systemctl status nginx

2. 配置文件结构

Nginx主要配置文件通常位于: - /etc/nginx/nginx.conf (包管理器安装) - /usr/local/nginx/conf/nginx.conf (源码安装)

典型配置目录结构:

/etc/nginx/
├── nginx.conf          # 主配置文件
├── conf.d/             # 额外配置文件目录
├── sites-available/    # 可用站点配置
├── sites-enabled/      # 已启用站点配置(通常是符号链接)
├── modules-available/  # 可用模块
└── modules-enabled/    # 已启用模块

3. 基本配置示例

# /etc/nginx/sites-available/example.com
server {
    listen 80;
    server_name example.com www.example.com;

    root /var/www/example.com/html;
    index index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

    # 静态文件缓存
    location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
        expires 30d;
        add_header Cache-Control "public, no-transform";
    }
}

启用站点配置:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t  # 测试配置语法
sudo systemctl reload nginx

三、高级功能

1. 配置HTTPS

# 安装Certbot(Let's Encrypt客户端)
sudo apt install certbot python3-certbot-nginx

# 获取并安装证书
sudo certbot --nginx -d example.com -d www.example.com

# 设置自动续期
sudo certbot renew --dry-run

2. 负载均衡配置

upstream backend {
    server backend1.example.com weight=5;
    server backend2.example.com;
    server backend3.example.com;

    # 可选: 负载均衡方法
    # least_conn;  # 最少连接
    # ip_hash;     # IP哈希
}

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://backend;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

3. 反向代理配置

server {
    listen 80;
    server_name app.example.com;

    location / {
        proxy_pass http://localhost:3000;  # 代理到本地3000端口
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

四、日志管理

Nginx日志通常位于: - 访问日志: /var/log/nginx/access.log - 错误日志: /var/log/nginx/error.log

自定义日志格式:

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;
}

五、性能调优

  1. 工作进程优化
worker_processes auto;  # 自动设置为CPU核心数
worker_connections 1024;  # 每个工作进程的最大连接数
  1. 启用Gzip压缩
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
gzip_min_length 1024;
gzip_proxied any;
  1. 启用缓存
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m inactive=60m use_temp_path=off;

server {
    location / {
        proxy_cache my_cache;
        proxy_cache_valid 200 302 10m;
        proxy_cache_valid 404 1m;
    }
}

六、常见问题排查

  1. 测试配置语法
sudo nginx -t
  1. 检查运行状态
sudo systemctl status nginx
ps aux | grep nginx
  1. 查看监听端口
sudo netstat -tulnp | grep nginx
# 或
sudo ss -tulnp | grep nginx
  1. 查看错误日志
sudo tail -f /var/log/nginx/error.log
  1. 403 Forbidden错误

- 检查目录权限 - 确保index文件存在 - 检查SELinux状态(如启用)

通过以上步骤,您应该能够在Linux环境下成功安装、配置和使用Nginx作为Web服务器或反向代理服务器。