插件窝 干货文章 Centos7下nginx的安装与配置的方法

Centos7下nginx的安装与配置的方法

Nginx 配置 sudo example 151    来源:    2025-04-21

CentOS 7下Nginx的安装与配置指南

一、安装Nginx

方法1:通过EPEL仓库安装(推荐)

  1. 添加EPEL仓库:

    sudo yum install epel-release
    
  2. 安装Nginx:

    sudo yum install nginx
    
  3. 启动Nginx服务:

    sudo systemctl start nginx
    
  4. 设置开机启动:

    sudo systemctl enable nginx
    

方法2:从Nginx官方仓库安装(获取最新版本)

  1. 添加Nginx官方仓库:

    sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
    
  2. 安装Nginx:

    sudo yum install nginx
    
  3. 启动并设置开机启动(同上)

二、基本配置

1. 主要配置文件位置

  • 主配置文件:/etc/nginx/nginx.conf
  • 默认服务器配置:/etc/nginx/conf.d/default.conf
  • 自定义站点配置:建议在/etc/nginx/conf.d/目录下创建单独的.conf文件

2. 常用配置示例

基本HTTP服务器配置

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

    root   /usr/share/nginx/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;
    }
}

启用Gzip压缩

/etc/nginx/nginx.conf的http块中添加:

gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
gzip_min_length 1000;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_vary on;

3. 配置SSL/TLS(HTTPS)

  1. 首先获取SSL证书(可以使用Let's Encrypt免费证书)

  2. 配置示例:

server {
    listen       443 ssl http2;
    server_name  example.com www.example.com;

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

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;

    root   /usr/share/nginx/html;
    index  index.html index.htm;

    # 其他配置...
}

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

三、常用命令

  • 启动Nginx:sudo systemctl start nginx
  • 停止Nginx:sudo systemctl stop nginx
  • 重启Nginx:sudo systemctl restart nginx
  • 重新加载配置(不中断服务):sudo systemctl reload nginx
  • 查看状态:sudo systemctl status nginx
  • 测试配置文件语法:sudo nginx -t

四、防火墙配置

如果启用了firewalld,需要开放HTTP和HTTPS端口:

sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

五、日志文件位置

  • 访问日志:/var/log/nginx/access.log
  • 错误日志:/var/log/nginx/error.log

六、性能调优建议

  1. 调整worker进程数(在/etc/nginx/nginx.conf中):

    worker_processes auto;  # 通常设置为CPU核心数
    
  2. 调整连接数限制:

    events {
       worker_connections 1024;
       multi_accept on;
    }
    
  3. 启用文件缓存:

    open_file_cache max=1000 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors on;
    

七、常见问题排查

  1. 测试配置文件sudo nginx -t
  2. 查看错误日志sudo tail -f /var/log/nginx/error.log
  3. 检查端口占用sudo netstat -tulnp | grep :80
  4. 检查SELinux状态getenforce,如果为Enforcing可能需要调整策略

希望这个指南能帮助您在CentOS 7上成功安装和配置Nginx!如需更高级的配置,可以参考Nginx官方文档。