插件窝 干货文章 Nginx安装及配置实例分析

Nginx安装及配置实例分析

Nginx server 配置 example 351    来源:    2025-04-23

Nginx安装及配置实例分析

一、Nginx简介

Nginx是一款高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。它具有占用内存少、并发能力强、模块化设计等优点,被广泛用于负载均衡、静态资源服务和反向代理等场景。

二、Nginx安装方法

1. Linux系统安装

Ubuntu/Debian系统

sudo apt update
sudo apt install nginx
sudo systemctl start nginx
sudo systemctl enable nginx

CentOS/RHEL系统

sudo yum install epel-release
sudo yum install nginx
sudo systemctl start nginx
sudo systemctl enable nginx

2. Windows系统安装

  1. 从官网下载Windows版本的Nginx压缩包
  2. 解压到指定目录
  3. 运行nginx.exe即可启动

3. 源码编译安装(适用于自定义模块)

wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0
./configure --prefix=/usr/local/nginx --with-http_ssl_module
make && make install

三、Nginx基本配置实例

1. 基本HTTP服务器配置

server {
    listen 80;
    server_name example.com;

    location / {
        root /var/www/html;
        index index.html index.htm;
    }

    error_page 404 /404.html;
    location = /404.html {
        root /var/www/error_pages;
    }
}

2. 负载均衡配置

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

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. HTTPS配置

server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate /etc/ssl/certs/example.com.crt;
    ssl_certificate_key /etc/ssl/private/example.com.key;

    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;

    location / {
        root /var/www/html;
        index index.html;
    }
}

# HTTP重定向到HTTPS
server {
    listen 80;
    server_name example.com;
    return 301 https://$host$request_uri;
}

4. 静态文件缓存配置

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

    location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
        root /var/www/static;
        expires 30d;
        add_header Cache-Control "public";
    }
}

四、Nginx常用命令

# 启动Nginx
nginx 或 systemctl start nginx

# 停止Nginx
nginx -s stop 或 systemctl stop nginx

# 重新加载配置
nginx -s reload 或 systemctl reload nginx

# 检查配置文件语法
nginx -t

# 查看Nginx版本
nginx -v

五、常见问题及解决方案

1. 端口冲突问题

现象: 启动Nginx时报错bind() to 0.0.0.0:80 failed (98: Address already in use)

解决方案:

# 查找占用80端口的进程
sudo netstat -tulnp | grep :80

# 停止占用进程或修改Nginx监听端口

2. 403 Forbidden错误

可能原因: - 目录权限不足 - 没有默认索引文件 - SELinux限制

解决方案:

# 检查目录权限
chmod -R 755 /var/www/html
chown -R nginx:nginx /var/www/html

# 检查SELinux状态
getenforce
setenforce 0  # 临时关闭

3. 502 Bad Gateway错误

可能原因: - 后端服务未启动 - 代理配置错误 - 连接超时

解决方案: 检查后端服务状态和Nginx代理配置中的proxy_pass设置是否正确。

六、性能优化建议

  1. 工作进程优化:

    worker_processes auto;  # 自动设置为CPU核心数
    worker_connections 1024;  # 每个工作进程的最大连接数
    
  2. 启用Gzip压缩:

    gzip on;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
    
  3. 启用文件缓存:

    open_file_cache max=1000 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors on;
    
  4. 调整缓冲区大小:

    client_body_buffer_size 10K;
    client_header_buffer_size 1k;
    client_max_body_size 8m;
    large_client_header_buffers 2 1k;
    

通过以上配置实例和优化建议,您可以快速搭建和优化Nginx服务器,满足不同的业务需求。