LearningNginx反向proxy configurationmethod and application场景, improving网站 security性 and performance
反向proxy is aserverconfiguration, 它接收客户端 request, 然 after 将request转发给 after 端server, 最 after 将 after 端server response返回给客户端. and 正向proxy不同, 反向proxy for 客户端 is 透明 , 客户端不知道自己 request被转发 to 了 after 端server.
usingNginx proxy_pass指令可以configuration反向proxy.
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://localhost:8080;
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_set_header X-Forwarded-Proto $scheme;
}
}
指定 after 端server 地址, 可以 is IP地址, 域名 or Unix套接字
proxy_pass http://localhost:8080;
设置传递给 after 端server HTTP头
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_set_header X-Forwarded-Proto $scheme;
设置proxy缓冲区 big small
proxy_buffers 8 16k; proxy_buffer_size 32k;
设置 and after 端server建立连接 超时时间
proxy_connect_timeout 60s;
设置 from after 端server读取response 超时时间
proxy_read_timeout 60s;
将客户端requestproxy to after 端 applicationserver, such asTomcat, Node.js, Djangoetc..
server {
listen 80;
server_name app.example.com;
location / {
proxy_pass http://localhost:8080;
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_set_header X-Forwarded-Proto $scheme;
}
}
serving asAPIgateway, 将requestrouting to 不同 after 端APIservice.
server {
listen 80;
server_name api.example.com;
location /user/ {
proxy_pass http://localhost:8080/user/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location /product/ {
proxy_pass http://localhost:8081/product/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location /order/ {
proxy_pass http://localhost:8082/order/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
usingNginxprocessing静态file, 将动态requestproxy to after 端server.
server {
listen 80;
server_name example.com;
root /var/www/example.com;
index index.html;
# processing静态file
location ~* \.(css|js|jpg|jpeg|png|gif|ico|svg)$ {
expires 30d;
add_header Cache-Control "public, max-age=2592000";
}
# proxy动态request
location /api/ {
proxy_pass http://localhost:8080/api/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
# proxyother动态 in 容
location /app/ {
proxy_pass http://localhost:8080/app/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
in Nginx on processingSSL/TLSencryption and decryption, after 端serverusingHTTP.
server {
listen 443 ssl http2;
server_name example.com;
# SSLconfiguration
ssl_certificate /etc/nginx/ssl/example.com.crt;
ssl_certificate_key /etc/nginx/ssl/example.com.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
location / {
proxy_pass http://localhost:8080;
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_set_header X-Forwarded-Proto $scheme;
}
}
# 重定向HTTP to HTTPS
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
fake设我们 has 一个run in 3000端口 Node.jsapplication, 需要throughNginx反向proxy使其可以through80端口访问:
server {
listen 80;
server_name nodeapp.example.com;
# 访问log and errorlog
access_log /var/log/nginx/nodeapp.access.log main;
error_log /var/log/nginx/nodeapp.error.log warn;
# 反向proxyconfiguration
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_set_header X-Forwarded-Proto $scheme;
# proxy缓冲区设置
proxy_buffers 16 16k;
proxy_buffer_size 32k;
# 超时设置
proxy_connect_timeout 60s;
proxy_read_timeout 60s;
proxy_send_timeout 60s;
}
# processing静态file (such as果Node.jsapplication也providing静态file)
location ~* \.(css|js|jpg|jpeg|png|gif|ico|svg)$ {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
expires 30d;
add_header Cache-Control "public, max-age=2592000";
}
}
这个configuration会将所 has for nodeapp.example.com requestproxy to 本地run in 3000端口 Node.jsapplication, 并设置了适当 HTTP头, 缓冲区 and 超时parameter.
in 反向proxy时, 可以usingrewrite指令modifyURL.
server {
listen 80;
server_name example.com;
location /old-path/ {
rewrite ^/old-path/(.*)$ /new-path/$1 break;
proxy_pass http://localhost:8080;
proxy_set_header Host $host;
}
}
可以将requestproxy to Unix套接字, 这通常比TCP连接更 high 效.
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://unix:/var/run/app.sock;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
可以configurationhealthycheck, 确保只向healthy after 端server发送request.
upstream backend {
server localhost:8080 max_fails=3 fail_timeout=30s;
server localhost:8081 max_fails=3 fail_timeout=30s;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
# healthycheck端点
location /health {
proxy_pass http://backend/health;
proxy_set_header Host $host;
}
}
1. 反向proxy 主要优势 is what? ( many 选)
2. 以 under 哪个指令用于configuration反向proxy after 端server地址?
3. 实践练习: configuration反向proxy
请configuration一个Nginx反向proxy, 将 for api.example.com requestproxy to run in localhost:8080 after 端APIservice. 要求:
through本tutorial, 您已经Learning了Nginx反向proxy configurationmethod and application场景. 反向proxy is Nginx important functions, 它可以:
in after 续 tutorialin, 我们将深入LearningNginx load balancing, 静态fileservice, securityconfiguration, performanceoptimizationetc.advanced features, helping您全面MasterNginx usingtechniques.