Nginx作为高性能的Web服务器,其核心功能主要体现在以下四大模块:
一、正向代理
主要用于客户端访问外部网络的中转服务。典型配置示例:
server {
listen 8080;
resolver 8.8.8.8;
location / {
proxy_pass http://$http_host$request_uri;
proxy_set_header Host $http_host;
}
}
注:需配合客户端代理设置使用
二、反向代理
核心应用场景是负载均衡与服务隐藏,常见配置:
upstream backend {
server 10.0.0.1:8000 weight=3;
server 10.0.0.2:8000;
}
server {
location / {
proxy_pass http://backend;
proxy_set_header X-Real-IP $remote_addr;
}
}
支持7种负载策略:轮询、权重、IP哈希等
三、缓存加速
多级缓存配置示例:
proxy_cache_path /data/cache levels=1:2 keys_zone=my_cache:10m;
server {
location / {
proxy_cache my_cache;
proxy_cache_valid 200 302 10m;
proxy_cache_use_stale er