当前位置:首页 > 后端开发 > Centos 7.6配置nginx反向代理,直接yum安装

Centos 7.6配置nginx反向代理,直接yum安装

6个月前 (05-24)42

一,实验介绍

利用三台centos7虚拟机搭建简单的nginx反向代理负载集群,

三台虚拟机地址及功能介绍

192.168.2.76    nginx负载均衡器

192.168.2.82    web01服务器

192.168.2.78    web02服务器

二,安装nginx软件(以下操作三台虚拟机都要进行)

有些Centos 7.6里面没有安装wget命令,所以要自己安装:

yum -y install wget

安装nginx软件:(三个服务器都要安装)

$ wget http://dl.Fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm 

$ rpm -ivh epel-release-latest-7.noarch.rpm

$ yum install nginx (直接yum安装)

安装就这么简单方便,安装完成后,就可以使用systemctl来控制nginx的启动了

$ systemctl enable nginx (加入开机启动)
$ systemctl start nginx (开启nginx)
$ systemctl status nginx (查看状态)

三台服务器分别安装好nginx后测试能否正常运行,提供web服务。如果报错可能是防火墙的原因,请看最后几步关于防火墙的。

修改代理服务器的nginx的配置文件,实现负载均衡。顾名思义就是将多个请求分发到不同的服务上,实现均衡的负载,减小单个服务的压力。

$ vi /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/


user nginx;
worker_processes auto; (默认为自动,可以自己设置,一般不大于cpu核数)
error_log /var/log/nginx/error.log; (错误日志路径)
pid /run/nginx.pid; (pid文件路径)


# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;


events {
    accept_mutex on; (设置网路连接序列化,防止惊群现象发生,默认为on)
    multi_accept on; (设置一个进程是否同时接受多个网络连接,默认为off)
    worker_connections 1024; (一个进程的最大连接数)
}


http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';


    access_log  /var/log/nginx/access.log  main;


    sendfile            on;
    # tcp_nopush          on; (这里注释掉)
    tcp_nodelay         on;
    keepalive_timeout   65; (连接超时时间)
    types_hash_max_size 2048;
    gzip on; (开启压缩)
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;


    # 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;


# 这里设置负载均衡,负载均衡有多中策略,nginx自带的有轮询,权重,ip-hash,响应时间等粗略。
# 默认为平分http负载,为轮询的方式。
# 权重则是按照权重来分发请求,权重高的负载大
# ip-hash,根据ip来分配,保持同一个ip分在同一台服务器上。
# 响应时间,根据服务器都nginx 的响应时间,优先分发给响应速度快的服务器。
集中策略可以适当组合
    upstream tomcat { (tomcat为自定义的负载均衡规则名)
        ip_hash; (ip_hash则为ip-hash方法)

      server 192.168.2.78:80 weight=3 fail_timeout=20s;
      server 192.168.2.82:80 weight=4 fail_timeout=20s;




## 可以定义多组规则
}




    server {
        listen       80 default_server; (默认监听80端口)
        listen       localhost; (监听的服务器)
        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://tomcat; (反向代理,填上你自己的负载均衡规则名)
  proxy_redirect off; (下面一些设置可以直接复制过去,不要的话,有可能导致一些 没法认证等的问题)
  proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_connect_timeout 90; (下面这几个都只是一些超时设置,可不要)
          proxy_send_timeout 90;
          proxy_read_timeout 90;
        }
   # location ~\.(gif|jpg|png)$ { (比如,以正则表达式写)  
   #  root /home/root/images;
   #  }


        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 {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }
}

更新配置后,可以重载配置生效,不需要重启服务

nginx -s reload

如果不能访问,可能是由于防火墙打开了,端口没有开启:

启动: systemctl start firewalld
关闭: systemctl stop firewalld
查看状态: systemctl status firewalld 
开机禁用  : systemctl disable firewalld
开机启用  : systemctl enable firewalld
开启一个端口:
添加
firewall-cmd --zone=public --add-port=80/tcp --permanent    (--permanent永久生效,没有此参数重启后失效)
重新载入
firewall-cmd --reload
查看
firewall-cmd --zone= public --query-port=80/tcp
删除
firewall-cmd --zone= public --remove-port=80/tcp --permanent

 



作者:人走茶良
来源链接:https://www.cnblogs.com/aqicheng/p/10243021.html

标签: Nginx

“Centos 7.6配置nginx反向代理,直接yum安装” 的相关文章

docker pull镜像报错:rror response from daemon: Get https://index.docker.io/v1/search?q=nginx&n=25: net/http: TLS handshake timeout

docker pull镜像报错:rror response from daemon: Get https://index.docker.io/v1/search?q=nginx&n=25: net/http: TLS handshake timeout

docker search镜像时发现报错:rror response from daemon: Get https://index.docker.io/v1/search?q=nginx...

Nginx.pid打开失败以及失效的解决方案

Nginx.pid打开失败以及失效的解决方案

    在启动nginx的时候报了如下的错误:      其意思是没有该文件或者是目录,通过查看之后发现确实没有该目录...

Nginx一键安装脚本

Nginx一键安装脚本

百度云盘分享 链接:https://pan.baidu.com/s/1w9etGSMRzwv9L4BXPpnm3A 提取码:pxdy...

Nginx做web服务器反向代理

Nginx做web服务器反向代理

实验目的 通过nginx实现反向代理的功能,类似apache反向代理和haproxy反向代理 工作中用nginx做反向代理和负载均衡的也越来越多了...

Nginx访问权限配置

最近建个人网站,在服务器上新建了一个用户zengfp,并且把网站的目录放到了/home/zengfp/www目录下,配置的nginx: server { li...

Nginx 安装&默认配置简介

Nginx 安装&默认配置简介

  一.  yum 安装 二. mac 安装 三. Linux编译安装 1.下载 2.安装先安装nginx依赖的包 g...

nginx configure配置

记录安装nginx配置初始化 ./configure --prefix=/data/services/nginx-1.10.2 --use...

nginx配置详解(转发)

nginx配置详解(转发)

序言 Nginx是lgor Sysoev为俄罗斯访问量第二的rambler.ru站点设计开发的。从2004年发布至今,凭借开源的力量,已经接近成熟与完善。 Ngi...

http-proxy-middleware和nginx设置代理

react开发 开发环境时在src底下建一个setupProxy.js文件,在里面使用http-proxy-middleware进行代理,这个文件只会在node环境底下有效果,打包...

Nginx+tomcat 负载均衡

Nginx+tomcat 负载均衡

  一、系统版本 Nginx使用版本、tomcat使用版本: Nginx:ngin...