nginx是一个高性能的HTTP和反向代理服务器,可以用来搭建静态网站、负载均衡、反向代理、动态缓存等等。下面是一个大致一万字左右的nginx配置文件示例:
# 监听的端口
listen 80;# 默认网站根目录
root /var/www/html;# 默认首页
index index.html index.htm;# 服务器名称
server_name example.com;# 日志文件
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;# MIME类型映射
types {text/html html htm shtml;text/css css;text/xml xml;image/gif gif;image/jpeg jpeg jpg;application/javascript js;application/x-javascript js;application/xml xml;application/json json;
}# 缓存配置
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;# 虚拟主机配置
server {# 监听的域名server_name www.example.com;# 静态文件缓存location ~* \.(jpg|jpeg|gif|png|ico|css|js)$ {expires 30d;}# 动态代理location /api/ {proxy_pass http://api.example.com/;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_cache my_cache;proxy_cache_valid 200 304 1d;proxy_cache_key "$host$request_uri";add_header X-Cached $upstream_cache_status;}# 反向代理location / {proxy_pass http://backend_server;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}
}# 负载均衡配置
upstream backend_server {server backend1.example.com;server backend2.example.com;server backend3.example.com;
}# SSL配置
server {listen 443 ssl http2;server_name secure.example.com;ssl_certificate /etc/ssl/certs/nginx.crt;ssl_certificate_key /etc/ssl/private/nginx.key;location / {proxy_pass http://backend_server;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}
}
以上是一个简单的nginx配置文件示例,包含了基本的监听端口、根目录、默认首页、服务器名称、日志文件、MIME类型映射、缓存配置、虚拟主机配置、动态代理、反向代理、负载均衡以及SSL配置等内容。根据实际需求,可以根据这个示例进行修改和扩展。