最近Nginx部署用的多点,学过的tomcat仅仅适合用于java项目。
简介
开源的高性能 HTTP 服务器和反向代理服务器
IMAP/POP3 邮件代理服务器
特点
- 高并发处理能力
- 反向代理和负载均衡
- 动静分离
- 高效的静态文件服务
- 支持多种协议
反向代理
反向代理充当客户端和后端服务器之间的中间层。它的作用是接收客户端的请求,转发给后端服务器处理,然后将处理结果返回给客户端。使用反向代理的主要目的是提高系统性能、安全性和可扩展性。Nginx 和 Apache 等服务器软件通常被配置为反向代理。
单个服务器使用反向代理
可以用于分发请求、缓存静态资源、隐藏后端服务等
优势:
安全性:隐藏后端服务器的 IP,防止直接攻击。
可扩展性:在需要扩展时,可以在同一 Nginx 配置下代理多个应用或服务。
性能优化:通过缓存静态资源和 SSL 卸载,提升整体性能。
多个服务器使用反向代理
后端服务器不需要直接连接互联网,只要它们和反向代理服务器(例如 Nginx)在同一个内网中。
优点
- 安全性
- 后端服务器的 IP 地址不会暴露在公网上,降低了受到外部攻击的风险。
- 可以通过 Nginx 过滤和控制访问,进一步增强安全性。
- 负载均衡和容错
- Nginx 可以使用负载均衡策略在内网中将请求分发到多台服务器上,提高系统的整体并发能力。
- 通过健康检查,Nginx 可以检测后端服务器的状态,自动跳过故障服务器,提高系统容错能力。
- 简化配置
- 后端服务器只需与反向代理服务器(Nginx)进行通讯,而不需要考虑公网的 IP 和 DNS 配置,简化了网络配置。
- 通过内网 IP 直接访问,减少了外部因素的影响,通信效率更高。
Nginx配置案例
1. 静态网站服务
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
| server { listen 80; server_name example.com www.example.com; root /var/www/example.com; index index.html index.htm; location / { try_files $uri $uri/ =404; } location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ { expires 30d; access_log off; gzip_static on; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; }
|
2. 反向代理
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
| server { listen 80; server_name api.example.com; client_max_body_size 10m; location / { proxy_pass http://localhost:3000; 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_connect_timeout 60; proxy_read_timeout 90; proxy_send_timeout 90; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_buffering off; } location /health { proxy_pass http://localhost:3000/health; access_log off; } }
|
3. 负载均衡
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
| upstream backend { server backend1.example.com weight=3; server backend2.example.com; server backend3.example.com backup; keepalive 32; }
server { listen 80; server_name app.example.com; gzip on; gzip_types text/plain text/css application/json application/javascript; location / { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Proto $scheme; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; proxy_connect_timeout 2s; } location /nginx_status { stub_status on; access_log off; allow 192.168.1.0/24; deny all; } }
|
4. HTTPS安全配置
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
| server { listen 80; server_name secure.example.com; return 301 https://$host$request_uri; }
server { listen 443 ssl http2; server_name secure.example.com; ssl_certificate /etc/letsencrypt/live/secure.example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/secure.example.com/privkey.pem; ssl_stapling on; ssl_stapling_verify on; ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384'; ssl_prefer_server_ciphers on; ssl_session_cache shared:SSL:10m; ssl_session_timeout 1d; add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always; add_header X-Content-Type-Options nosniff; add_header X-Frame-Options DENY; add_header X-XSS-Protection "1; mode=block"; root /var/www/secure.example.com; index index.html; location / { try_files $uri $uri/ =404; } location ~ /\. { deny all; access_log off; log_not_found off; } }
|
5. 高级URL重写
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
| server { listen 80; server_name blog.example.com; root /var/www/blog; index index.php index.html index.htm; location /old-blog { return 301 /blog$request_uri; } location /blog { rewrite ^/blog/(.*)$ /$1 break; try_files $uri $uri/ /index.php?$args; } location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_cache my_cache; fastcgi_cache_valid 200 301 302 1h; fastcgi_cache_use_stale error timeout invalid_header updating; } location ~* (\.env|\.git|\.svn) { deny all; access_log off; log_not_found off; } location ~* \.(?:ico|css|js|gif|jpe?g|png|webp)$ { expires 365d; access_log off; add_header Cache-Control "public"; } }
|