开箱即用的配置

Nginx 提供了开箱即用的配置文件,可以直接使用或根据需求进行修改。

默认配置文件

主配置文件

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

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;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;
    #gzip  on;

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

默认服务器配置

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

MIME 类型

MIME 类型文件

Nginx 包含了一个预配置的 MIME 类型文件,定义了文件扩展名与 MIME 类型的映射。

常用 MIME 类型

扩展名MIME 类型
.htmltext/html
.csstext/css
.jsapplication/javascript
.jsonapplication/json
.jpgimage/jpeg
.pngimage/png
.gifimage/gif
.svgimage/svg+xml
.pdfapplication/pdf
.zipapplication/zip

启用注释的配置

启用 gzip 压缩

http {
    gzip  on;
    gzip_vary on;
    gzip_min_length  1024;
    gzip_comp_level 6;
    gzip_types text/plain text/css text/xml text/javascript application/json application/javascript application/xml+rss application/rss+xml font/truetype font/opentype application/vnd.ms-fontobject image/svg+xml;
}

启用虚拟主机

server {
    listen       80;
    server_name  example.com;

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

启用 PHP 支持

location ~ \.php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    include        fastcgi_params;
}

启用访问日志

server {
    listen       80;
    server_name  localhost;

    access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
}

快速配置模板

静态网站

server {
    listen       80;
    server_name  example.com;

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

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

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

PHP 网站

server {
    listen       80;
    server_name  example.com;

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

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

反向代理

upstream backend {
    server 127.0.0.1:8080;
}

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;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

HTTPS 网站

server {
    listen       443 ssl;
    server_name  example.com;

    ssl_certificate      /etc/ssl/certs/server.crt;
    ssl_certificate_key  /etc/ssl/private/server.key;

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

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

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

配置文件组织

推荐结构

/etc/nginx/
├── nginx.conf              # 主配置文件
├── conf.d/                 # 额外配置目录
│   ├── default.conf        # 默认服务器配置
│   ├── example.com.conf    # 虚拟主机配置
│   └── blog.conf           # 博客配置
├── sites-available/        # 可用站点
│   ├── example.com
│   └── blog.conf
└── sites-enabled/          # 启用站点(符号链接)
    ├── example.com -> ../sites-available/example.com
    └── blog.conf -> ../sites-available/blog.conf

使用 include

# 主配置文件
http {
    include /etc/nginx/mime.types;
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

测试配置

# 测试配置
sudo nginx -t

# 显示配置
sudo nginx -T

# 重新加载
sudo nginx -s reload

最佳实践

  1. 备份配置:修改前备份原始配置
  2. 逐步启用:逐个启用注释的配置
  3. 测试配置:每次修改后测试配置
  4. 使用 include:组织多个配置文件
  5. 注释说明:为自定义配置添加注释
  6. 版本控制:使用 Git 管理配置文件