培训制作网站,网页入口网站推广,做随车吊网站要多大,wordpress链接设置方法文章目录 四、反向代理配置4.1 proxy_pass效果1—路径重写效果2—转发到其他服务器 4.2 proxy_pass使用规则4.3 proxy_set_header4.3.1 修改请求协议 五、负载均衡配置5.1 upstream5.2 server5.3 负载均衡策略5.3.1 轮询5.3.2 加权轮询5.3.3 最少连接5.3.3 ip_hash#xff1a;… 文章目录 四、反向代理配置4.1 proxy_pass效果1—路径重写效果2—转发到其他服务器 4.2 proxy_pass使用规则4.3 proxy_set_header4.3.1 修改请求协议 五、负载均衡配置5.1 upstream5.2 server5.3 负载均衡策略5.3.1 轮询5.3.2 加权轮询5.3.3 最少连接5.3.3 ip_hash基于IP的负载均衡算法 六、SSL/TLS配置七、缓存配置八、日志配置九、性能优化9.1 gzip启用压缩配置9.2 keepalive_timeout保持连接超时时间9.3 client_body_buffer_size客户端请求体缓冲区大小 回顾 Nginx 配置文件的完整指南 (一)
四、反向代理配置 4.1 proxy_pass proxy_pass指令用于设置反向代理的目标地址。
效果1—路径重写
请求路径http://localhost:80/test最终路径http://localhost:8800/test1
server {listen 80; # 监听80端口的所有请求location /test {proxy_pass http://localhost:8800/test1/;}
}效果2—转发到其他服务器
请求路径http://localhost:80/test最终路径http://xxx.xxx.xxx:8800/test1
server {listen 80; # 监听80端口的所有请求location /test {proxy_pass http://xxx.xxx.xxx:8800/test1/;}
}4.2 proxy_pass使用规则 在nginx中配置proxy_pass代理转发时如果在proxy_pass后面的url加/表示绝对根路径如果没有/表示相对路径把匹配的路径部分也给代理走。
第一种url末尾有/ 请求路径http://127.0.0.1/test/a.html 最终路径http://xxx.xxx.xxx/a.html
location /test {proxy_pass http://xxx.xxx.xxx/;
}第二种url末尾没/ 请求路径http://127.0.0.1/test/a.html 最终路径http://xxx.xxx.xxx/test/a.html
location /test {proxy_pass http://xxx.xxx.xxx;
}第三种 请求路径http://127.0.0.1/test/a.html 最终路径http://xxx.xxx.xxx/sss/a.html
location /test {proxy_pass http://xxx.xxx.xxx/ssss/;
}第四种 请求路径http://127.0.0.1/test/a.html 最终路径http://xxx.xxx.xxx/ssstest/a.html
location /test/ {proxy_pass http://xxx.xxx.xxx/sss;
}4.3 proxy_set_header 用于设置反向代理请求的头信息我们可以根据需要自定义、修改或删除请求头字段以满足代理服务器的要求或与后端服务器进行正确的通信。
location / {# 设置请求头中的 Host 字段为当前请求的主机名proxy_set_header Host $host; # 设置请求头中的 X-Real-IP 字段为客户端的 IP 地址proxy_set_header X-Real-IP $remote_addr; # 设置请求头中的 X-Forwarded-For 字段为客户端的 IP 地址proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 设置请求头中的 User-Agent 字段为客户端的 User-Agent proxy_set_header User-Agent $http_user_agent; # 将请求转发给名为 backend 的后端服务器proxy_pass http://backend;
}
4.3.1 修改请求协议 没错还能修改请求协议下面的例子是把HTTP协议修改为webSocket
location /socket {rewrite ^/socket/(.*)$ /$1 break; #拦截标识去除proxy_pass http://cim.tfjy.tech:34567;# 这里是http不是ws不用怀疑proxy_http_version 1.1; # http 1.1# 请求协议设置为webSocketproxy_set_header Upgrade $http_upgrade;proxy_set_header Connection upgrade;
}五、负载均衡配置 5.1 upstream upstream指令用于定义后端服务器组即负载均衡的目标服务器列表。 upstream指令定义了一个名为backend_servers的后端服务器组包含了三个服务器
upstream backend_servers {server backend1.xxx.com;server backend2.xxx.com;server backend3.xxx.com;
}5.2 server server块用于配置一个负载均衡服务器。location块中的proxy_pass指令将请求转发到backend_servers后端服务器组。
http {upstream backend_servers {server backend1.xxx.com;server backend2.xxx.com;server backend3.xxx.com;}server {location / {proxy_pass http://backend_servers; # backend_servers是后端服务器组名可以任意命名}}
}5.3 负载均衡策略 Nginx 负载均衡是指使用 Nginx 作为反向代理服务器将传入的请求分发到多个后端服务器以平衡服务器负载并提高系统的可用性和性能。 在 Nginx 的配置文件中可以指定不同的负载均衡策略来分配请求到后端服务器。
5.3.1 轮询 轮询是默认的负载均衡策略它按照请求的顺序逐个将请求分配给后端服务器。
upstream backend {server backend1.xxx.com;server backend2.xxx.com;server backend3.xxx.com;
}server {location / {proxy_pass http://backend ;}}5.3.2 加权轮询 加权轮询策略根据后端服务器的权重值分配请求权重越高的服务器接收到的请求越多。
upstream backend {server backend1.xxx.com weight3;server backend2.xxx.com weight2;server backend3.xxx.com weight1;
}server {location / {proxy_pass http://backend ;}}5.3.3 最少连接 最少连接策略将请求分配给当前连接数最少的后端服务器。 upstream backend {least_conn;server backend1.xxx.com;server backend2.xxx.com;server backend3.xxx.com;}server {location / {proxy_pass http://backend ;}}5.3.3 ip_hash基于IP的负载均衡算法 基于客户端的IP地址进行负载均衡使得同一IP的请求始终被转发到同一台后端服务器解决session不共享问题。
http {upstream backend {ip_hash;server backend1.xxx.com;server backend2.xxx.com;server backend3.xxx.com;}server {location / {proxy_pass http://backend ;}}
}六、SSL/TLS配置 通过配置SSL证书路径、SSL证书私钥路径、SSL/TLS协议支持版本和SSL加密算法可以实现安全的HTTPS通信。 ssl_certificate设置SSL证书的路径。 ssl_certificate_key设置SSL证书的私钥路径为 ssl_protocols设置SSL/TLS协议的支持版本 ssl_ciphers 指定SSL加密算法
server {listen 443 ssl;ssl_certificate /path/to/certificate.crt;ssl_certificate_key /path/to/private.key;ssl_protocols TLSv1.2 TLSv1.3; # 支持版本为TLSv1.2和TLSv1.3ssl_ciphers AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256;
}七、缓存配置 通过配置反向代理缓存路径、缓存键和缓存有效期可以实现对反向代理响应的缓存。这样可以提高响应速度并减轻后端服务器的负载。根据实际需求可以进一步调整缓存路径、缓存键和缓存有效期的设置。 proxy_cache_path设置了反向代理缓存的存储路径 levels1:2表示缓存目录的层级结构为1层目录和2层子目录keys_zonemy_cache:10m定义了缓存区域的名称为my_cache并分配了10MB的内存空间 proxy_cache启用了反向代理缓存并将缓存区域设置为my_cache proxy_cache_valid设置了缓存的有效期对于状态码为200的响应缓存的有效期为1小时 proxy_cache_key缓存的键为$scheme$host$request_uri即根据请求的协议、主机名和URI来生成缓存的键。
http {proxy_cache_path /path/to/cache levels1:2 keys_zonemy_cache:10m;server {location / {proxy_pass http://backend_server;proxy_cache my_cache;proxy_cache_valid 200 1h;proxy_cache_key $scheme$host$request_uri;}}
}八、日志配置 可以通过配置访问日志路径和格式以及错误日志路径和级别来记录服务器的访问日志和错误信息。根据实际需求可以选择合适的日志路径和级别以及自定义日志格式。
access_log配置访问日志的路径和格式error_log配置错误日志的路径和级别
http {access_log /path/to/access.log;error_log /path/to/error.log;
}九、性能优化 9.1 gzip启用压缩配置 gzip指令用于启用压缩功能并配置相应的压缩参数。
gzip on表示启用压缩功能gzip_types指定了需要进行压缩的文件类型如文本文件、CSS文件和JavaScript文件gzip_min_length指定了最小压缩文件大小的阈值只有大于等于该阈值的文件才会进行压缩。
http {gzip on;gzip_types text/plain text/css application/javascript;gzip_min_length 1000;
}9.2 keepalive_timeout保持连接超时时间 keepalive_timeout指令用于设置保持连接的超时时间。
示例代码
http {keepalive_timeout 60s;
}9.3 client_body_buffer_size客户端请求体缓冲区大小 通过配置压缩功能、保持连接超时时间和客户端请求体缓冲区大小可以进行性能优化提高服务器的响应速度和资源利用率。根据实际需求可以调整压缩参数、超时时间和缓冲区大小的设置。
client_body_buffer_size指令用于设置客户端请求体缓冲区的大小。
http {client_body_buffer_size 8k;
}