Centos 7.6配置nginx反向代理,直接yum安装
一,实验介绍
利用三台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=/tcp --permanent (--permanent永久生效,没有此参数重启后失效)
重新载入
firewall-cmd --reload
查看
firewall-cmd --zone= public --query-port=/tcp
删除
firewall-cmd --zone= public --remove-port=/tcp --permanent
Centos 7.6配置nginx反向代理,直接yum安装的更多相关文章
- Centos 7.6配置nginx反向代理负载均衡集群
一,实验介绍 利用三台centos7虚拟机搭建简单的nginx反向代理负载集群, 三台虚拟机地址及功能介绍 192.168.2.76 nginx负载均衡器 192.168.2.82 web ...
- CentOS 7 学习(二) 配置Nginx反向代理
CentOS 7 学习(二) 配置Nginx反向代理 Nginx可以通过php-fpm来运行PHP程序,也可以转向apache,让apache调用php程序来运行. 不过对于Nginx来说,其反向代理 ...
- 为docker私有registry配置nginx反向代理
公司的Docker私有registry已经搭建好了,用官方的registry image很容易就搭建好了.现在就是要用nginx的反向代理把它放出来,以便在外网可以访问. 我的上一篇blog 讲了如何 ...
- 使用SSL配置Nginx反向代理的简单指南
反向代理是一个服务器,它接收通过Web发出的请求,即http和https,然后将它们发送到后端服务器(或服务器).后端服务器可以是单个或一组应用服务器,如Tomcat,wildfly或Jenkins等 ...
- 配置LANMP环境(7)-- 配置nginx反向代理,与配置apache虚拟主机
一.配置nginx反向代理 1.修改配置文件 vim /etc/nginx/nginx.conf 在35行http下添加一下内容: include /data/nginx/vhosts/*.conf; ...
- Centos 7配置nginx反向代理负载均衡集群
一,实验介绍 利用三台centos7虚拟机搭建简单的nginx反向代理负载集群, 三台虚拟机地址及功能介绍 192.168.2.76 nginx负载均衡器 192.168.2.82 web ...
- node项目发布+域名及其二级域名配置+nginx反向代理+pm2
学习node的时候也写了一些demo.但是只是限于本地测试,从来没有发布.今天尝试发布项目. 需要准备的东西 node 项目:为了突出重点,说明主要问题.我只是拿express 写了很简单的demo. ...
- [亲测]ASP.NET Core 2.0怎么发布/部署到Ubuntu Linux服务器并配置Nginx反向代理实现域名访问
前言 ASP.NET Core 2.0 怎么发布到Ubuntu服务器?又如何在服务器上配置使用ASP.NET Core网站绑定到指定的域名,让外网用户可以访问呢? 步骤 第1步:准备工作 一台Liun ...
- [亲测]七步学会ASP.NET Core 2.0怎么发布/部署到Ubuntu Linux服务器并配置Nginx反向代理实现域名访问
前言 ASP.NET Core 2.0 怎么发布到Ubuntu服务器?又如何在服务器上配置使用ASP.NET Core网站绑定到指定的域名,让外网用户可以访问呢? 步骤 第1步:准备工作 一台Liun ...
随机推荐
- Linux下Memcache服务器端的安装
最近在研究怎么让Discuz!去应用Memcache去做一些事情,记录下Memcache安装的过程. Linux下Memcache服务器端的安装服务器端主要是安装memcache服务器端,目前的最新版 ...
- git无法同步
出现问题: fatal: destination path 'test' already exists and is not an empty directory. 解决方法如下: git init ...
- 从零起步做到Linux运维经理, 你必须管好的23个细节
“不想成为将军的士兵,不是好士兵”-拿破仑 如何成为运维经理? 一般来说,运维经理大概有两种出身:一种是从底层最基础的维护做起,通过出色的维护工作,让公司领导对这个人非常认可,同时对Linux运维工作 ...
- VSCode插件开发全攻略(二)HelloWord
更多文章请戳VSCode插件开发全攻略系列目录导航. 写着前面 学习一门新的语言或者生态首先肯定是从HelloWord开始. 您可以直接克隆我放在GitHub上vscode-plugin-demo 的 ...
- 【面试必备】常见Java面试题大综合
一.Java基础 1.Arrays.sort实现原理和Collections.sort实现原理答:Collections.sort方法底层会调用Arrays.sort方法,底层实现都是TimeSort ...
- Ubuntu16.04设置静态ip
给Ubuntu系统配置一个静态IP ,方法如下 : 1. sudo vi /etc/network/interfaces (本人更推荐使用 sudo gedit /etc/network/inter ...
- Aseprite入门教程
因为最近在学cocos2d-x和vs搭配做手机游戏开发,想自己做一些素材,所以找到了这款软件,Aseprite v1.1.12.刚安装上时也是不懂该怎么操作,随着逐渐地摸索,对初始的使用有了一些了解. ...
- SpringDataJPA与Mybatis的优异性
首先表达个人观点,JPA必然是首选的. 个人认为仅仅讨论两者使用起来有何区别,何者更加方便,不足以真正的比较这两个框架.要评判出更加优秀的方案,我觉得可以从软件设计的角度来评判.个人对 mybatis ...
- Django--缓存设置
Django缓存机制 一. 缓存介绍 缓存是将一些常用的数据保存内存或者memcache中,在一定的时间内有人来访问这些数据时,则不再去执行数据库及渲染等操作,而是直接从内存或memcache的缓存中 ...
- IDEA中MAVEN项目有多个子目录,如何加载构建
ddts这个项目有三个子目录,每个子目录下面也都有一个 pom.xml 此时需要 右键子目录的 pom.xml,选择Add as Maven Project,在上图中cli.core两个目 ...