插件窝 干货文章 centos服务器如何配置nginx

centos服务器如何配置nginx

Nginx 配置 sudo 安装 996    来源:    2025-04-22

CentOS服务器配置Nginx指南

1. 安装Nginx

方法一:使用YUM仓库安装

# 添加EPEL仓库
sudo yum install epel-release

# 安装Nginx
sudo yum install nginx

# 启动Nginx服务
sudo systemctl start nginx

# 设置开机自启
sudo systemctl enable nginx

方法二:从源代码编译安装(最新版本)

# 安装编译依赖
sudo yum install gcc pcre-devel zlib-devel make

# 下载最新稳定版Nginx
wget http://nginx.org/download/nginx-x.x.x.tar.gz
tar -zxvf nginx-x.x.x.tar.gz
cd nginx-x.x.x

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

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

# 创建systemd服务文件
sudo vi /etc/systemd/system/nginx.service

2. 基本配置

主配置文件位置

  • YUM安装:/etc/nginx/nginx.conf
  • 源码安装:/usr/local/nginx/conf/nginx.conf

常用配置修改

user nginx;
worker_processes auto;  # 根据CPU核心数自动设置

events {
    worker_connections 1024;
    use epoll;
    multi_accept on;
}

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;
    error_log /var/log/nginx/error.log warn;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;

    keepalive_timeout 65;

    gzip on;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    include /etc/nginx/conf.d/*.conf;
}

3. 配置虚拟主机

创建站点配置文件

sudo vi /etc/nginx/conf.d/example.com.conf

基本虚拟主机配置

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

    error_page 404 /404.html;
    location = /404.html {
        internal;
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        internal;
    }
}

4. SSL/TLS配置

使用Let's Encrypt免费证书

# 安装Certbot
sudo yum install certbot python3-certbot-nginx

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

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

手动配置SSL

server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate /etc/nginx/ssl/example.com.crt;
    ssl_certificate_key /etc/nginx/ssl/example.com.key;

    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384';
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;

    # 其他配置...
}

5. 常用管理命令

# 检查配置语法
sudo nginx -t

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

# 重启Nginx
sudo systemctl restart nginx

# 停止Nginx
sudo systemctl stop nginx

# 查看状态
sudo systemctl status nginx

# 查看版本和编译参数
nginx -V

6. 性能优化建议

  1. 调整worker_processes为CPU核心数
  2. 启用gzip压缩
  3. 配置静态文件缓存
  4. 限制客户端请求体大小
  5. 配置连接超时时间
  6. 启用HTTP/2(需要SSL)
  7. 配置负载均衡(如果需要)

7. 日志管理

# 查看访问日志
tail -f /var/log/nginx/access.log

# 查看错误日志
tail -f /var/log/nginx/error.log

# 日志轮转配置
sudo vi /etc/logrotate.d/nginx

8. 防火墙配置

# 开放HTTP和HTTPS端口
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

按照以上步骤配置完成后,您的Nginx服务器应该可以正常运行并服务网站内容了。根据实际需求,您可能需要进一步调整配置参数。