web集群之Ngnix相关配置
1. 源码安装nginx,并提供服务脚本。
1)、下载
[root@localhost ~]# wget -c https://repo.huaweicloud.com/nginx/nginx-1.20.0.tar.gz
2)、解压
[root@localhost ~]# tar xf nginx-1.20.0.tar.gz -C /usr/local/src/
[root@localhost ~]# cd /usr/local/src/nginx-1.20.0/
[root@localhost nginx-1.20.0]#
3)、开始安装 Nginx
#创建Ngnux用户
[root@localhost nginx-1.20.0]# useradd nginx -s /sbin/nologin -M#安装依赖
[root@localhost nginx-1.20.0]# yum install gcc gcc-c++ make pcre-devel openssl-devel perl-devel perl-ExtUtils-Embed -y#配置功能
[root@localhost nginx-1.20.0]# ./configure --prefix=/usr/local/nginx \
> --user=nginx --group=nginx \
> --with-threads \
> --with-http_ssl_module \
> --with-http_gzip_static_module \
> --with-http_auth_request_module \
> --with-http_stub_status_module \
> --with-http_perl_module \
> --with-stream \
> --with-pcre#编译安装
[root@localhost nginx-1.20.0]# make
[root@localhost nginx-1.20.0]# make install[root@localhost nginx-1.20.0]# ln -sv /usr/local/nginx/sbin/nginx /usr/sbin/n
[root@localhost nginx-1.20.0]# nginx -v
nginx version: nginx/1.20.0
配置服务脚本
[root@localhost ~]# vim /usr/lib/systemd/system/nginx.service
[root@localhost ~]# more /usr/lib/systemd/system/nginx.service[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true[Install]
WantedBy=multi-user.target
修改配置文件指定pid位置
[root@localhost nginx-1.20.0]# vim /usr/local/nginx/conf/nginx.confpid /usr/local/nginx/logs/nginx.pid;
启动服务
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl start nginx
测试
配置nginx子配置文件
[root@localhost ~]# cd /usr/local/nginx/
[root@localhost nginx]# mkdir conf.duser nginx; #指定用户http {include /usr/local/nginx/conf.d/*.conf; #在http模块下指定子配置文件位置
重启
[root@localhost nginx]# systemctl restart nginx
2. 配置基于域名的虚拟主机。
通过子配置文件的方式配置
1)、配置测试界面
[root@localhost ~]# cd /usr/local/nginx/conf.d
[root@localhost conf.d]# cd ../html/
[root@localhost html]# mv index.html {,.bak}
[root@localhost html]# vim index.html
[root@localhost html]# more index.html
<html>
<meta charset="utf-8">
<head><title>TEST Site</title>
</head>
<body>测试页面<table border=1><tr> <td>01</td> <td>云计算 </td> </tr> <tr> <td>02</td> <td>大数据</td> </tr> <tr> <td>03</td> <td>人工智</td> </tr></table>
<body>
</html>
2)、配置虚拟主机
[root@localhost ~]# cd /usr/local/nginx/conf.d/
[root@localhost conf.d]# vim vhost.conf[root@localhost conf.d]# vim vhost.confserver {listen 80;server_name web1.test.com;location / {root /data/web1/html;index index.html index.html;}
}server {listen 80;server_name web2.test.com;location / {root /data/web2/html;index index.html index.html;}
}[root@localhost conf.d]# mkdir /data/web{1,2}/html -p
[root@localhost conf.d]# echo "web1 test page " > /data/web1/html/index.html
[root@localhost conf.d]# echo "web2 test page " > /data/web2/html/index.html[root@localhost conf.d]# vim /etc/hosts192.168.159.133 web1.test.com web2.test.com[root@localhost conf.d]# systemctl restart nginx
测试:
[root@localhost conf.d]# curl web1.test.com
web1 test page
[root@localhost conf.d]# curl web2.test.com
web2 test page
3. 配置nginx基于用户和地址的访问控制。
1)、基于地址的访问控制
[root@localhost ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;server_name web1.test.com;location / {root /data/web1/html;index index.html index.html;autoindex on;if (!-f $request_filename) {rewrite /.* /err.html permanent;}deny 192.168.159.136; //拒绝该地址allow 192.168.159.0/24; //允许改网段}
}
在地址为192.168.159.136端测试:
[root@rs1 ~]# curl http://192.168.159.133/forum/index.html
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.20.0</center>
</body>
</html>
2)、基于用户的访问控制
auth_basic指令包含一个具有测试用户名和密码的HTTP基本认证,指定的参数将用于认证域。如果将值设置为“off”则忽略下级指令继承的动作。auth_basic_user_file指令为验证域指定了密码文件,0.6.7版本以后这里指定的文件是nginx.conf所在目录的相对路径,而不是–prefix指定的路径。
配置认证
server {listen 80;server_name web1.test.com;location / {root /data/web1/html;index index.html index.html;autoindex on;if (!-f $request_filename) {rewrite /.* /err.html permanent;}allow 192.168.159.0/24;auth_basic "Restricted";auth_basic_user_file /data/web1/webpass;}
}
下载httpd-tools
[root@localhost ~]# yum install httpd-tools -y
创建账号密码, 此账号密码就是用户访问网站时需要输入的
[root@localhost ~]# htpasswd -c -m /data/web1/webpass tom
New password:
Re-type new password:
Adding password for user tom
重启服务
[root@localhost html]# systemctl restart nginx
测试
4. 配置nginx rewrite,要求如果访问不存在的任意网页都重定向到错误页面,错误页面内容自行定义。
[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# vim err.html
this page is non-existent[root@localhost html]# vim /usr/local/nginx/conf.d/vhost.conf server {listen 80;server_name web1.test.com; location / {root /data/web1/html;index index.html index.html;autoindex on;if (!-f $request_filename) {rewrite /.* /err.html permanent;}}
}[root@localhost html]# systemctl restart nginx
web集群之Ngnix相关配置相关推荐
- Linux相关配置 集群免密码登录配置
Linux相关配置 集群免密码登录配置 实验目的要求: 1.完成VMware Workstation安装,会应用相关操作 2.掌握虚拟机中Linux CentOS 7.4操作系统安装 3.完成静态网络 ...
- WEB集群与各种负载均衡简介 (资源)
2019独角兽企业重金招聘Python工程师标准>>> 负载均衡构架图 集群(Cluster):是一组独立的计 ...
- Nginx+Keepalived+Tomcat之动静分离的web集群
为小公司提供大概一天持续在100万/日之间访问的高性能.高可用.高并发访问及动静分离的web集群方案 Nginx+Keepalived 高可用.反向代理 ...
- SaltStck 搭建Web集群运用示例 (一)
saltstack是一个非常强大的管理工具,使用saltstack会做到标准化的管理,下面就以一个简单的示例来更加具体的了解一下saltstack的功能特性. 使用saltstack来搭建一个简单的w ...
- aproxy配合Nginx搭建Web集群部署实验(图文详解)
文章目录 一.常见的Web集群调度器 二.Haproxy应用分析 三.Haproxy调度算法原理 四.Haproxy搭建 Web 群集 实验所需安装包 环境配置 1.部署haproxy服务器(192. ...
- 高效多用的群集-Haproxy搭建Web集群
Haproxy搭建Web集群 Haproxy搭建Web集群 一.Haproxy前言 二.常见的Web集群调度器 三.Haproxy应用分析 四.Haproxy高性能负载均衡主要优点 五.四层与七层负载 ...
- 一把王者的时间就写完了一个nginx的web集群项目
构建一个基于nginx的web集群项目 一.什么是负载均衡? 二.为什么需要负载均衡? 1.编译安装nginx 2.解决软件的依赖关系,需要安装的软件包 3.新建luogan用户和组 4.下载ngin ...
- RHCS+Conga+GFS+cLVM共享存储的高可用性web集群
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://sohudrgon.blog.51cto.com/3088108/1599984 ...
- 03_项目-基于Docker Swarm的高可用Web集群
文章目录 项目名称:基于Docker Swarm的高可用Web集群 网络拓扑图 数据流程图 项目环境:Docker 20.10.3,CentOS 8.2(8台 1核1G),Ansible 2.9.17 ...
- 项目名称:基于Docker Swarm的高可用Web集群
文章目录 项目名称:基于Docker Swarm的高可用Web集群 网络拓扑图 数据流程图 项目环境:Docker 20.10.3,CentOS 8.2(8台 1核1G),Ansible 2.9.17 ...
最新文章
- word自动消除html标签,清理Word生成HTML的冗余;清理与清除HTML标签
- 一文提升你对深度学习分布式训练的整体认知!
- java csv格式文件写入_java csv文件写入
- 每个开发阶段对应的最流行的Java工具
- [网络流24题] 最长k可重线段集问题 (费用流)
- VM options
- 黑苹果hidp显示不清楚_让黑苹果开启Retina的方法
- 如何用PR制作GIF图(Premiere)
- IDEA修改创建多级package包结构样式
- 解决驱动问题导致的浏览器频繁出现闪烁
- 机上WiFi 万米高空不断线
- 【Unity3D进阶4-14】Unity3D 连接MySQL数据库
- 尼康d850相机参数测试软件,尼康(Nikon)D850 单机数码相机ISO感光度评测-ZOL中关村在线...
- java 获取包下的所有类,附完整源码和测试代码
- java电信计费项目论文_电信计费系统的设计与实现毕业论文.doc
- vue使用ttf字体包压缩 字蛛 font-spider
- DirectX图形开发(一)-基本概念
- 小米手机 root权限 获取
- php 206实现微信浏览器,PHP实现限制页面只能在微信自带浏览器访问的代码
- linux查看sata端口速率,[linux] 查看SATA速度和具体设备