# Nginx 常用配置示例

以下是 nginx.conf 配置文件示例

对于 Gzip 压缩,类型填写 * 表示压缩所有类型

user  nginx;
worker_processes  1;

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


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


http {
    server_names_hash_bucket_size 512;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 50m;
	
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 256k;
	
    client_header_timeout 10;
    client_body_timeout 10;
	
    reset_timedout_connection on;
    send_timeout 10;

    limit_conn_zone $binary_remote_addr zone=addr:10m;
    limit_conn addr 100;
	
    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;

    fastcgi_intercept_errors on;
    # error_page  404 403 500 502 503 504  /404.html;
	
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

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

# WordPress 配置示例

server
{
   listen 80;
   server_name example.com;

   return 301 https://$server_name$request_uri;
}

server
{
   listen 443 ssl http2;
   server_name example.com;
   index index.php;
   root /var/www/wordpress;
 
   ssl_certificate /etc/nginx/ssl/example.crt;
   ssl_certificate_key /etc/nginx/ssl/example.key;
   ssl_session_cache shared:SSL:10m;
   ssl_session_timeout 10m;
   charset utf-8;

   location = /robots.txt {
      allow all;
      log_not_found off;
      access_log off;
   }
 
   location / {
      try_files $uri $uri/ /index.php?$args ;
      proxy_set_header  Host  $host;
      proxy_set_header  X-Real-IP  $remote_addr;
      proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
   }
 
   location ~ \.php$ {
      include snippets/fastcgi-php.conf;
      fastcgi_pass unix:/run/php/php7.0-fpm.sock;
   }

   location ~* ^.+\.(eot|ttf|otf|woff|svg)$ {
      access_log off;
      expires max;
   }

   location ~* \.(?:css|js)$ {
      access_log off;
      add_header Cache-Control "public";
   }
 
   access_log /var/log/www/access.log;
   error_log /var/log/www/error.log;
}

# 反向代理配置示例

server
{
   listen 80;
   server_name example.com;

   return 301 https://$server_name$request_uri;
}

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

   ssl_certificate /etc/nginx/ssl/example.crt;
   ssl_certificate_key /etc/nginx/ssl/example.key;
   ssl_session_cache shared:SSL:10m;
   ssl_session_timeout 10m;
   charset utf-8;

   location / {
      proxy_pass http://127.0.0.1:5000; 
      proxy_redirect off;
      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_http_version 1.1;
   }

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