一:nginx能做什么
-
Web服务器
-
反向代理
-
负载均衡
-
正向代理
二:Web服务器
可以作为独立的 Web 服务器,直接处理和响应 HTTP/HTTPS 请求,静态文件(如 HTML、CSS、JavaScript、图片等)的传输效率非常高
简单配置
server {
listen 80;
server_name localhost;
client_max_body_size 1024M;
location / {
root /data/www/test;
index index.html;
}
# 所有静态请求都由nginx处理,存放目录为/data/www/file
location ~ \.(gif|jpg|jpeg|png|bmp|swf|css|js)$ {
root /data/www/file;
}
}
三:反向代理
反向代理应该是Nginx做的最多的一件事了,什么是反向代理呢,以下是百度百科的说法:反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。简单来说就是真实的服务器不能直接被外部网络访问,所以需要一台代理服务器,而代理服务器能被外部网络访问的同时又跟真实服务器在同一个网络环境,当然也可能是同一台服务器,端口不同而已。
简单配置
server {
listen 80;
server_name localhost;
client_max_body_size 1024M;
location / {
proxy_pass https://localhost:8080;
proxy_set_header Host $host:$server_port;
}
}
当我们访问localhost的时候,就相当于访问localhost:8080
四:负载均衡
负载均衡也是Nginx常用的一个功能,负载均衡其意思就是分摊到多个操作单元上进行执行,例如Web服务器、FTP服务器、企业关键应用服务器和其它关键任务服务器等,从而共同完成工作任务。简单而言就是当有2台或以上服务器时,根据规则随机的将请求分发到指定的服务器上处理,负载均衡配置一般都需要同时配置反向代理,通过反向代理跳转到负载均衡。而Nginx目前支持自带3种负载均衡策略,还有2种常用的第三方策略。
1:策略一:RR(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除此服务器
简单配置
upstream test {
server localhost:8080;
server localhost:8081;
}
server {
listen 80;
server_name localhost;
client_max_body_size 1024M;
location / {
proxy_pass https://test;
proxy_set_header Host $host:$server_port;
}
}
这里我配置了2台服务器,当我们访问localhost的时候,会默认跳转到localhost:8080,Nginx会自动判断服务器的状态,如果这个服务器处于不能访问(服务器挂了),就不会跳转到这台服务器而是跳转到localhost:8081,所以也避免了一台服务器挂了影响使用的情况
2:策略二:权重
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况
简单配置
upstream test {
server localhost:8080 weight=9;
server localhost:8081 weight=1;
}
server {
listen 80;
server_name localhost;
client_max_body_size 1024M;
location / {
proxy_pass https://test;
proxy_set_header Host $host:$server_port;
}
}
10次一般只会有1次会访问到8081,而有9次会访问到8080
3:策略三:ip_hash
上面的2种策略都有一个问题,那就是下一个请求来的时候请求可能分发到另外一个服务器,当我们的程序不是无状态的时候(采用了session保存数据),这时候就有一个很大的很问题了,比如把登录信息保存到了session中,那么跳转到另外一台服务器的时候就需要重新登录了,所以很多时候我们需要一个客户只访问一个服务器,那么就需要用iphash了,iphash的每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
简单配置
upstream test {
ip_hash;
server localhost:8080;
server localhost:8081;
}
server {
listen 80;
server_name localhost;
client_max_body_size 1024M;
location / {
proxy_pass https://test;
proxy_set_header Host $host:$server_port;
}
}
4:策略四:fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配
简单配置
upstream test {
fair;
server localhost:8080;
server localhost:8081;
}
server {
listen 80;
server_name localhost;
client_max_body_size 1024M;
location / {
proxy_pass https://test;
proxy_set_header Host $host:$server_port;
}
}
fair第三方拓展安装
(1):下载fair拓展:https://github.com/gnosek/nginx-upstream-fair
(2):安装fair拓展
./configure --with-http_ssl_module --add-module=/xxx/nginx-upstream-fair
make #注意千万不要执行make install 避免覆盖
mv /etc/nginx /etc/nginx_bak #备份原来的nginx执行文件
cp objs/nginx /etc/nginx #将编译好的nginx复制到nginx执行文件下
nginx -s reopen #重启
5:策略五:url_hash(第三方)
按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。 在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法
简单配置
upstream test {
hash $request_uri;
hash_method crc32;
server localhost:8080;
server localhost:8081;
}
server {
listen 80;
server_name localhost;
client_max_body_size 1024M;
location / {
proxy_pass https://test;
proxy_set_header Host $host:$server_port;
}
}
url_hash第三方拓展安装
(1):下载fair拓展:https://github.com/evanmiller/nginx_upstream_hash
(2):安装url_hash拓展
./configure --with-http_ssl_module --add-module=/xxx/nginx_upstream_hash
make #注意千万不要执行make install 避免覆盖
mv /etc/nginx /etc/nginx_bak #备份原来的nginx执行文件
cp objs/nginx /etc/nginx #将编译好的nginx复制到nginx执行文件下
nginx -s reopen #重启
五:正向代理
正向代理,意思是一个位于客户端和原始服务器(origin server)之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并指定目标(原始服务器),然后代理向原始服务器转交请求并将获得的内容返回给客户端。客户端才能使用正向代理。当你需要把你的服务器作为代理服务器的时候,可以用Nginx来实现正向代理
简单配置
server {
resolver 8.8.8.8; # 配置指定DNS解析IP地址(在/etc/resolv.conf查看)
resolver_timeout 5s; #超时时间(5秒)
# 监听端口
listen 8080;
#access_log /home/reistlin/logs/proxy.access.log;
#error_log /home/reistlin/logs/proxy.error.log;
location / {
# 配置正向代理参数
proxy_pass https://$https_host$request_uri;
# 解决如果URL中带"."后Nginx 503错误
proxy_set_header Host $https_host;
# 配置缓存大小
proxy_buffers 256 4k;
# 关闭磁盘缓存读写减少I/O
proxy_max_temp_file_size 0;
# 代理连接超时时间
proxy_connect_timeout 30;
# 配置代理服务器HTTP状态缓存时间
proxy_cache_valid 200 302 10m;
proxy_cache_valid 301 1h;
proxy_cache_valid any 1m;
}
}