语法:listen address:port [ssl] [udp] [proxy_protocol] [backlog=number] [bind] [ipv6only=on|off] [reuseport] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];
反代配置实例:
实验目的:
将用户的请求分别调度至后端RS服务器
在两台RS服务器上面先配置好web服务:
反代服务器配置:
[root@CentOS7 nginx]# vim /etc/nginx/nginx.conf
在http段中添加以下配置:
upstream WebServer {
server 10.1.68.1 weight=1 max_fails=2;
server 10.1.68.2 weight=1 max_fails=2;
}
server段中的配置:
server {
listen 80;
server_name localhost;
#添加响应首部信息
add_header X-Via $server_addr;
add_header X-Accel $server_name;
#设定发往后端主机的请求报文的请求首部的值
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#将所有来自本机的请求发送WebServer服务器组,默认算法为rr
location / {
index index.html index.htm;
proxy_pass http://WebServer;
}
}
测试:
动静分离配置实例:
实验目的:
将不同的资源分别分发至不同服务器组,以实现动静分离机制
Nginx Reverse的配置:
在http段中添加以下配置:
upstream static {
server 10.1.68.1 weight=1 max_fails=2;
server 10.1.68.2 weight=1 max_fails=2;
}
upstream dynamic {
server 10.1.68.3 weight=1 max_fails=2;
server 10.1.68.4 weight=1 max_fails=2;
}
upstream picture {
server 10.1.68.5 weight=1 max_fails=2;
server 10.1.68.6 weight=1 max_fails=2;
}
server段中的配置:
server {
listen 80;
server_name localhost;
add_header X-Via $server_addr;
add_header X-Accel $server_name;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
location ~* ^/.*\.(html|htm|xml)$ { #将静态内容发往static服务器组
proxy_pass http://static;
}
location ~* ^/.*\.(jpg|png|gif)$ { #将图片资源发往picture服务器组
proxy_pass http://picture;
}