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

Nginx反向代理配置步骤:

一、规划网络拓扑

二、配置Apache服务器

三、配置nginx服务器

四、进行测试

 

一、规划网络拓扑

二、配置Apache服务器


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25


安装Apache服务

[[email protected] ~]# yum -y install httpd php

注:由于我们的Apache服务器要负责动态页面的处理,所以要安装PHP。

 

编辑Apache配置文件

[[email protected] ~]# 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跟目录文件

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

<?php

 phpinfo();

?>

保存并推出。

 

然后启用Apache服务;

[[email protected] ~]# service httpd start

接下来就能用我们的PC机进行测试了。(测试前请关闭防火墙,service iptables stop)

三、配置nginx服务器


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90


Nginx服务器是192.168.2.80

 

安装nginx服务

[[email protected] ~]# yum -y install nginx

编辑nginx主配置文件

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

# For more information on configuration, see:

  #   * Official English Documentation: http://nginx.org/en/docs/

  #   * Official Russian Documentation: http://nginx.org/ru/docs/

  

  5 user              nginx;

  6 worker_processes  1;

  

  8 error_log  /var/log/nginx/error.log;

  #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,后边跟的文件就是主配置文件的关联文件。因此我们编辑关联文件配置虚拟主机。

 

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

  4 server {

  5     listen       80 default_server;

  6     server_name  _;

  

  8     #charset koi8-r;

  

 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服务

[[email protected] ~]# service nginx start

打开主机浏览器进行测试。

四、测试动静分离

当我们输入http://192.168.2.80/index.html的时候给我们展现的是nginx服务器处理的静态页面。

当我们输入http://192.168.2.80/index.php的时候给我们返回的就是192.168.2.93上的Apache处理的动态页面。

时间: 2024-08-08 09:37:09

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

nginx反向代理tomcat集群实现动静分离

我们都知道,nginx作为一个轻量级的web服务器,其在高并发下处理静态页面的优越性能是tomcat这样的web容器所无法媲美的,tomcat更倾向于处理动态文件,所以一个web应用可以通过nginx反向代理来实现动静分离,静态文件由nginx处理,动态文件由tomcat处理. 环境: hadoop0.updb.com    192.168.0.100    nginx server hadoop2.updb.com    192.168.0.102    tomcat server hadoo

Nginx反向代理、负载均衡、动静分离、缓存、压缩、防盗链、跨域访问

一.反向代理 1.在192.168.189.130机器启动tomcat服务,http://192.168.189.130:8080/ 访问服务正常 2.在192.168.189.131机器配置nginx server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://192.168.189.130:80

Nginx 反向代理、负载均衡与动静分离

1.环境: 前端Nginx服务器:主机名:node5.a.com IP:192.168.10.205  编译安装nginx 1.6.3 后端tomcat: Server1--ip:192.168.10.209  主机名:node9.a.com Server2--ip:192.168.10.210  主机名: node10.a.com 2.Ngginx配置: user  nginx; worker_processes  1; events { use epoll; worker_connectio

搭建nginx反向代理用做内网域名转发

基于域名的7层转发的实现(NAT+反向代理) 在实际办公网中,因为出口IP只有一个,要实现对外提供服务的话就必须得做端口映射,如果有多个服务要对外开放的话,这只能通过映射不同端口来区分,这在实际使用过程中非常的痛苦(记忆困难.一一对应关系也没有规律.访问的时候还得加端口),这个痛苦的问题用表格的形式来形象的描述如下: Public IP Public Port Number Internal IP Internal Port Number Note 1.1.1.1 80 192.168.1.10

相同Ip 不同端口配置Nginx反向代理Apache

相同Ip  不同端口 配置Nginx反向代理Apache(就是Nginx跳转到Apache) 在linux 一经搭建好环境  先后安装了Nginx  和Apache 由于 默认端口都是:80 一般客户请求的服务器端口默认为80  所以Nginx作为静态页端口设置:80 Apache设置端口为:8080(在httpd.conf  文件中修改Listen:8080) 如何跳转: 在nginx.conf中 添加 location / { proxy_pass http://202.85.224.166

Nginx 反向代理Apache要保证Apache虚拟机和Nginx虚拟机的一致性

1.nginx相对于apache的优点:  轻量级,同样起web 服务,比apache 占用更少的内存及资源  抗并发,nginx 处理请求是异步非阻塞的,而apache 则是阻塞型的,在高并发下nginx 能保持低资源低消耗高性能 apache 相对于nginx 的优点: rewrite , 比nginx 的rewrite 强大  动态页面 模块超多,基本想到的都可以找到 少bug ,nginx 的bug 相对较多 超稳定 nginx处理静态文件好,耗费内存少 . nginx处理动态请求是鸡肋

nginx 反向代理apache服务器 配置java与PHP共存环境

listen 80; listen 443; ssl on; ssl_certificate /passport.crt; ssl_certificate_key /passport.key; ssl_session_timeout 5m; server_name localhost; index index.html index.htm index.php; root /www/; location ~ .*\.(php|php5)?$ { #fastcgi_pass unix:/tmp/ph

搭建Nginx反向代理做内网域名转发

由于公司内网有多台服务器的 http 服务要映射到公司外网静态 IP,如果用路由的端口映射来做,就只能一台内网服务器的 80 端口映射到外网 80 端口,其他服务器的 80 端口只能映射到外网的非 80 端口.非 80 端口的映射在访问的时候要域名加上端口,比较麻烦. 我们可以在内网搭建一个Nginx反向代理服务器,将Nginx反向代理服务器的80映射到外网IP的80,这样指向到公司外网IP的域名的HTTP请求就会发送到Nginx反向代理服务器,利用Nginx反向代理将不同域名的请求转发到内网不

Kubernetes用nginx反向代理另外服务

公司利用K8S搭建测试环境,以及存在多套测试换,目前的想法是每一套测试环境使用一个出口IP.方案:1.搭建ingress2.通过搭建一个反向代理 结合实际情况,我们使用搭建一个反向代理解决此问题. 我们环境中存在以下K8S服务vipapi-mall-com #基于.netcorebillapi-mall-com #基于PHPwww-mall-com #基于PHPmobile-mall-com #基于VUE vipapi-mall-com对外的域名就是vipapi.mall.combillapi-