Rumah > Artikel > pembangunan bahagian belakang > Nginx服务器配置指令
Nginx服务器配置指令:
Rewrite功能,代理功能
Rewrite功能
后端服务器组的配置指令
upstream指令
upstream指令是设置后端服务器组的主要指令
<code>upstream name {<span>...</span>} </code>
请求按照轮叫调度(Round-Robin, RR)策略顺序选择服务器处理
server
server指令用于设置组内的服务器
<code>server address [<span>params</span>];</code>
ip_hash指令
ip_hash指令用于实现会话保持功能,将某个客户端的多次请求定向到组内同一台服务器上,保证客户端与服务器之间建立稳定的会话。
注:ip_hash指令不能与weight变量一起使用,在整个系统中,Nginx服务器必须处于最前端的服务器,而且客户端地址必须为C类地址
keepalive指令
keepalive指令用于控制网络连接保持功能
<code>keepalive connections;</code>
设置服务器的每一个工作进程允许该服务器组保持的空闲网络连接数的上限值
least_conn指令
least_conn指令用于配置Nginx服务器使用负载均衡策略为网络连接分配服务器组内的服务器,将请求分配给当前网络连接最少的服务器
Rewrite功能的配置指令
Rewrite功能的多种应用
代理功能
Nginx正向代理服务配置指令
resolver指令
resolver指令用于指定DNS服务器的IP地址
<code>resolver address <span>...</span> [valid=time];</code>
resolve_timeout指令
resolve_timeout指令用于设置DNS服务器域名解析超时时间
<code>resolve_timeout <span>time</span>;</code>
proxy_pass指令
proxy_pass指令用于设置代理服务器的协议和地址
<code>proxy_pass <span>URL</span>;</code>
Nginx反向代理服务配置指令
proxy_pass指令
proxy_pass指令用来设置被代理服务器的地址,可以是主机名称、IP地址加端口号形式等
<code>proxy_pass <span>URL</span>;</code>
proxy_hide_header指令
proxy_hide_header指令用于设置Nginx服务器在发送HTTP响应时,隐藏一些头域信息
<code>proxy<span>\_</span>hide_header field</code>
proxy_pass_header指令
proxy_pass_header指令用于设置那些头域信息被发送
<code>proxy<span>\_</span>hide_header field</code>
proxy_pass_header指令
proxy_pass_header指令用于设置那些头域信息被发送
<code>proxy<span>\_</span>hide_header field</code>
略
Nginx反向代理服务-负载均衡
一般轮询规则的负载均衡
<code><span>...</span> upstream backend { server <span>192.168</span><span>.1</span><span>.2</span>:<span>80</span>; server <span>192.168</span><span>.1</span><span>.3</span>:<span>80</span>; server <span>192.168</span><span>.1</span><span>.4</span>:<span>80</span>; } server { listen <span>80</span>; server_name www.mysite.name; index index.html index.htm; location / { proxy_pass http://backend; proxy_set_header Host $host; <span>...</span> } <span>...</span> }</code>
加权轮询规则的负载均衡
<code><span>...</span> upstream backend { server <span>192.168</span><span>.1</span><span>.2</span>:<span>80</span> weight=<span>5</span>; server <span>192.168</span><span>.1</span><span>.3</span>:<span>80</span> weight=<span>2</span>; server <span>192.168</span><span>.1</span><span>.4</span>:<span>80</span>; } server { listen <span>80</span>; server_name www.mysite.name; index index.html index.htm; location / { proxy_pass http://backend; proxy_set_header Host $host; <span>...</span> } <span>...</span> }</code>
特定资源的负载均衡
<code><span>...</span> upstream videobackend { server <span>192.168</span><span>.1</span><span>.2</span>:<span>80</span>; server <span>192.168</span><span>.1</span><span>.3</span>:<span>80</span>; server <span>192.168</span><span>.1</span><span>.4</span>:<span>80</span>; } upstream filebackend { server <span>192.168</span><span>.1</span><span>.5</span>:<span>80</span>; server <span>192.168</span><span>.1</span><span>.6</span>:<span>80</span>; server <span>192.168</span><span>.1</span><span>.7</span>:<span>80</span>; } server { listen <span>80</span>; server_name www.mysite.name; index index.html index.htm; location /video/ { proxy_pass http://videobackend; proxy_set_header Host $host; <span>...</span> } location /file/ { proxy_pass http://filebackend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; <span>...</span> } <span>...</span> }</code>
对不同域名实现负载均衡
<code><span>...</span> upstream bbsbackend{ server <span>192.168</span><span>.1</span><span>.2</span>:<span>80</span> weight=<span>2</span>; server <span>192.168</span><span>.1</span><span>.3</span>:<span>80</span> weight=<span>2</span>; server <span>192.168</span><span>.1</span><span>.4</span>:<span>80</span>; } upstream homebackend { server <span>192.168</span><span>.1</span><span>.4</span>:<span>80</span>; server <span>192.168</span><span>.1</span><span>.5</span>:<span>80</span>; server <span>192.168</span><span>.1</span><span>.6</span>:<span>80</span>; } server { listen <span>80</span>; server_name home.mysite.name; index index.html index.htm; location / { proxy_pass http://homebackend; proxy_set_header Host $host; <span>...</span> } <span>...</span> } server { listen <span>81</span>; server_name bbs.mysite.name; index index.html index.htm; location / { proxy_pass http://bbsbackend; proxy_set_header Host $host; <span>...</span> } <span>...</span> }</code>
带有URL重写的负载均衡
<code><span>...</span> upstream backend{ server <span>192.168</span><span>.1</span><span>.2</span>:<span>80</span>; server <span>192.168</span><span>.1</span><span>.3</span>:<span>80</span>; server <span>192.168</span><span>.1</span><span>.4</span>:<span>80</span>; } server { listen <span>80</span>; server_name www.mysite.name; index index.html index.htm; location /file/ { rewrite ^(/file/.*)/media/(.*)\.*$ $<span>1</span>/mp3/$<span>2.</span>mp3 last; } location / { proxy_pass http://backend; proxy_set_header Host $host; <span>...</span> } }</code>
以上就介绍了Nginx服务器配置指令,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。