Nginx05-基础配置案例

news2024/10/10 6:08:32

零、文章目录

Nginx05-基础配置案例

1、案例需求

(1)有如下访问
  • http://192.168.119.161:8081/server1/location1 访问的是:index_sr1_location1.html
  • http://192.168.119.161:8081/server1/location2 访问的是:index_sr1_location2.html
  • http://192.168.119.161:8082/server2/location1 访问的是:index_sr2_location1.html
  • http://192.168.119.161:8082/server2/location2 访问的是:index_sr2_location2.html
(2)如果访问的资源不存在
  • 返回自定义的404页面
(3)将/server1和/server2使用各自配置文件
  • 将文件放到/home/www/conf.d目录下
  • 然后使用include进行合并
(4)为/server1和/server2各自创建访问日志

2、准备文件

(1)创建文件结构
# 创建 404 页面
touch /home/www/404.html

# 创建 conf.d 目录
mkdir /home/www/conf

# 创建两个配置文件
touch /home/www/conf/server1.conf
touch /home/www/conf/server2.conf

# 创建 myweb 目录
mkdir /home/www/myweb

# 创建 server1 目录和其子目录以及html文件
mkdir -p /home/www/myweb/server1/location1
mkdir -p /home/www/myweb/server1/location2

touch /home/www/myweb/server1/location1/index_sr1_location1.html
touch /home/www/myweb/server1/location2/index_sr1_location2.html

# 创建 server1 日志目录和日志文件
mkdir -p /home/www/myweb/server1/logs
touch /home/www/myweb/server1/logs/access.log

# 创建 server2 目录和其子目录以及html文件
mkdir -p /home/www/myweb/server2/location1
mkdir -p /home/www/myweb/server2/location2

touch /home/www/myweb/server2/location1/index_sr2_location1.html
touch /home/www/myweb/server2/location2/index_sr2_location2.html

# 创建 server2 日志目录和日志文件
mkdir -p /home/www/myweb/server2/logs
touch /home/www/myweb/server2/logs/access.log

# 创建完成整体文件结构如下
[root@localhost ~]# tree /home/www/
/home/www/
├── 404.html
├── conf
│   ├── server1.conf
│   └── server2.conf
└── myweb
    ├── server1
    │   ├── location1
    │   │   └── index_sr1_location1.html
    │   ├── location2
    │   │   └── index_sr1_location2.html
    │   └── logs
    │       └── access.log
    └── server2
        ├── location1
        │   └── index_sr2_location1.html
        ├── location2
        │   └── index_sr2_location2.html
        └── logs
            └── access.log
(2)准备配置文件
  • 配置文件/usr/local/nginx/conf/nginx.conf内容如下
user www; # 配置允许运行 Nginx 工作进程的用户和用户组
worker_processes 2;  # 配置运行 Nginx 进程生成的 worker 进程数
error_log logs/error.log;  # 配置 Nginx 服务器运行对错误日志存放的路径
pid logs/nginx.pid;   # 配置 Nginx 服务器允许时记录 Nginx 的 master 进程的 PID 文件路径和名称
daemon on;   # 配置 Nginx 服务是否以守护进程方法启动

events{
	accept_mutex on;   # 设置 Nginx 网络连接序列化,解决惊群
	multi_accept on;   # 设置 Nginx 的 worker 进程是否可以同时接收多个请求
	worker_connections 1024;   # 设置 Nginx 的 worker 进程最大的连接数
	use epoll;   # 设置 Nginx 使用的事件驱动模型
}

http{

	include mime.types;   # 定义 MIME-Type
	default_type application/octet-stream;
	sendfile on;   # 配置允许使用 sendfile 方式运输
	keepalive_timeout 65;   # 配置连接超时时间
	
	# 配置请求处理日志格式
	log_format server1 '===>server1 access log';
	log_format server2 '===>server2 access log';
	
	include /home/www/conf/*.conf;  # 引用其他 conf 文件
}
  • 配置文件/home/www/conf/server1.conf内容如下
server{
  listen 8081;   # 配置监听端口和主机名称
  server_name localhost;
  access_log /home/www/myweb/server1/logs/access.log server1;   # 配置请求处理日志存放路径
  error_page 404 /404.html;   # 配置错误页面

  location /server1/location1{   # 配置处理 /server1/location1 请求的 location
      root /home/www/myweb;
      index index_sr1_location1.html;       # 这是 server1 下的 location1 的 index_sr1_location1.html
  }

  location /server1/location2{   # 配置处理 /server1/location2 请求的 location
      root /home/www/myweb;
      index index_sr1_location2.html;    # 这是 server1 下的 location2 的 index_sr1_location2.html
  }

  location = /404.html {   # 配置错误页面转向
      root /home/www;
      index 404.html;
  }
}
  • 配置文件/home/www/conf/server2.conf内容如下
server{
  listen 8082;   # 配置监听端口和主机名称
  server_name localhost;
  access_log /home/www/myweb/server2/logs/access.log server2;   # 配置请求处理日志存放路径
  error_page 404 /404.html;   # 配置错误页面,对404.html做了定向配置
  
  location /server2/location1{   # 配置处理 /server1/location1 请求的 location
      root /home/www/myweb;
      index index_sr2_location1.html;   # 这是 server2 下的 location1 的 index_sr2_location1.html
  }
 
  location /server2/location2{   # 配置处理 /server2/location2 请求的 location
      root /home/www/myweb;
      index index_sr2_location2.html;    # 这是 server2 下的 location2 的 index_sr2_location2.html
  }
  
  location = /404.html {   # 配置错误页面转向
      root /home/www;
      index 404.html;
  }
}
(3)准备页面文件
  • 页面文件/home/www/404.html内容如下
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
	<h1>404 Not Found</h1>
</body>
</html>
  • 页面文件/home/www/myweb/server1/location1/index_sr1_location1.html内容如下
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
	<h1>server1下面的loaction1下面的index_sr1_location1.html</h1>
</body>
</html>
  • 页面文件/home/www/myweb/server1/location2/index_sr1_location2.html内容如下
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
	<h1>server1下面的loaction2下面的index_sr1_location2.html</h1>
</body>
</html>
  • 页面文件/home/www/myweb/server2/location1/index_sr1_location1.html内容如下
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
	<h1>server2下面的loaction1下面的index_sr2_location1.html</h1>
</body>
</html>
  • 页面文件/home/www/myweb/server2/location2/index_sr1_location2.html内容如下
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
	<h1>server2下面的loaction2下面的index_sr2_location2.html</h1>
</body>
</html>

3、访问测试

(1)重载配置文件
/usr/local/nginx/sbin/nginx -s reload
(2)访问页面
  • http://192.168.119.161:8081/server1/location1 访问的是:index_sr1_location1.html

image-20240926110844626

  • http://192.168.119.161:8081/server1/location2 访问的是:index_sr1_location2.html

image-20240926110903931

  • http://192.168.119.161:8082/server2/location1 访问的是:index_sr2_location1.html

image-20240926110925747

  • http://192.168.119.161:8082/server2/location2 访问的是:index_sr2_location2.html

image-20240926110938161

  • 访问一个不存在的页面http://192.168.119.161:8082/server3/location3,返回404页面

image-20240926111051977

4、配置成系统服务

(1)当前操作中的问题
  • 启动、关闭或重新加载nginx配置文件,都需要先进入到nginx的安装目录的sbin目录,然后使用nginx的二级制可执行文件来操作,相对来说操作比较繁琐。
  • 每次开机启动之后,都要启动一次nginx。
(2)配置成系统服务
  • /usr/lib/systemd/system目录下添加nginx.service文件
[Unit]
Description=nginx web service
Documentation=http://nginx.org/en/docs/
After=network.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
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=default.target
  • 添加完成后如果权限有问题需要进行权限设置
chmod 755 /usr/lib/systemd/system/nginx.service
(3)系统命令操作Nginx
# 启动 Nginx
systemctl start nginx

# 停止 Nginx
systemctl stop nginx

# 重启 Nginx
systemctl restart nginx

# 重新加载配置文件
systemctl reload nginx

# 查看 Nginx 状态
systemctl status nginx

# 开机启动
systemctl enable nginx

# 关闭开启启动
systemctl disable nginx

5、Nginx命令配置到系统环境

(1)当前操作的问题
  • Nginx安装目录下的二级制可执行文件nginx的很多命令,要想使用这些命令前提是需要进入sbin目录下才能使用,很不方便。
  • 我们可以将该二进制可执行文件加入到系统的环境变量,这样的话在任何目录都可以使用nginx对应的相关命令。
(2)命令配置到系统环境
  • 修改/etc/profile文件
vim /etc/profile
# 在最后一行添加
export PATH=$PATH:/usr/local/nginx/sbin
  • 使之立即生效
source /etc/profile
(3)验证命令
  • 验证成功,任何地方均可使用命令
[root@localhost ~]# nginx -v
nginx version: nginx/1.26.2

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

慢接口分析与优化总结

文章目录 1. 慢接口优化的意义2. 接口耗时构成3. 优化技巧3.1. 内部代码逻辑异步执行[异步思想]并行优化拒绝阻塞等待预分配与循环使用[池化思想]线程池合理设计锁粒度避免过粗优化程序结构 3.2. 缓存恰当引入缓存[空间换时间思想]缓存延迟优化提前初始化缓存[预取思想] 3.3. 数…

工具函数(截取文本第一个字为图片)

const subStringToImage (params) > {const { str ,color #FFF,background #4F54FF,size 60,fontSize 20 } paramsif(str.length < 0) return console.error(字符不能为空!)const text str.slice(0, 1)const canvas document.createElement(canvas)const ctx …

github 国内文件加速下载

参看;https://www.cnblogs.com/ting1/p/18356265 在源网址前加上 https://hub.gitmirror.com/ 或https://mirror.ghproxy.com/&#xff0c;例如&#xff1a; https://hub.gitmirror.com/https://github.com/t1m0thyj/WinDynamicDesktop/releases/download/v5.4.1/WinDynamicD…

算法题总结(十)——二叉树上

#二叉树的递归遍历 // 前序遍历递归LC144_二叉树的前序遍历 class Solution {public List<Integer> preorderTraversal(TreeNode root) {List<Integer> result new ArrayList<Integer>(); //也可以把result 作为全局变量&#xff0c;只需要一个函数即可。…

公开数据集网站分享

参考链接&#xff1a;常用的医学组织切片细胞图像数据集_细胞分割数据集-CSDN博客文章浏览阅读1.3w次&#xff0c;点赞32次&#xff0c;收藏133次。乳腺癌细胞图像数据集、血细胞图像数据集、HE染色切片、疟疾细胞图像图像识别、分类、分割_细胞分割数据集https://blog.csdn.ne…

Redis list 类型

list类型 类型介绍 列表类型 list 相当于 数组或者顺序表 list内部的编码方式更接近于 双端队列 &#xff0c;支持头插 头删 尾插 尾删。 需要注意的是&#xff0c;Redis的下标支持负数下标。 比如数组大小为5&#xff0c;那么要访问下标为 -2 的值可以理解为访问 5 - 2 3 …

Linux dlsym和直接调用函数地址解析分析

dlsym 函数是 Linux 下动态链接库&#xff08;shared library&#xff09;编程中的一个重要函数。它用于在运行时获取动态链接库中符号的地址&#xff0c;通常用于获取函数指针或变量的地址。 以下是 dlsym 函数的基本用法和示例。 1. 函数原型 void *dlsym(void *handle, c…

【超详细】基于YOLOv11的PCB缺陷检测

主要内容如下&#xff1a; 1、数据集介绍 2、下载PCB数据集 3、不同格式数据集预处理&#xff08;Json/xml&#xff09;&#xff0c;制作YOLO格式训练集 4、模型训练及可视化 5、Onnxruntime推理 运行环境&#xff1a;Python3.8&#xff08;要求>3.8&#xff09;&#xff…

ubuntu ssh远程执行k8s命令报错the connection to the server localhost:8080 was refused

修改前&#xff1a; ssh root192.168.31.167 kubectl apply -f /root/jenkinsexcute/saas.demo.api.k8s.yml --recordecho "export KUBECONFIG/etc/kubernetes/admin.conf" >> /root/.bashrc 修改后 添加一段&#xff1a;export KUBECONFIG/etc/kubernetes/a…

【专题】操作系统概述

1. 操作系统的目标和作用 操作系统的目标与应用环境有关。 在查询系统中所用的OS&#xff0c;希望能提供良好的人—机交互性&#xff1b; 对于应用于工 业控制、武器控制以及多媒体环境下的OS&#xff0c;要求其具有实时性&#xff1b; 对于微机上配置的OS&#xff0c;则更看…

什么是强基计划?

“强基计划”是中国教育部于2020年推出的一项全新的高等教育招生改革计划&#xff0c;旨在通过更加科学、公正的选拔机制&#xff0c;选拔出有志于基础学科并具备扎实学科功底、创新潜质的优秀学生&#xff0c;从而推动国家基础学科的发展&#xff0c;提升自主创新能力。与传统…

【自动驾驶】UniAD代码解析

1.参考 论文&#xff1a;https://arxiv.org/pdf/2212.10156 代码&#xff1a;https://github.com/OpenDriveLab/UniAD 2.环境配置 docs/INSTALL.md &#xff08;1&#xff09;虚拟conda环境 conda create -n uniad python3.8 -y conda activate uniad &#xff08;2&#…

微信小程序和抖音小程序的分享和广告接入代码

开发完成小程序或者小游戏之后&#xff0c;我们为什么要接入分享和广告视频功能&#xff0c;主要原因有以下几个方面。 微信小程序和抖音小程序接入分享和广告功能主要基于以下几个原因&#xff1a; 用户获取与增长&#xff1a;分享功能可以帮助用户将小程序内容传播给更多人&…

C语言刷题--有关闰年

满足以下一种即是闰年 能被4整除&#xff0c;但不能被100整除能被400整除 //输入年&#xff0c;月&#xff0c;输出该月的天数 //1月 31,28,31,30,31,30,31,31,30,31,30,31int is_leap_year(int y) {if (((y % 4 0) && (y % 100 ! 0)) || y % 400 0)return 1;return…

步进电机和步进电机驱动器详解

一、步进电机的概念 步进电机是通过步进电机配套的驱动器&#xff0c;将控制器传来的脉冲信号转换成角位移的开环电机&#xff08;没有反馈&#xff09;。 步进电机工作时的位置和速度信号不反馈给控制系统&#xff0c;如果电机工作时的位置和速度信号反馈给控制系统&#xff…

《从零开始大模型开发与微调》真的把大模型说透了!零基础入门一定要看!

2022年底&#xff0c;ChatGPT震撼上线&#xff0c;大语言模型技术迅速“席卷”了整个社会&#xff0c;人工智能技术因此迎来了一次重要进展。与大语言模型相关的研发岗薪资更是水涨船高&#xff0c;基本都是5w月薪起。很多程序员也想跟上ChatGPT脚步&#xff0c;今天给大家带来…

apache.poi读取.xls文件时The content of an excel record cannot exceed 8224 bytes

目录 问题描述版本定位&#xff1a;打印size最大的Record定位&#xff1a;RefSubRecord解决代码 问题描述 使用apache.poi读取.xls文件时有The content of an excel record cannot exceed 8224 bytes的报错。待读取的文件的内容也是通过apache.poi写入的&#xff0c;我的文件修…

【sqlmap】sqli-labs速通攻略

sqli-labs工具速通 Less-1 sqlmap -u http://127.0.0.1:8081/Less-1/?id1 --batch --dbs sqlmap -u http://127.0.0.1:8081/Less-1/?id1 --batch -D security --tables sqlmap -u http://127.0.0.1:8081/Less-1/?id1 --batch -D security -T users --columns sqlmap -u ht…

购物清单 | 双十一加购率最高好物合集,数码购物车必备!

​双十一来临&#xff0c;小伙伴们肯定已经被种草了很多很多清单&#xff0c;开始买买买了&#xff01;但是&#xff0c;作为一个数码博主&#xff0c;怎么能少了数码产品&#xff01;今天我给大家准备了一份数码人专属的购物清单&#xff0c;快来看看吧&#xff01; 运动耳机…

[水墨:创作周年纪念] 特别篇!

本篇是特别篇&#xff01;&#xff01; 个人主页水墨不写bug // _ooOoo_ // // o8888888o // // 88" . "88 …