Nginx反向代理和缓存杂记

Nginx反向代理

反向代理实验

1、准备node1,node2两台节点,node1反向至node2,node2配置wed服务

2、node2启动web服务

3、配置node1的nginx反向代理

3.1 备份配置文件

[[email protected] nginx]# cd conf.d/
[[email protected] conf.d]# cp default.conf{,.bak}

3.2 node1配置反向代理至后端服务器

[[email protected] conf.d]# vim default.conf

location / {
    #root   /usr/share/nginx/html;
    proxy_pass http://10.201.106.22/;

3.3 重载nginx服务

[[email protected] conf.d]# service nginx configtest
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[[email protected] conf.d]# service nginx reload
Reloading nginx:                                           [  OK  ]

3.4 测试访问http://10.201.106.21能够成功跳转至node2的网页

3.5 查看node2访问日志,记录的是Client的IP

[[email protected] ~]# tail -1 /var/log/httpd/access_log
10.201.106.1 - - [12/Dec/2016:00:10:31 +0800] "GET /favicon.ico HTTP/1.1" 404 288 "http://10.201.106.22/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36"

4、只代理某个请求

4.1 node2节点配置新的网站目录

[[email protected] ~]# cd /var/www/html/
[[email protected] html]# ls
index.html
[[email protected] html]# mkdir bbs
[[email protected] html]# vim bbs/index.html

<h1>bbs on node2</h1>

4.2 node1配置反向代理

location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    # example
    #ModSecurityEnabled on;
    #ModSecurityConfig /etc/nginx/modsecurity.conf;
}

location /bbs/ {
    proxy_pass http://10.201.106.22/bbs/;
}

[[email protected] conf.d]# service nginx reload
Reloading nginx:                                           [  OK  ]

4.3 访问http://10.201.106.21/bbs/能够跳转到node2的界面

4.4 测试将node1的反向配置,前端改成错误的后再测试

location /qqq/ {
    proxy_pass http://10.201.106.22/bbs/;
}

[[email protected] conf.d]# service nginx reload
Reloading nginx:                                           [  OK  ]

测试:http://10.201.106.21/qqq/
可以访问到node2的bbs页面

实际是node1请求的
[[email protected] html]# tail -1 /var/log/httpd/access_log
10.201.106.21 - - [12/Dec/2016:02:10:01 +0800] "GET /bbs/ HTTP/1.0" 200 22 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36"

4.5 只有前端,没有后端测试

location /forum/ {
    proxy_pass http://10.201.106.22/;
}

[[email protected] conf.d]# service nginx reload
Reloading nginx:                                           [  OK  ]

测试后跳转到主页了,这也是一个URL

4.6 匹配后缀名,跳转

location ~* \.(jpg|png|gif)$ {
    proxy_pass http://10.201.106.22;
}

语法检查:
[[email protected] conf.d]# service nginx configtest
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[[email protected] conf.d]# 

重载服务
[[email protected] conf.d]# service nginx reload
Reloading nginx:                                           [  OK  ]
[[email protected] conf.d]# 

上传图片到node2节点

访问:http://10.201.106.21/bg.jpg可以访问到node2的图片

4.7 放到目录下的图片访问

上传图片到node2节点;
[[email protected] html]# mkdir images
[[email protected] html]# cd images/
[[email protected] images]# ls
2.jpg
[[email protected] images]# 

访问测试,可以看到图片
http://10.201.106.21/images/2.jpg

4.8 修改后端路径,期望放到/images下

location ~* \.(jpg|png|gif)$ {
    proxy_pass http://10.201.106.22/images/;
}

第一种例外
语法错误,模式匹配,后面就不能再跟上URL,连/也不能加
[[email protected] conf.d]# service nginx configtest
nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block in /etc/nginx/conf.d/default.conf:25
nginx: configuration file /etc/nginx/nginx.conf test failed
[[email protected] conf.d]# 

4.9 第二种例外,location如果有重写,重写后的结果

发送到后端的值,向后端发送特定首部

1、反向服务器将客户端真实IP发送给node2网站服务器

1.1 node1 配置

[[email protected] conf.d]# vim default.conf

location /forum/ {
    proxy_pass http://10.201.106.22/;
    proxy_set_header HOST $host;
    proxy_set_header X-Real-IP $remote_addr;
}

location ~* \.(jpg|png|gif)$ {
    proxy_pass http://10.201.106.22;
    proxy_set_header X-Real-IP $remote_addr;
}

1.2 服务重载

[[email protected] conf.d]# service nginx configtest
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[[email protected] conf.d]#
[[email protected] conf.d]# service nginx reload
Reloading nginx:                                           [  OK  ]
[[email protected] conf.d]# 

1.3 定义node2后端服务器的日志格式

记录日志首部的值
[[email protected] images]# vim /etc/httpd/conf/httpd.conf 

#LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%{X-Real-IP}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

重启服务
[[email protected] images]# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]
[[email protected] images]# 

1.4 更改日志格式后,相比前两条,最后两条的访问日志记录已经变成真正的客户端主机IP了

10.201.106.21 - - [12/Dec/2016:06:57:21 +0800] "GET / HTTP/1.0" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36"
10.201.106.21 - - [12/Dec/2016:06:57:39 +0800] "GET / HTTP/1.0" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36"
10.201.106.1 - - [12/Dec/2016:07:05:37 +0800] "GET / HTTP/1.0" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36"
10.201.106.1 - - [12/Dec/2016:07:05:38 +0800] "GET / HTTP/1.0" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36"
[[email protected] images]# 

Nginx缓存

1、定义node1节点缓存配置,1条命令

[[email protected] ~]# cd /etc/nginx/
[[email protected] nginx]# vim nginx.conf

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  ‘$remote_addr - $remote_user [$time_local] "$request" ‘
                      ‘$status $body_bytes_sent "$http_referer" ‘
                      ‘"$http_user_agent" "$http_x_forwarded_for"‘;

    access_log  /var/log/nginx/access.log  main;

    proxy_cache_path /cache/nginx/ levels=1:1 keys_zone=mycache:32m;

    sendfile        on;

创建缓存目录,修改权限
[[email protected] nginx]# mkdir -pv /cache/nginx
mkdir: created directory `/cache‘
mkdir: created directory `/cache/nginx‘
[[email protected] nginx]# chown -R nginx:nginx /cache/nginx/
[[email protected] nginx]# 

2、调用缓存

[[email protected] nginx]# vim conf.d/default.conf

location /forum/ {
    proxy_cache mycache;    调用缓存区域
    proxy_cache_valid 200 1d;   200的缓存一天
    proxy_cache_valid 301 302 10m;  301缓存10分钟
    proxy_cache_valid any 1m;   其他缓存1分钟
    proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;    如果有这些情况,使用旧缓存
    proxy_pass http://10.201.106.22/;
    proxy_set_header HOST $host;
    proxy_set_header X-Real-IP $remote_addr;
}

3、重载服务

[[email protected] nginx]# service nginx configtest
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[[email protected] nginx]#
[[email protected] nginx]#
[[email protected] nginx]# service nginx reload
Reloading nginx:                                           [  OK  ]
[[email protected] nginx]# 

4、测试

访问网页后,相应缓存目录有产生文件
[[email protected] nginx]# cd /cache/nginx/
[[email protected] nginx]# ls
[[email protected] nginx]# ls
c
[[email protected] nginx]# ls
7  c
[[email protected] nginx]# ls -lht
total 8.0K
drwx------ 3 nginx nginx 4.0K Nov 24 11:21 7
drwx------ 3 nginx nginx 4.0K Nov 24 11:21 c
[[email protected] nginx]# cd 7
[[email protected] 7]# ls
c
[[email protected] 7]# cd c
[[email protected] c]# ls
99cd97b13b9069e769098b964e66bbc7
[[email protected] c]# ls -lht
total 12K
-rw------- 1 nginx nginx 8.4K Nov 24 11:21 99cd97b13b9069e769098b964e66bbc7
[[email protected] c]# 

缓存后,

Nginx负载均衡

1、关闭缓存

[[email protected]ode1 ~]# vim /etc/nginx/nginx.conf

#proxy_cache_path /cache/nginx/ levels=1:1 keys_zone=mycache:32m;

2、定义第三个节点的网页

[[email protected] ~]# vim /var/www/html/index.htm 

<h1>nginx on node3</h1>

3、编辑前端配置

全局配置
[[email protected] ~]# vim /etc/nginx/nginx.conf

    upstream upservers {
        server 10.201.106.22;
        server 10.201.106.130;

    }

web配置

[[email protected] ~]# vim /etc/nginx/conf.d/default.conf

location /forum/ {

proxy_pass http://upservers/;

}

4、 访问http://10.201.106.21/forum/已经可以在两个节点中切换

5、修改负载后端的某台主机权重

[[email protected] ~]# vim /etc/nginx/nginx.conf

    upstream upservers {
        server 10.201.106.22; weight=2;
        server 10.201.106.130;

    }

6、访问网页,22访问2次,130才访问一次

7、

[[email protected] ~]# vim /etc/nginx/nginx.conf

upstream upservers {
    ip_hash;
    server 10.201.106.22 weight=2;
    server 10.201.106.130;

}

8、

upstream upservers {
    server 10.201.106.22 max_fails=2 fail_timeout=1;
    server 10.201.106.130 max_fails=2 fail_timeout=1;

}

将其中一个节点关系服务
[[email protected] ~]# service httpd stop
Stopping httpd:                                            [  OK  ]

测试后:只会在好的节点访问了

重新打开服务后,又能在两个节点间切换了;

9、标记为备用节点

upstream upservers {
    server 10.201.106.22 max_fails=2 fail_timeout=1;
    server 10.201.106.130 max_fails=2 fail_timeout=1 backup;

}

10、

原文地址:http://blog.51cto.com/zhongle21/2087740

时间: 2024-10-08 11:01:33

Nginx反向代理和缓存杂记的相关文章

Nginx反向代理、缓存、负载均衡服务器构建

代理服务可简单的分为正向代理和反向代理: 正向代理: 用于代理内部网络对Internet的连接请求(如VPN/NAT),客户端指定代理服务器,并将本来要直接发送给目标Web服务器的HTTP请求先发送到代理服务器上,然后由代理服务器去访问Web服务器, 并将Web服务器的Response回传给客户端: 反向代理: 与正向代理相反,如果局域网向Internet提供资源,并让Internet上的其他用户可以访问局域网内资源, 也可以设置一个代理服务器, 它提供的服务就是反向代理. 反向代理服务器接受来

Nginx 反向代理并缓存及缓存清除

Nginx 反向代理并缓存及缓存清除 原文地址:http://www.cnblogs.com/caoguo/p/5012447.html 一. Nginx 配置 #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_conne

nginx 反向代理、缓存

lvs+keepalive+nginx(realserver)两台+tomcat(后端服务器),nginx的配置文件nginx.conf如下 user  nobody nobody; worker_processes 12; error_log /var/log/nginx/error.log crit;(取消记录错误日志) #error_log  /var/log/nginx/debug.log  debug_http; #error_log  logs/error.log; #error_l

nginx反向代理及缓存清理

#下载安装包wget http://nginx.org/download/nginx-1.13.7.tar.gzwget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gzwget https://nchc.dl.sourceforge.net/project/pcre/pcre/8.40/pcre-8.41.tar.bz2 #--------------- 在未安装nginx的情况下安装ngx_cache_purge -------

Nginx反向代理、缓存、 负载均衡、upstream以及fastcgi模块应用

Nginx反向代理,缓存, 负载均衡, upstream及fastcgi模块应用 Nginx版本为nginx-1.6.2-1.el6.ngx.x86_64.rpm可以去官网下载: http://nginx.org/packages/centos/6/x86_64/RPMS/ [[email protected] ~]# rpm -ivhnginx-1.6.2-1.el6.ngx.x86_64.rpm [[email protected] ~]# vim/var/www/html/index.ht

Nginx反向代理、缓存、负载均衡

环境介绍 主机名 系统环境 Ip地址 Nginx nginx Centos 6.6 64位 172.16.4.100 Web-01 Web-01 Centos 6.6 64位 172.16.4.101 Web-02 Web-02 Centos 6.6 64位 172.16.4.102 安装nginx 使用官网制作好的rpm包 [[email protected] ~]# rpm -ivh nginx-1.6.2-1.el6.ngx.x86_64.rpm [[email protected] ~]

初识nginx反向代理和缓存机制(简单实现)

实现的需求图: 环境: nginx缓存和反向代理服务器:192.168.0.224 实际存储数据机器:192.168.0.37 一.实现反向代理 1.安装nginx,两台服务器都需要安装 1)安装依赖包 yum -y install gcc make cmake ncurses-devel libxml2-devel libtool-ltdl-devel gcc-c++ autoconf automake bison zlib-devel 2)下载nginx wget http://nginx.

初识nginx反向代理和缓存机制

实现的需求图: 环境: nginx缓存和反向代理服务器:192.168.0.224 实际存储数据机器:192.168.0.37 一.实现反向代理 1.安装nginx,两台服务器都需要安装 1)安装依赖包 yum -y install gcc make cmake ncurses-devel libxml2-devel libtool-ltdl-devel gcc-c++ autoconf automake bison zlib-devel pcre-devel openssl openssl-d

Nginx反向代理缓存服务器搭建

Nginx反向代理 代理服务可简单的分为正向代理和反向代理: 正向代理: 用于代理内部网络对Internet的连接请求(如VPN/NAT),客户端指定代理服务器,并将本来要直接发送给目标Web服务器的HTTP请求先发送到代理服务器上, 然后由代理服务器去访问Web服务器,并将Web服务器的Response回传给客户端: 反向代理: 与正向代理相反,如果局域网向Internet提供资源,并让Internet上的其他用户可以访问局域网内资源, 也可以设置一个代理服务器, 它提供的服务就是反向代理.