插件窝 干货文章 nginx HTTP服务器怎么安装与配置

nginx HTTP服务器怎么安装与配置

Nginx 配置 example systemctl 944    来源:    2025-04-23

Nginx HTTP服务器安装与配置指南

安装Nginx

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

Windows系统安装

  1. nginx官网下载Windows版本
  2. 解压到指定目录(如C:\nginx)
  3. 运行nginx.exe启动服务

macOS安装

brew install nginx
brew services start nginx

基本配置

主配置文件位置

  • Linux: /etc/nginx/nginx.conf
  • Windows: nginx安装目录/conf/nginx.conf

常用配置指令

# 工作进程数,通常设置为CPU核心数
worker_processes auto;

# 事件模块配置
events {
    worker_connections 1024;  # 每个工作进程的最大连接数
}

http {
    # MIME类型设置
    include       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;

    # 开启高效文件传输模式
    sendfile        on;
    tcp_nopush     on;
    tcp_nodelay    on;

    # 连接超时时间
    keepalive_timeout  65;

    # 开启gzip压缩
    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;
    include /etc/nginx/sites-enabled/*;
}

虚拟主机配置

创建虚拟主机配置文件

通常放在/etc/nginx/conf.d//etc/nginx/sites-available/目录下

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

    # 网站根目录
    root   /var/www/example.com;
    index  index.html index.htm;

    # 访问日志
    access_log  /var/log/nginx/example.com.access.log  main;

    # 错误页面配置
    error_page  404              /404.html;
    error_page   500 502 503 504  /50x.html;

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

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

启用站点(适用于sites-available/sites-enabled结构)

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

SSL/TLS配置

获取证书(以Let's Encrypt为例)

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com

手动配置SSL

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

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    # SSL优化配置
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384...';
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    ssl_session_tickets off;

    # 其他配置...
}

反向代理配置

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;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

常用命令

  • 测试配置: nginx -t
  • 重新加载配置: nginx -s reloadsystemctl reload nginx
  • 停止服务: nginx -s stopsystemctl stop nginx
  • 启动服务: systemctl start nginx
  • 查看状态: systemctl status nginx

日志文件位置

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

性能调优建议

  1. 根据服务器CPU核心数设置worker_processes
  2. 调整worker_connections以适应高并发
  3. 启用sendfiletcp_nopush提高静态文件传输效率
  4. 配置合理的keepalive_timeout
  5. 启用Gzip压缩减少传输数据量
  6. 为静态资源设置缓存头

希望这份指南能帮助您顺利安装和配置Nginx服务器!