Nginx的配置文件通常分为几个部分:main(全局设置)、events(事件设置)、http(http相关设置)以及server(服务器特定设置)。每个部分都包含了影响Nginx行为的指令。
可以将main、events、http配置合并在nginx.conf配置里,打开nginx.conf,配置如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| user nginx nginx;
worker_processes 2;
worker_cpu_affinity 01 10;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
worker_rlimit_nofile 32000;
events { worker_connections 16000; multi_accept on; use epoll; }
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" "$request_uri"'; access_log /var/log/nginx/access.log main;
gzip on; gzip_min_length 10k; gzip_comp_level 4; gzip_types text/plain text/css application/javascript application/x-javascript application/json text/xml application/xml application/xml+rss application/x-httpd-php text/javascript image/jpeg image/gif image/png; gzip_vary on; gzip_static on; proxy_cache_path /var/cache/nginx/cache levels=1:2 keys_zone=imgcache:100m inactive=1d max_size=10g; server_tokens off; proxy_intercept_errors on; fastcgi_intercept_errors on; sendfile on; tcp_nopush on; keepalive_timeout 30; keepalive_requests 500; tcp_nodelay on; client_body_timeout 15; client_header_timeout 15; reset_timedout_connection on; send_timeout 15; client_max_body_size 20M; client_body_buffer_size 1M; open_file_cache max=20000 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on; limit_req_zone $binary_remote_addr zone=allips:10m rate=30r/s;
map $http_upgrade $connection_upgrade { default upgrade; '' close; } include /etc/nginx/conf.d/*.conf; }
|
服务器特定设置(server)
特定设置放在conf.d目录下,这里可以包含N个设置文件,根据自己管理方便程度自行添加文件,上面 include /etc/nginx/conf.d/*.conf; 自动包含了conf.d下的配置文件。
server配置示例如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| server { listen 80; server_name admin.****.com; return 301 https://$server_name$request_uri; }
server { listen 443 ssl; server_name admin.****.com; ssl_certificate /etc/nginx/cert/certificate.pem; ssl_certificate_key /etc/nginx/cert/certificate.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on;
proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Port $server_port; proxy_set_header Cookie $http_cookie;
location / { root /usr/share/nginx/html; index index.html index.htm; }
location ^~ /api/ { rewrite ^/api/(.*)$ /$1 break; proxy_pass http://127.0.0.1:8001; } location .\.(gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ { valid_referers none blocked server_names ~\.example\.com$; error_page 403 /usr/share/nginx/403.html; access_log off; expires 30d; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }
|