nginx的安装及代理和负载均衡设置

一、通过yum方式进行安装

官网参考地址:https://nginx.org/en/linux_packages.html#RHEL

1.1 安装好依赖

执行下面的命令安装

sudo yum install yum-utils

1.2、 先配置好yum源

新建文件/etc/yum.repos.d/nginx.repo,文件内容:

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

1.3、启动nginx的yum源,一般可以不操作

sudo yum-config-manager --enable nginx-mainline

1.4、执行安装操作

[root@min ~]# yum install -y nginx
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * extras: mirrors.cqu.edu.cn
...
Retrieving key from https://nginx.org/keys/nginx_signing.key
Importing GPG key 0x7BD9BF62:
 Userid     : "nginx signing key <signing-key@nginx.com>"
 Fingerprint: 573b fd6b 3d8f bc64 1079 a6ab abf5 bd82 7bd9 bf62
 From       : https://nginx.org/keys/nginx_signing.key

1.5、启动nginx

输入如下命令启动nginx

systemctl start nginx

1.6、设置开机自启动

通过如下指令来进行开机自启动

[root@min ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

1.7、测试是否部署成功

http://192.168.19.51/
在这里插入图片描述
看到上上面的这个界面,我们可以确定nginx安装成功了

二、nginx常用命令介绍

我们可以通过nginx -h命令来获取使用帮助

[root@min ~]# nginx -h
nginx version: nginx/1.25.1
Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
             [-e filename] [-c filename] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /etc/nginx/)
  -e filename   : set error log file (default: /var/log/nginx/error.log)
  -c filename   : set configuration file (default: /etc/nginx/nginx.conf)
  -g directives : set global directives out of configuration file

从上面的输出我们可以看出打那个前我们按照的nginx版本是1.25.1版本,然后我们可以使用nginx -V打印出版本信息及配置选项信息; nginx -s 可以分别对nginx进行关闭,重新加载配置文件

三、nginx程序的结构说明

使用rpm -ql nginx 来获取到nginx安装的相关文件

[root@min ~]# rpm -ql nginx
# 日志切割(默认以天为单位)
/etc/logrotate.d/nginx
# nginx主程序存放路径
/etc/nginx
# Nginx的自配置文件目录
/etc/nginx/conf.d
# Nginx默认配置文件
/etc/nginx/conf.d/default.conf
# Nginx与PHP交互的内置变量
/etc/nginx/fastcgi_params
# 存放响应报文中回传的文件类型
/etc/nginx/mime.types
# 存放Nginx程序模块路径
/etc/nginx/modules
# Nginx主配置文件
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
# 存放uwsgi交互的内置变量
/etc/nginx/uwsgi_params
/usr/lib/systemd/system/nginx-debug.service
# nginx 自启动服务文件
/usr/lib/systemd/system/nginx.service
/usr/lib64/nginx
/usr/lib64/nginx/modules
/usr/libexec/initscripts/legacy-actions/nginx
/usr/libexec/initscripts/legacy-actions/nginx/check-reload
/usr/libexec/initscripts/legacy-actions/nginx/upgrade
# nginx启动入口文件
/usr/sbin/nginx
/usr/sbin/nginx-debug
/usr/share/doc/nginx-1.25.1
/usr/share/doc/nginx-1.25.1/COPYRIGHT
/usr/share/man/man8/nginx.8.gz
/usr/share/nginx
# nginx默认存放网站源码的位置
/usr/share/nginx/html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
/var/cache/nginx
/var/log/nginx

四、nginx主配置文件

[root@min logrotate.d]# cat /etc/nginx/nginx.conf

# 指定我们Nginx服务的运行用户
user  nginx;
# 定义Nginx的worker进程数量的 根据服务器的内核来自动设定
worker_processes  auto;
# 指定Nginx错误日志
error_log  /var/log/nginx/error.log notice;
# 指定Nginx PID进程号文件
pid        /var/run/nginx.pid;


events {
# 指定Nginx当前一个worker进程同时可以处理的最大连接数量
    worker_connections  1024;
}


http {
    # 应用文件媒体类型,如text/html、application/json
    include       /etc/nginx/mime.types;
    # 当Nginx无法识别当前访问页面内容时,出发下载动作
    default_type  application/octet-stream;
   # 指定Nginx访问日志格式的
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
  # 定义Nginx访问日志的位置
    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;
   # 当Nginx建立TCP连接之后,多长时间没有动作,自动断开
    keepalive_timeout  65;

    #gzip  on;
    # 包含自配置文件路径下的所有以.conf结尾的文件
    include /etc/nginx/conf.d/*.conf;
}

五、使用子配置来配置nginx对应的web服务器

5.1 编写配置文件

切换目录到/etc/nginx/conf.d目录中,然后创建一个myweb.conf文件并使用nginx -t 对刚刚配置的文件进行测试

 
[root@min logrotate.d]# cd /etc/nginx/conf.d
[root@min conf.d]# vi myweb.conf
server { 
	listen 8080;
        server_name www.myweb.com;
        location / { 
           root /html/myweb;
           index index.html;
  } 
}
[root@min conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

5.2 创建myweb的主页文件

创建文件夹/html/myweb,并在其中创建文件index.html,文件内容如下:
Hello,this my web page!

[root@min conf.d]# mkdir -p /html/myweb
[root@min conf.d]# vi /html/myweb/index.html
Hello,this my web page!

5.3 重新加载配置文件设置host 文件

重新加载nginx的配置文件

[root@min conf.d]# nginx -s reload
[root@min conf.d]# 

修改hosts文件

[root@min conf.d]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.19.51 www.myweb.com

5.4 测试刚刚配置是否生效

使用curl访问http://www.myweb.com

[root@min conf.d]# curl http://www.myweb.com:8080
Hello,this is my web page!

可以看出刚刚配置的是有效的!

6、使用nginx进行多站点部署

6.1 部署myweb2页面

  • 编辑配置文件
[root@min conf.d]# cat /etc/nginx/conf.d/myweb2.conf 
server {
	   listen 80;
        server_name www.myweb2.com;
        location / {
           root /html/myweb2;
           index index.html;
      } 
}
[root@min conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@min conf.d]# nginx -s reload
  • 编写myweb2的主页面
[root@min conf.d]# vi /html/myweb2/index.html
[root@min conf.d]# cat /html/myweb2/index.html 
Hello,this is my web2 page!
  • 配置hosts文件
[root@min conf.d]# vi /etc/hosts
[root@min conf.d]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.19.51 www.myweb.com www.myweb2.com
[root@min conf.d]# systemctl restart network

6.2 修改myweb项目对应的配置项

[root@min conf.d]# vi myweb.conf 
[root@min conf.d]# cat /etc/nginx/conf.d/myweb.conf 
server { 
	listen 80;
        server_name www.myweb.com;
        location / { 
           root /html/myweb;
           index index.html;
  } 
}
[root@min conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@min conf.d]# nginx -s reload

6.3 测试多站点部署的效果

  • 使用curl分别访问:http://www.myweb.com、http://www.myweb2.com,预期访问http://www.myweb.com的是,将会响应Hello,this is my web page!。但是当访问http://www.myweb2.com时,返回Hello,this is my web2 page!
[root@min conf.d]# curl http://www.myweb.com
Hello,this is my web page!
[root@min conf.d]# curl http://www.myweb2.com
Hello,this is my web2 page!

从上面的结果我们可以发现我们实现了多站点部署

7、nginx安全访问控制

官方地址:http://nginx.org/en/docs/http/ngx_http_access
_module.html#allow

nginx的ngx_http_access_module 模块可以通过客户端的地址来进行访问控制,语法的格式如下:

 location / {
    deny  192.168.1.1;
    allow 192.168.1.0/24;
    allow 10.1.1.0/16;
    allow 2001:0db8::/32;
    deny  all;
}

allow代表的是允许通过,deny 拒绝。
这里我们将会以myweb项目进行演示,禁用192.168.19.50这个ip对www.myweb.com的访问

server { 
	listen 80;
        server_name www.myweb.com;
        location / { 
           root /html/myweb;
           index index.html;
           deny  192.168.19.50;
           allow 192.168.19.51;
  } 
}

测试配置文件正确并且重新加载配置文件

[root@min conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@min conf.d]# nginx -s reload
[root@min conf.d]# 

重新加载nginx的配置后,在192.168.19.50服务器上访问http://www.myweb.com

[root@min ~]# curl http://www.myweb.com
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.25.1</center>
</body>
</html>

在其他的服务器上可以正常访问

[root@k8s-master1 ~]# curl http://www.myweb.com
Hello,this is my web page!

8、nginx的反向代理设置

  • 场景一:本机代理到本机
    1、访问www.test.com 80端口 -->代理到后端1314端口的站点
    2、本机部署www.test.com 1314端口站点
    配置如下:
    配置监听1314端口
server {
      listen 1314;
      server_name www.test.com;
      location / {
     root    /html/test;
     index  index.html;
     }
}

配置反向代理

     server {
           listen 80;
           server_name www.test.com;
            location  / {
                 proxy_pass http://127.0.0.1:1314;
                 proxy_set_header HOST $host;
                 proxy_http_version 1.1;
        }
    }

重新加载配置项:

[root@min conf.d]# nginx -s reload
[root@min conf.d]# 

创建test.com的主页

[root@min conf.d]# mkdir -p /html/test
[root@min conf.d]# vi /html/test/index.html
[root@min conf.d]# cat /html/test/index.html 
Hello, this is test page!

测试代理的效果:

[root@min conf.d]# curl http://www.test.com:1314
Hello, this is test page!
[root@min conf.d]# cat /html/test/index.html 
Hello, this is test page!

通过上面的测试,我们发现可以直接通过访问http://www.test.com来达到访问http://www.test.com:1314一样的效果

  • 场景二:本机代理到其他机器
    前置条件说明:已经在192.168.19.50服务通过8080端口启动了一个web服务,开发的端口有/hello 和/hello/sub两个接口
    配置:
     server {
           listen 81;
           server_name www.test.com;
            location  / {
                 proxy_pass http://192.168.19.50:8080;
                 proxy_set_header HOST $host;
                 proxy_http_version 1.1;
        }
   }
[root@min conf.d]# nginx -t 
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@min conf.d]# nginx -s reload
[root@min conf.d]# 

 [root@min conf.d]# curl http://www.test.com:81/hello
    hello : 8080;range:CN
[root@min conf.d]# 

9、负载均衡配置

什么是负载均衡?
Load Balance(lb),指将工作任务进行分流,减轻单点压力,实
现工作任务均摊到过个节点的操作。(实现集群化)
负载均衡常见的实现方式:
硬件:F5
软件:
Nginx
LVS
HAproxy

这里我们将在192.168.19.50的8081,8082,8083三个端口上运行web服务,然后在192.168.19.51通过监听82端口对192.168.19.50三个web服务进行负载均衡,如下图所示
在这里插入图片描述

nginx提供负载均衡功能的模块ngx_stream_upstream_module,官网地址为:http://nginx.org/en/docs/http/ngx_http_upstream_module.html
配置的语法为:

upstream backend {
    server backend1.example.com       weight=5;
    server backend2.example.com:8080;
    server unix:/tmp/backend3;

    server backup1.example.com:8080   backup;
    server backup2.example.com:8080   backup;
}

server {
    location / {
        proxy_pass http://backend;
    }
}

配置文件:

upstream backend {
    server 192.168.19.50:8080;
    server 192.168.19.50:8081;
    server 192.168.19.50:8082;
}
server {
   listen 82;
   server_name www.upstream.com;
   location / {
      proxy_pass http://backend;
   } 
}

重新加载配置文件,然后进行测试:

[root@min conf.d]# curl www.upstream.com:82/hello
hello : 8082;range:CN
[root@min conf.d]# curl www.upstream.com:82/hello
hello : 8081;range:CN
[root@min conf.d]# curl www.upstream.com:82/hello
hello : 8081;range:CN
[root@min conf.d]# curl www.upstream.com:82/hello
hello : 8080;range:CN
[root@min conf.d]# curl www.upstream.com:82/hello
hello : 8082;range:CN
[root@min conf.d]# curl www.upstream.com:82/hello

从上面测试我们可以看出,我们达到负载均衡的效果

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/28663.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Spark SQL数据源:Hive表

文章目录 一、Spark SQL支持读写Hive二、Spark配置hive-site.xml三、准备工作&#xff08;一&#xff09;启动Hive的metastore&#xff08;二&#xff09;启动Spark Shell 四、Spark读写Hive数据&#xff08;一&#xff09;导入SparkSession&#xff08;二&#xff09;创建Spar…

内网安全:Cobalt Strike 与 MSF 联动( 会话 相互转移 )

内网安全&#xff1a;Cobalt Strike 与 MSF 联动&#xff08; 会话 相互转移 &#xff09; 在渗透中&#xff0c;有时候 Cobalt Strike 会话可能会受限制&#xff0c;所以我们需要把 Cobalt Strike 会话转移到 MSF 上进行后面的渗透。也有的时候会话在 MSF 上&#xff0c;但是…

MySQL数据库的认识及基础命令操作

目录 一、数据库的基本概念 1、数据库定义 &#xff08;1&#xff09; 数据 &#xff08;2&#xff09;表 &#xff08;3&#xff09; 数据库 2、 数据库管理系统&#xff08;DBMS&#xff09; 3、 数据库系统&#xff08;DBS&#xff09; 二、数据库系统发展史 1、 第一…

编程必备:JAVA多线程详解

目录 前言 1.入门多线程 1.1. 线程、进程、多线程、线程池 1.2.并发、串行、并行 1.3. 线程的实现方式 1.3.1. 继承 Thread 类 1.3.2. 实现 Runnable 接口 1.3.3. 使用 Callable 和 Future 1.3.4. 使用线程池 1.4.线程的状态 1.5. 线程常用方法 1.5.1 sleep() 1.4…

docker 网络理论知识点 - CNM 和命名空间

Network 目录 1 network namespace1.1 动手小实验 2 回到 docker2.1 driver and docker02.2 network2.3 网桥 docker0 3 总结 1 network namespace 1.1 动手小实验 网络命名空间。linux kernel 提供的网络虚拟化的功能。创建多个隔离的网络空间。每个空间内 firewall, ether …

【taro react】---- 解决H5接入uni-app版本的IM

1. 问题 由于项目开发比较紧张&#xff0c;腾讯 IM 的接入就使用了 TUIKit 含UI集成方案&#xff0c;遇到的问题&#xff0c;uni-app的UI本来就是一个单独的项目&#xff0c;需要集成到现有的 Taro React 中&#xff0c;就只能作为一个独立的项目&#xff0c;不跳转时不影响原有…

在十四届蓝桥杯开赛前一星期开始复习

文章目录 十三届蓝桥杯国赛原题1.20222.钟表3卡牌4最大数字4.5 Dijkstra算法5出差 十三届蓝桥杯国赛原题 1.2022 #include<iostream> using namespace std;long long int f[2023][11][2023]; //表示前2022个物品选择10个物品&#xff0c;体积总和为2022的方案个数 ,,数…

入门编程其实也简单

随着信息技术的快速发展&#xff0c;编程已经成为一个越来越重要的技能。那么&#xff0c;我们该如何入门编程呢&#xff1f; 编程是指使用计算机语言编写计算机程序的过程。计算机程序是一系列指令的集合&#xff0c;这些指令告诉计算机要执行的操作。编程的目的是创建计算机…

Unity编辑器扩展-第二集-按钮排序/分组/放入右键菜单

第一集链接&#xff1a;Unity编辑器扩展-第一集-在菜单栏加入自己的按钮_菌菌巧乐兹的博客-CSDN博客 一、本节目标效果展示 1.按钮排序 变成 2.按钮分组 仔细看&#xff0c;有个灰色的杠杠 3.放入右键菜单 4.皮一下 二、按钮排序具体流程 第一集讲&#xff0c;如果想放入…

阿里云PAIx达摩院GraphScope开源基于PyTorch的GPU加速分布式GNN框架

作者&#xff1a;艾宝乐 导读 近期阿里云机器学习平台 PAI 团队和达摩院 GraphScope 团队联合推出了面向 PyTorch 的 GPU 加速分布式 GNN 框架 GraphLearn-for-PyTorch(GLT) 。GLT 利用 GPU 的强大并行计算性能来加速图采样&#xff0c;并利用 UVA 来减少顶点和边特征的转换和…

4.4.2 译码器

1. 学习基础知识&#xff1a;首先&#xff0c;我会了解译码器的基本概念、原理和应用。通过阅读教科书、参考资料或在线资源&#xff0c;我会学习译码器的工作原理、不同类型的译码器以及它们在电子系统中的应用场景。 2. 研究示例和练习题&#xff1a;为了更好地理解译码器的…

【Spring】透过Spring源码查看Bean的命名转换规则

近期在写Spring项目的时候&#xff0c;需要通过注解的形式去替代之前直接将Bean存放在Spring容器这种方式&#xff0c;以此来简化对于Bean对象的操作&#xff0c;但是这样无法通过准确的Id去获取到相应的Bean对象了 测试观察 首先&#xff0c;如果要将指定的对象存放到Spring中…

Unity入门6——光源组件

一、参数面板 二、参数介绍 Type&#xff1a;光源类型 Spot&#xff1a;聚光灯 Range&#xff1a;发光距离Spot Angle&#xff1a;光锥角度Directional&#xff1a;方向光Point&#xff1a;点光源Area&#xff08;Baked Only&#xff09;&#xff1a;面光源 仅烘焙。预先算好&…

操作教程:如何正确配置让EasyNVR级联至EasyNVS平台?

EasyNVS是EasyNVR的云管理平台&#xff0c;可实现内网监控上云&#xff0c;视频汇聚等功能。近期经常有用户咨询EasyNVR如何级联至EasyNVS平台进行云端统计和管理&#xff0c;在今天的文章中&#xff0c;我们来详细介绍一下。 1、配置EasyNVS 1&#xff09;运行EasyNVS之前&a…

回归预测 | MATLAB实现基于GRU-AdaBoost门控循环单元结合AdaBoost多输入单输出回归预测

回归预测 | MATLAB实现基于GRU-AdaBoost门控循环单元结合AdaBoost多输入单输出回归预测 目录 回归预测 | MATLAB实现基于GRU-AdaBoost门控循环单元结合AdaBoost多输入单输出回归预测预测效果基本介绍模型描述程序设计参考资料 预测效果 基本介绍 1.MATLAB实现基于GRU-AdaBoost门…

C++【STL】之vector的使用

文章目录&#xff1a; vector介绍vector使用1. 默认成员函数1.1 默认构造1.2 拷贝构造1.3 析构函数1.4 赋值重载 2. 迭代器2.1 正向迭代器2.2 反向迭代器 3. 容量操作3.1 获取空间数据3.2 空间扩容3.3 大小调整3.4 空间缩容 4. 数据访问4.1 下标随机访问4.2 获取首尾元素 5. 数…

基于Java新生报到系统设计与实现(源码+lw+部署文档+讲解等)

博主介绍&#xff1a; ✌全网粉丝30W,csdn特邀作者、博客专家、CSDN新星计划导师、java领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战 ✌ &#x1f345; 文末获取源码联系 &#x1f345; &#x1f447;&#x1f3fb; 精…

Spring源码解密--事务篇

文章目录 一、事务的实现方式1、JDBC2、Spring基于xml配置编程式事务声明式事务 二、源码设计1、TransactionManager1&#xff09;TransactionManager2&#xff09;PlatformTransactionManager3&#xff09;ReactiveTransactionManager 2、TransactionDefinition3、Transaction…

Qt编写onvif工具(搜索/云台/预置位/OSD/录像存储)

一、前言 从最初编写这个工具开始的时间算起来&#xff0c;至少5年多&#xff0c;一直持续完善到今天&#xff0c;这个工具看起来小也不小大也不大&#xff0c;但是也是经历过无数个现场的洗礼&#xff0c;毫不夸张的说&#xff0c;市面上能够遇到的主流的厂商的设备&#xff…

攻防世界-Crypto-easychallenge

题目描述&#xff1a;将文件下载下来&#xff0c;只有一个pyc文件 1. 思路分析 先向chatgpt问下什么是pyc文件&#xff1a; OK&#xff0c;这里简单总结下&#xff1a; 1. pyc文件是python源码编译后的生成的二进制文件 2. 通过一些库可以逆向出pyc的源代码 那么我们需要做…
最新文章