Nginx 常用配置彙總:從入門到實戰
引言
Nginx 是一款高性能的 HTTP 和反向代理服務器,同時也是 IMAP/POP3/SMTP 代理服務器和通用的 TCP/UDP 代理服務器。它以其高併發、低資源消耗、易於擴展和配置靈活等特點,在現代互聯網架構中佔據重要地位。本文將詳細介紹 Nginx 的常用配置,幫助讀者從入門到實戰掌握 Nginx 的配置技巧。
一、Nginx 簡介
Nginx 由俄羅斯人 Igor Sysoev 開發,開源且輕量級。它的設計目標是高效處理高併發連接,支持高達 50,000 個併發連接。Nginx 還支持熱部署,可以在不中斷服務的情況下更新配置或升級軟件。
二、Nginx 常用功能
1. 正向代理與反向代理
-
正向代理:代理客戶端訪問無法直接訪問的服務器資源。
-
反向代理:代理服務器處理來自客戶端的請求,並將其轉發給內部網絡上的服務器,然後將結果返回給客戶端。反向代理常用於負載均衡和隱藏真實服務器地址。
2. 負載均衡
Nginx 支持多種負載均衡策略,包括輪詢、加權輪詢、IP hash 等,可以根據實際業務需求選擇合適的策略。
3. Web 緩存
Nginx 可以對靜態內容進行緩存,減少對後端服務器的請求,提高系統性能。同時,Nginx 也支持 FastCGI 緩存,適用於動態內容的緩存。
三、Nginx 配置文件結構
Nginx 的配置文件通常位於 /etc/nginx/
目錄下,主配置文件名爲 nginx.conf
。配置文件以區塊(block)的形式組織,主要包括全局塊、events 塊、http 塊等。
1. 全局塊
主要設置影響 Nginx 服務器整體運行的配置指令,如運行用戶、進程數、錯誤日誌等。
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
2. events 塊
影響 Nginx 服務器與用戶的網絡連接,如工作進程的最大連接數、事件驅動模型等。
events {
worker_connections 1024;
use epoll;
}
3. http 塊
配置代理、緩存、日誌、第三方模塊等,是服務器配置中最頻繁的部分。
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;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
}
四、常用配置示例
1. 反向代理配置
反向代理配置主要通過 proxy_pass
指令實現,將請求轉發到後端服務器。
server {
listen 80;
server_name example.com;
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 192.168.1.100:8080;
server 192.168.1.101:8080 backup;
}
2. 負載均衡配置
負載均衡配置通過 upstream
指令定義一組後端服務器,並通過反向代理將請求分發到這些服務器。
upstream myapp1 {
server backend1.example.com weight=5;
server backend2.example.com;
server backend3.example.com down;
}
server {
listen 80;
location / {
proxy_pass http://myapp1;
}
}
3. 日誌配置
Nginx 支持詳細的訪問日誌和錯誤日誌記錄,便於問題排查和性能分析。
http {
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;
error_log /var/log/nginx/error.log;
}
4. 緩存配置
開啓緩存可以顯著提高靜態資源的訪問速度。
http {
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=my_cache:10m max_size=10g
inactive=60m use_temp_path=off;
server {
location /static/ {
proxy_pass http://backend;
proxy_cache my_cache;
proxy_cache_valid 200 302 60m;
proxy_cache_valid 404 1m;
}
}
}
五、Nginx 常用命令
-
查看 Nginx 版本:
nginx -v
-
檢查配置文件語法:
nginx -t
-
啓動 Nginx:
systemctl start nginx
或/usr/sbin/nginx
-
停止 Nginx:
systemctl stop nginx
或/usr/sbin/nginx -s stop
-
重啓 Nginx:
systemctl restart nginx
或/usr/sbin/nginx -s reload
六、總結
Nginx 是一款功能強大的 Web 服務器和反向代理服務器,通過合理的配置可以顯著提高系統的性能和穩定性。本文介紹了 Nginx 的常用功能和配置方法,包括反向代理、負載均衡、Web 緩存等,並提供了詳細的配置示例和常用命令,希望能幫助讀者更好地掌握 Nginx 的配置技巧。
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源:https://mp.weixin.qq.com/s/U9FZCYqP5Pflgk1zzmdakQ