nginx 反向代理

Nginx优点:

  1. 轻量级,采用C进行编写。同样的 web 服务,占用更少内存和资源
  2. 抗并发,nginx 以epoll and kqueue 作为开发模型,处理请求异步非阻塞的负载能力比 apache高很多,而apache则是阻塞 型的。在高并发下nginx能保持低资源、低消耗、高性能,而apache 在PHP 处理慢,或者前端压力很大的情况下,很容易出现进程飙升,从而拒绝服务的现象。
  3. nginx处理静态文件好,静态处理性能比apache高三倍以上。
  4. nginx 的设计高度模块化,编写模块相对简单。
  5. nginx 配置简洁,正则配置让很多事情变得简单,而且改完配置能使用 -t 测试配置有没有问题, apache 配置复杂,重启的时候发现配置出错,会很崩溃。
  6. nginx 作为负载均衡服务器,支持7层高负载均衡。
  7. nginx 本身就是反向代理服务器,而且可以作为非常优秀的优秀的邮件代理服务器。
  8. 启动特别容易,并且几乎可以做到7*24 不间断运行,即使运行数个月也不需要重启,还能够不间断服务的情况下进行软件版本升级。

 

apache优点:

  1. apache 的rewrite 比nginx 强大,在rewrite 频繁的情况下,用apache
  2. apache 发展到现在,模块多,基本想到的都可以找到。
  3. apache 更为成熟,bug少。
  4. apache 稳定
  5. apache 在处理动态请求有优势,nginx 在这方面是鸡肋,一般动态请求用apache 去做,nginx 适合静态和反向。

  apache目前依然是主流,拥有丰富的特性,成熟的技术和开发社区

nginx反向代理详解:

准备环境

反向代理服务器IP:192.168.5.45/24

web1服务器IP: 192.168.5.145/24

web2服务器IP:192.168.5.104/24

配置返现代理服务器端:

下载nginx 的网络源:

[epel]
name=epel
enabled=1
gpgcheck=0
baseurl=https://mirrors.aliyun.com/epel/7Server/x86_64/

 

配好yum源后需要重新挂载

[root@localhost ~]# mount /dev/cdrom /mnt
mount: /dev/sr0 写保护,将以只读方式挂载

之后就可以进行下载

[root@localhost ~]# yum install nginx -y

配置nginx 文件,要实现静态文件都被代理到192.168.5.145 , 动态文件都被代理到192.168.5.104,实现动静分离

[root@localhost ~]# vim /etc/nginx/nginx.conf
  # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        proxy_pass http://192.168.5.145; 
        }
        location ~ php$ {
        proxy_pass http://192.168.5.104;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {

进行语法检测

[root@localhost ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

检查没报错后重启服务

[root@localhost ~]# systemctl restart nginx

要修改一个配置文件,不修改可能最后配置完服务起不来

[root@localhost ~]# vim /etc/sysconfig/selinux



# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled                    #将enforcing改为disabled
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

配置web 服务器端

安装apache

[root@localhost ~]# yum install httpd -y

准备测试文件,192.168.5.145准备静态文件

[root@localhost ~]# echo "this is 192.168.5.145">/var/www/html/index.html

 

192.168.5.104需要下载php和httpd 以便支持动态文件

[root@localhost ~]# yum install php httpd -y

准备动态文件

[root@localhost ~]# vim /var/www/html/index.php
<?php
phpinfo();
?>

web 服务器重启

[root@localhost ~]# systemctl restart httpd

关闭安全服务

[root@localhost ~]# iptables -F

浏览器测试

静态请求已成功发送到192.168.5.145

请求动态文件测试

 

动态文件请求已发送到192.168.5.104

测试成功

posted @ 2019-04-20 19:03  怜寒  阅读(278)  评论(0编辑  收藏  举报