Nginx基础

1.反向代理及负载均衡
Nginx实现负载均衡用到了proxy_pass代理模块核心配置,将客户端请求代理转发至一组upstream虚拟服务池。

1)upstream配置语法

Syntax: upstream name { ... }
Default: -
Context: http

#upstream示例
upstream backend {
server backend1.example.com weight=5;
server backend2.example.com:8080;
server unix:/tmp/backend3;
server backup1.example.com:8080 backup;
}
server {
location / {
proxy_pass http://backend;
}
}

2)Nginx负载均衡状态

down 当前的server暂时不参与负载均衡
backup	预留的备份服务器
max_fails	允许请求失败的次数
fail_timeout	经过max_fails失败后, 服务暂停时间
max_conns	限制最大的接收连接数

3)Nginx负载均衡调度策略

轮询	按时间顺序逐一分配到不同的后端服务器(默认)
weight	加权轮询,weight值越大,分配到的访问几率越高
ip_hash	每个请求按访问IP的hash结果分配,这样来自同一IP的固定访问一个后端服务器
url_hash	按照访问URL的hash结果来分配请求,是每个URL定向到同一个后端服务器
least_conn	最少链接数,那个机器链接数少就分发
hash关键数值	hash自定义的key

2.反向代理参数配置

[root@localhost ~]# vim /etc/nginx/proxy_params
proxy_redirect default;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;

proxy_buffer_size 32k;
proxy_buffering on;
proxy_buffers 4 128k;
proxy_busy_buffers_size 256k;
proxy_max_temp_file_size 256k;

#具体location实现
location / {
proxy_pass http://127.0.0.1:8080;
include proxy_params;
}

3.Nginx实现四层代理

#Nginx四层代理仅能存在于main段(nginx1.9版本后实现)
stream {
upstream ssh_proxy {
hash $remote_addr consistent;
server 10.0.0.12:22;
}
upstream mysql_proxy {
hash $remote_addr consistent;
server 10.0.0.12:3306;
}
server {
listen 20022;
proxy_connect_timeout 1s;
proxy_timeout 300s;
proxy_pass ssh_proxy;
}
server {
listen 23306;
proxy_connect_timeout 1s;
proxy_timeout 300s;
proxy_pass mysql_proxy;
}
}

4.案例
1)反向代理实现动静分离

upstream static {
server 10.0.0.12:80;
}

upstream java {
server 10.0.0.13:8080;
}

server {
listen 80;
server_name 127.0.0.1;

location / {
root /soft/code;
index index.html;
}

location ~ .*.(png|jpg|gif)$ {
proxy_pass http://static;
include proxy_params;
}

location ~ .*.jsp$ {
proxy_pass http://java;
include proxy_params;
}

}

2)不同客户端显示不同信息

#通过浏览器来分别连接不同的浏览器访问不同的效果。
http {
...
upstream firefox {
server 10.0.0.12:80;
}
upstream chrome {
server 10.0.0.13:80;
}
upstream iphone {
server 10.0.0.14:80;
}
upstream android {
server 10.0.0.15:80;
}
upstream default {
server 10.0.0.11:80;
}
...
}

#server根据判断来访问不同的页面
server {
listen 80;
server_name 127.0.0.1;

#safari浏览器访问的效果
location / {
if ($http_user_agent ~* "Safari"){
proxy_pass http://dynamic_pools;
} 
#firefox浏览器访问效果
if ($http_user_agent ~* "Firefox"){
proxy_pass http://static_pools;
}
#chrome浏览器访问效果
if ($http_user_agent ~* "Chrome"){
proxy_pass http://chrome;
} 

#iphone手机访问效果
if ($http_user_agent ~* "iphone"){
proxy_pass http://iphone;
}

#android手机访问效果
if ($http_user_agent ~* "android"){
proxy_pass http://and;
}

#其他浏览器访问默认规则
proxy_pass http://dynamic_pools;
include proxy.conf;
}
}
}

3)访问不同目录代理到不同服务器

#默认动态,静态直接找设置的static,上传找upload
upstream static_pools {
server 10.0.0.12:80 weight=1;
}
upstream upload_pools {
server 10.0.0.13:80 weight=1;
}
upstream default_pools {
server 10.0.0.11:80 weight=1;
}

server {
listen 80;
server_name 127.0.0.1;

location / { 
proxy_pass http://default_pools;
include proxy.conf;
}

location /static/ {
proxy_pass http://static_pools;
include proxy.conf;
}

location /upload/ {
proxy_pass http://upload_pools;
include proxy.conf;
}
}

#方案2:以if语句实现。
if ($request_uri ~* "^/static/(.*)$")
{
proxy_pass http://static_pools/$1;
}
if ($request_uri ~* "^/upload/(.*)$")
{
proxy_pass http://upload_pools/$1;
}
location / { 
proxy_pass http://default_pools;
include proxy.conf;
}
原文地址:https://www.cnblogs.com/Wang-Hongwei/p/13274653.html