Nginx-基礎(chǔ)詳細的配置文件說明
詳細的注釋說明:
# 用戶和工作進程配置
user nginx;
worker_processes auto;
# 錯誤日志和進程 ID 文件路徑
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
# 事件模塊配置
events {
? ? worker_connections 1024;
}
# HTTP 模塊配置
http {
? ? # MIME 類型映射
? ? 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;
? ? # 文件傳輸和網(wǎng)絡(luò)優(yōu)化配置
? ? sendfile on;
? ? tcp_nopush on;
? ? tcp_nodelay on;
? ? keepalive_timeout 65;
? ? types_hash_max_size 2048;
? ? # Gzip 壓縮配置
? ? gzip on;
? ? gzip_comp_level 5;
? ? gzip_min_length 256;
? ? gzip_proxied any;
? ? gzip_vary on;
? ? gzip_types application/javascript text/css text/xml application/xml application/rss+xml text/plain image/svg+xml;
? ? # HTTP 服務(wù)器配置
? ? server {
? ? ? ? # 監(jiān)聽端口和域名配置
? ? ? ? listen 80;
? ? ? ? server_name example.com;
? ? ? ? # 根路徑和默認(rèn)文檔配置
? ? ? ? root /usr/share/nginx/html;
? ? ? ? index index.html;
? ? ? ? # 靜態(tài)資源緩存配置
? ? ? ? location ~* \.(js|css|pdf|docx?|xlsx?|pptx?|txt|ico|jpe?g|png|gif|bmp|svg)$ {
? ? ? ? ? ? expires 7d;
? ? ? ? }
? ? ? ? # 提升網(wǎng)頁訪問速度配置
? ? ? ? location ~* \.(html|htm|txt)$ {
? ? ? ? ? ? add_header Cache-Control "public";
? ? ? ? ? ? add_header Vary Accept-Encoding;
? ? ? ? ? ? expires 1d;
? ? ? ? }
? ? ? ? # 反向代理配置
? ? ? ? location /api {
? ? ? ? ? ? proxy_pass http://backend;? ? ? ? ? ? ? ? ? # 反向代理到后端服務(wù)器
? ? ? ? ? ? proxy_set_header Host $host;
? ? ? ? ? ? proxy_set_header X-Real-IP $remote_addr;
? ? ? ? }
? ? ? ? # 別名配置
? ? ? ? location /images {
? ? ? ? ? ? alias /var/www/images;
? ? ? ? }
? ? }
? ? # HTTPS 服務(wù)器配置
? ? server {
? ? ? ? # 監(jiān)聽端口和域名配置
? ? ? ? listen 443 ssl;
? ? ? ? server_name example.com;
? ? ? ? # SSL 證書和私鑰配置
? ? ? ? ssl_certificate /etc/nginx/ssl/example.crt;
? ? ? ? ssl_certificate_key /etc/nginx/ssl/example.key;
? ? ? ? # 根路徑和默認(rèn)文檔配置
? ? ? ? root /usr/share/nginx/html;
? ? ? ? index index.html;
? ? ? ? # 靜態(tài)資源緩存配置
? ? ? ? location ~* \.(js|css|pdf|docx?|xlsx?|pptx?|txt|ico|jpe?g|png|gif|bmp|svg)$ {
? ? ? ? ? ? expires 7d;
? ? ? ? }
? ? ? ? # 提升網(wǎng)頁訪問速度配置
? ? ? ? location ~* \.(html|htm|txt)$ {
? ? ? ? ? ? add_header Cache-Control "public";
? ? ? ? ? ? add_header Vary Accept-Encoding;
? ? ? ? ? ? expires 1d;
? ? ? ? }
? ? ? ? # 反向代理配置
? ? ? ? location /api {
? ? ? ? ? ? proxy_pass http://backend;? ? ? ? ? ? ? ? ? # 反向代理到后端服務(wù)器
? ? ? ? ? ? proxy_set_header Host $host;
? ? ? ? ? ? proxy_set_header X-Real-IP $remote_addr;
? ? ? ? }
? ? ? ? # 別名配置
? ? ? ? location /images {
? ? ? ? ? ? alias /var/www/images;
? ? ? ? }
? ? }
? ? # 后端服務(wù)器配置
? ? upstream backend {
? ? ? ? server backend1.example.com;? ? ? ? ? ? ? ? ? ? # 后端服務(wù)器地址
? ? }
}
請注意滓玖,此配置文件中的路徑和域名僅作為示例鬓催,您需要根據(jù)您自己的實際情況進行修改漠其。另外香椎,SSL 證書的路徑和名稱也需要根據(jù)實際情況進行調(diào)整。