centos6.5搭建nginx反向代理Apache服务并实现动静分离
2015-08-31 10:37
1041 查看
Nginx反向代理配置步骤:
一、规划网络拓扑
二、配置Apache服务器
三、配置nginx服务器
四、进行测试
一、规划网络拓扑
二、配置Apache服务器
三、配置nginx服务器
四、测试动静分离
当我们输入http://192.168.2.80/index.html的时候给我们展现的是nginx服务器处理的静态页面。
当我们输入http://192.168.2.80/index.php的时候给我们返回的就是192.168.2.93上的Apache处理的动态页面。
一、规划网络拓扑
二、配置Apache服务器
三、配置nginx服务器
四、进行测试
一、规划网络拓扑
二、配置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)
三、配置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://192.168.2.80/index.html的时候给我们展现的是nginx服务器处理的静态页面。
当我们输入http://192.168.2.80/index.php的时候给我们返回的就是192.168.2.93上的Apache处理的动态页面。
相关文章推荐
- nginx代理指定目录
- 访问Nginx发生SSL connection error的一种情况
- Nginx+Naxsi部署专业级Web应用防火墙
- CentOS 6.2实战部署Nginx+MySQL+PHP
- nginx中http核心模块的配置指令2
- nginx中http核心模块的配置指令3
- 网站502与504错误分析
- 艰难完成 nginx + puma 部署 rails 4的详细记录
- 把Lua编译进nginx步骤方法
- web 应用中常用的各种 cache详解
- Linux系统上配置Nginx+Ruby on Rails+MySQL超攻略
- window+nginx+php环境配置 附配置搭配说明
- 解析CI即CodeIgniter框架在Nginx下的重写规则
- 将PHP从5.3.28升级到5.3.29时Nginx出现502错误
- 基于Nginx0.8.54+PHP5.3.4+MySQL5.5.8的全新LNMP稳定版架构搭建的VPS
- Nginx(PHP/fastcgi)的PATH_INFO问题
- 如何让Nginx支持中文文件名具体设置步骤
- IIS中使用的ISAPI_Rewrite Full版本做反向代理详解
- Nginx upstream的5种权重分配方式分享