centos6.5搭建nginx反向代理Apache服务并实现动静分离
2015-09-09 11:37
1166 查看
Nginx反向代理配置步骤:
一、规划网络拓扑
二、配置Apache服务器
三、配置nginx服务器
四、进行测试
一、规划网络拓扑
![](http://s3.51cto.com/wyfs02/M02/72/6E/wKiom1XjvDCyvcFtAAGuCqRUXp0627.jpg)
二、配置Apache服务器
![](http://s3.51cto.com/wyfs02/M00/72/6E/wKiom1XjvNqiW3cCAADtgjt-idA719.jpg)
三、配置nginx服务器
![](http://s3.51cto.com/wyfs02/M02/72/6A/wKioL1Xjv0KgJFXiAAEr2YKNNPc784.jpg)
四、测试动静分离
当我们输入http://192.168.2.80/index.html的时候给我们展现的是nginx服务器处理的静态页面。
![](http://s3.51cto.com/wyfs02/M00/72/6A/wKioL1Xjv4LhoAKIAAE8ok4GlXI215.jpg)
当我们输入http://192.168.2.80/index.php的时候给我们返回的就是192.168.2.93上的Apache处理的动态页面。
![](http://s3.51cto.com/wyfs02/M01/72/6A/wKioL1Xjv6WRCKOgAADxhS7AfdE049.jpg)
一、规划网络拓扑
二、配置Apache服务器
三、配置nginx服务器
四、进行测试
一、规划网络拓扑
![](http://s3.51cto.com/wyfs02/M02/72/6E/wKiom1XjvDCyvcFtAAGuCqRUXp0627.jpg)
二、配置Apache服务器
安装Apache服务 [root@localhost ~]# yum -y install httpd php 注:由于我们的Apache服务器要负责动态页面的处理,所以要安装PHP。 编辑Apache配置文件 [root@localhost ~]# vim /etc/httpd/conf/httpd.conf ServerName 192.168.2.93:80 //修改sername为192.168.2.93:80 Listen 80 //修改监听端口为80端口 DirectoryIndex index.html index.html.var index.php //在后边添加index.php使Apache支持PHP 然后保存并退出。 然后编辑Apache跟目录文件 [root@localhost ~]# vim /var/www/html/index.php <?php phpinfo(); ?> 保存并推出。 然后启用Apache服务; [root@localhost ~]# service httpd start接下来就能用我们的PC机进行测试了。(测试前请关闭防火墙,service iptables stop)
![](http://s3.51cto.com/wyfs02/M00/72/6E/wKiom1XjvNqiW3cCAADtgjt-idA719.jpg)
三、配置nginx服务器
Nginx服务器是192.168.2.80 安装nginx服务 [root@localhost ~]# yum -y install nginx 编辑nginx主配置文件 [root@localhost ~]# vim /etc/nginx/nginx.conf 1 # For more information on configuration, see: 2 # * Official English Documentation: http://nginx.org/en/docs/ 3 # * Official Russian Documentation: http://nginx.org/ru/docs/ 4 5 user nginx; 6 worker_processes 1; 7 8 error_log /var/log/nginx/error.log; 9 #error_log /var/log/nginx/error.log notice; 10 #error_log /var/log/nginx/error.log info; 11 12 pid /var/run/nginx.pid; 13 14 15 events { 16 worker_connections 1024; 17 } 18 19 20 http { 21 include /etc/nginx/mime.types; 22 default_type application/octet-stream; 23 24 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 25 '$status $body_bytes_sent "$http_referer" ' 26 '"$http_user_agent" "$http_x_forwarded_for"'; 27 28 access_log /var/log/nginx/access.log main; 29 30 sendfile on; 31 #tcp_nopush on; 32 33 #keepalive_timeout 0; 34 keepalive_timeout 65; 35 36 #gzip on; 37 38 # Load config files from the /etc/nginx/conf.d directory 39 # The default server is in conf.d/default.conf 40 include /etc/nginx/conf.d/*.conf; 41 42 } 在此配置文件的40行出有include,后边跟的文件就是主配置文件的关联文件。因此我们编辑关联文件配置虚拟主机。 [root@localhost ~]# vim /etc/nginx/conf.d/default.conf 4 server { 5 listen 80 default_server; 6 server_name _; 7 8 #charset koi8-r; 9 10 #access_log logs/host.access.log main; 11 12 # Load configuration files for the default server block. 13 include /etc/nginx/default.d/*.conf; 14 15 location / { 16 root /usr/share/nginx/html; 17 index index.html index.htm; 18 if ( $request_uri ~* \.html$ ){ 19 proxy_pass http://192.168.2.80; 20 } 21 if ( $request_uri ~* \.php$ ){ 22 proxy_pass http://192.168.2.93; 23 } 24 } 25 26 error_page 404 /404.html; 27 location = /404.html { 28 root /usr/share/nginx/html; 29 } 30 31 # redirect server error pages to the static page /50x.html 32 # 33 error_page 500 502 503 504 /50x.html; 34 location = /50x.html { 35 root /usr/share/nginx/html; 36 } 60 } 在这个配置文件中我们主要修改的是监听的端口,还有location中的跳转服务地址(分辨请求的是动态页面还是静态页面,实现动静分离)。 配置完成后我们启动nginx服务 [root@localhost ~]# service nginx start打开主机浏览器进行测试。
![](http://s3.51cto.com/wyfs02/M02/72/6A/wKioL1Xjv0KgJFXiAAEr2YKNNPc784.jpg)
四、测试动静分离
当我们输入http://192.168.2.80/index.html的时候给我们展现的是nginx服务器处理的静态页面。
![](http://s3.51cto.com/wyfs02/M00/72/6A/wKioL1Xjv4LhoAKIAAE8ok4GlXI215.jpg)
当我们输入http://192.168.2.80/index.php的时候给我们返回的就是192.168.2.93上的Apache处理的动态页面。
![](http://s3.51cto.com/wyfs02/M01/72/6A/wKioL1Xjv6WRCKOgAADxhS7AfdE049.jpg)
相关文章推荐
- 基于centos 6.5使用varnish实现网站动静分离
- haproxy详细介绍
- nginx反向代理,动静态请求分离,以及nginx缓存应用,以及使用ngx_cache_purge清除指定URL
- nginx动静分离初尝试
- Nginx动静分离经典案例配置
- Nginx+tomcat7的负载均衡和动静分离配置
- 视频网站架构设计
- Nginx+tomcat实现动静分离
- Varnish的负载均衡、动静分离 推荐
- Haproxy的负载均衡、动静分离、状态监控、近期网络架构 推荐
- Tomcat与Apache整合(mod_jk)
- nginx+tomcat动静态资源分离
- nginx均衡后端两台lamp,并做动静分离
- haproxy使用详解
- 深入理解nginx反向代理
- HAProxy+KeepAlived实现web服务高可用、动静分离等 推荐
- LNMMP架构实现Web动静分离 推荐
- HAProxy基于KeepAlived实现Web高可用及动静分离 推荐
- nginx反向代理tomcat集群实现动静分离
- 高并发压力下nginx动静分离、nginx代理缓存两种模式的性能比较