Nginx05-基础配置案例

零、文章目录

Nginx05-基础配置案例

1、案例需求

(1)有如下访问
(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)访问页面

image-20240926110844626

image-20240926110903931

image-20240926110925747

image-20240926110938161

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李宥小哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值