欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > IT业 > Nginx05-基础配置案例

Nginx05-基础配置案例

2025/5/15 6:05:28 来源:https://blog.csdn.net/liyou123456789/article/details/142745604  浏览:    关键词:Nginx05-基础配置案例

零、文章目录

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/location2touch /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/location2touch /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-Typedefault_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 请求的 locationroot /home/www/myweb;index index_sr1_location1.html;       # 这是 server1 下的 location1 的 index_sr1_location1.html}location /server1/location2{   # 配置处理 /server1/location2 请求的 locationroot /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 请求的 locationroot /home/www/myweb;index index_sr2_location1.html;   # 这是 server2 下的 location1 的 index_sr2_location1.html}location /server2/location2{   # 配置处理 /server2/location2 请求的 locationroot /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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词