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

nginx安装配置实例分析

Nginx 配置 example server 755    来源:    2025-04-22

Nginx安装配置实例分析

一、Nginx简介

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

二、Nginx安装

1. Linux系统安装(以Ubuntu为例)

# 更新软件包索引
sudo apt update

# 安装Nginx
sudo apt install nginx

# 启动Nginx服务
sudo systemctl start nginx

# 设置开机自启
sudo systemctl enable nginx

# 检查状态
sudo systemctl status nginx

2. Windows系统安装

  1. 从官网(https://nginx.org/en/download.html)下载Windows版本
  2. 解压到指定目录
  3. 运行nginx.exe即可启动

三、基本配置实例

1. 静态网站配置

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

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

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

    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;
}

2. 反向代理配置

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

    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

3. 负载均衡配置

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

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

    location / {
        proxy_pass http://backend;
    }
}

四、常见问题解决方案

1. 403 Forbidden错误

可能原因及解决方案: - 目录权限问题:chmod -R 755 /var/www/example.com - 用户配置问题:确保nginx配置中的用户有访问权限 - SELinux限制:setenforce 0临时关闭或配置正确的安全上下文

2. 502 Bad Gateway错误

排查步骤: 1. 检查后端服务是否正常运行 2. 检查防火墙设置 3. 查看Nginx错误日志:tail -f /var/log/nginx/error.log

3. 性能优化建议

# 调整worker进程数
worker_processes auto;

# 每个worker的最大连接数
events {
    worker_connections 1024;
}

# 启用gzip压缩
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml;

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

五、进阶配置

1. 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;

    # 其他配置...
}

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

2. 限流配置

# 定义限流区域
limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;

server {
    location /api/ {
        limit_req zone=one burst=20 nodelay;
        proxy_pass http://backend;
    }
}

六、日志分析

Nginx访问日志格式示例:

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                '$status $body_bytes_sent "$http_referer" '
                '"$http_user_agent" "$http_x_forwarded_for"';

常用日志分析命令:

# 查看访问最多的IP
awk '{print $1}' access.log | sort | uniq -c | sort -nr | head -n 10

# 查看HTTP状态码统计
awk '{print $9}' access.log | sort | uniq -c | sort -rn

# 实时监控访问
tail -f access.log | awk '{print $1, $4, $6, $7, $9}'

七、总结

Nginx的配置灵活强大,通过合理的配置可以满足各种Web服务需求。建议: 1. 根据实际需求选择合适的配置方案 2. 定期检查日志,监控服务状态 3. 保持Nginx版本更新,及时修复安全漏洞 4. 复杂配置前先在测试环境验证