您的位置:首页 > 运维架构 > 反向代理

centos6.5搭建nginx反向代理Apache服务并实现动静分离

2015-08-31 10:37 1041 查看
Nginx反向代理配置步骤:
一、规划网络拓扑
二、配置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处理的动态页面。


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息