Nginx-基础篇(反向代理/负载均衡/页面缓存/URL重写/读写分离)
问题记载:
1.在配置Tomcat集群失效时,配置Nginx跳转有问题
参考文件:
Nginx原理理论部分详解 : http://blog.csdn.net/wave_1102/article/details/44479321
Nginx 反向代理、负载均衡、页面缓存、URL重写及读写分离详解:http://blog.csdn.net/wave_1102/article/details/44479175
大纲
一、前言
二、环境准备
三、安装与配置Nginx
四、Nginx之反向代理
五、Nginx之负载均衡
六、Nginx之页面缓存
七、Nginx之URL重写
八、Nginx之读写分离
注,操作系统为 CentOS 6.4 x86_64 , Nginx 是版本是最新版的1.4.2,所以实验用到的软件请点击这里下载:http://yunpan.cn/QXIgqMmVmuZrm
一.前言
在前面的几篇博文中,我们主要讲解了Nginx作为Web服务器知识点,主要的知识点有
(1) nginx的理论详解、(2) nginx作为web服务器的操作讲解、(3) nginx作为LNMP架构的讲解,不清楚的博友可以回头看看,
在这一篇博客中我们主要讲解:
(1) nginx的(1)反向代理、(2) 负载均衡、(3) 缓存、(4) URL重写以及(5) 读写分离详解
二.环境准备
说明:这里为测试功能需要同时安装了2台,且后面需要使用nginx作为反向代理服务器和负载均衡器,所有名称用 edu-proxy-xx
1.操作系统
CentOS 6.4 x86_64(CentOS 6.6 x86_64)
2.主机名配置
hostname "edu-proxy-01"
vi /etc/sysconfig/network
HOSTNAME=edu-proxy-01
vi /etc/hosts
127.0.0.1 edu-proxy-01 localhost
hostname "edu-proxy-02"
vi /etc/sysconfig/network
HOSTNAME=edu-proxy-02
vi /etc/hosts
127.0.0.1 edu-proxy-02 localhost
3. 软件版本 Nginx 1.4.2 (Nginx 1.10.0)
我的规划:edu-proxy-01 edu-proxy-02
4. 实验拓扑
注,实验拓扑见下文。
三.安装部分
说明:(没有参考原文,参考的是"Nginx + keepalived 实现web的高可用负载均衡"中Nginx的安装部分,如下)
安装的路径为:/usr/local/nginx
1.安装编译 Nginx 所需的依赖包
# yum install gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel -y
2.上传 Nginx(nginx-1.6.2.tar.gz)到 /usr/local/src 目录
3、编译安装 Nginx
# cd /usr/local/src/ # tar -zxvf nginx-1.6.2.tar.gz # cd nginx-1.6.2 # ./configure --prefix=/usr/local/nginx # make && make install
4、配置 Nginx
# vi /usr/local/nginx/conf/nginx.conf
user root; 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_connections 1024; } http { include 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 logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 88; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
修改 Nginx 欢迎首页内容:
注:用于后面测试,用于区分两个节点的 Nginx
# vi /usr/local/nginx/html/index.html
172.21.10.218 中的标题加 1 <h1>Welcome to nginx! 1</h1> 172.21.10.220 中的标题加 2 <h1>Welcome to nginx! 2</h1>
5、系统防火墙打开对应的端口 88
# chkconfig iptables on # service iptables start # vi /etc/sysconfig/iptables # Firewall configuration written by system-config-securitylevel # Manual customization of this file is not recommended. *filter ## Nginx -A INPUT -m state --state NEW -m tcp -p tcp --dport 88 -j ACCEPT COMMIT 重启防火墙: # service iptables restart 查看防火墙端口状态: # service iptables status
6、测试 Nginx 是否安装成功
# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
7、启动 /重启 Nginx
启动 # /usr/local/nginx/sbin/nginx & tail -f /usr/local/nginx/logs/error.log 重启 Nginx # /usr/local/nginx/sbin/nginx -s reload & tail -f /usr/local/nginx/logs/error.log
8、设置 Nginx 开机启动