ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

Nginx分布式框架详解-基础32-36nginx基础配置实例

2022-08-27 12:00:10  阅读:198  来源: 互联网

标签:www Nginx 32 36nginx server1 nginx html home


nginx基础配置实例需求分析

前面我们已经对 Nginx 服务器默认配置文件的结构和涉及的基本指令做了详细的阐述。通过这些指令的合理配置,我们就可以让一台 Nginx 服务器正常工作,并且提供基本的 Web 服务器功能。

接下来我们将通过一个比较完整和最简单的基础配置实例,来巩固下前面所学习的指令及其配置。

需求
  1. 有如下访问:
    http://192.168.199.27:8081/server1/location1 访问的是:index_sr1_location1.html http://192.168.199.27:8081/server1/location2 访问的是:index_sr1_location2.html http://192.168.199.27:8082/server2/location1 访问的是:index_sr2_location1.html http://192.168.199.27:8082/server2/location2 访问的是:index_sr2_location2.html
  2. 如果访问的资源不存在,返回自定义的 404 页面
  3. 将 /server1 和 /server2 的配置使用不同的配置文件分割,将两个文件文件放到 /home/www/conf.d 目录下,然后在 Nginx 的配置文件使用 include 合并两个文件
  4. 为 /server1 和 /server2 各自创建一个访问日志文件
实现
# 创建 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 目录和其子目录以及 index.html 文件
mkdir -p /home/www/myweb/server1/location1
mkdir -p /home/www/myweb/server1/location2

touch /home/www/myweb/server1/location1/index.html
touch /home/www/myweb/server1/location2/index.html

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

# 创建 server2 目录和其子目录以及 index.html 文件
# 和创建 server1 步骤一样,把 1 改为 2 即可

修改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 文件
}

第 25 行代码使用 include 将 service1 和service2 的配置文件进行引用。以后无需修改主配置文件,只需要引入子配置文件即可,主配置文件作为默认值,子配置文件的内容会覆盖和主配置文件相同的内容。

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.html;       # 这是 server1 下的 location1 的 index.html
  }

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

  location = /404.html {   # 配置错误页面转向
      root /home/www;
      index 404.html;
  }
}

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.html;   # 这是 server2 下的 location1 的 index.html
  }
 
  location /server2/location2{   # 配置处理 /server2/location2 请求的 location
      root /home/www/myweb;
      index index.html;    # 这是 server2 下的 location2 的 index.html
  }
  
  location = /404.html {   # 配置错误页面转向
      root /home/www;
      index 404.html;
  }
}

分别创建html文件

/home/www/myweb/server1/location1/index.html
/home/www/myweb/server1/location2/index.html
/home/www/myweb/server2/location1/index.html
/home/www/myweb/server2/location2/index.html

创建日志目录

mkdir /home/www/myweb/server1/logs
mkdir /home/www/myweb/server2/logs

如果是云服务器需要开启8081,8082端口
配置好后,重启Nginx服务

/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload

浏览器访问:

http://mayanan.cn:8081/server1/location1/
http://mayanan.cn:8081/server1/location2/
http://mayanan.cn:8082/server2/location1/
http://mayanan.cn:8082/server2/location2/

nginx配置成系统服务

nginx服务操作的问题

经过前面的操作,我们会发现,如果想要启动、关闭或重新加载 Nginx 配置文件,都需要先进入到 Nginx 的安装目录的 sbin 目录,然后使用 Nginx 的二级制可执行文件 nginx 来操作,相对来说操作比较繁琐,这块该如何优化?另外如果我们想把 Nginx 设置成随着服务器启动就自动完成启动操作,又该如何来实现?
这就需要用到接下来我们要讲解的两个知识点:

  • nginx服务启停配置
  • nginx全局命令配置
服务启停配置

把 Nginx 应用服务设置成为系统服务,方便对 Nginx 服务的启动和停止等相关操作,具体实现步骤:
在 /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

注意:可执行文件 nginx 根据自己的路径进行修改,以及 .conf 配置文件和 .pid 文件的路径。这份内容是基于默认安装目录的。

添加完成后,如果权限有问题需要进行权限设置,没有则忽略这一步
chmod 755 /usr/lib/systemd/system/nginx.service
使用系统命令来操作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

nginx命令配置到系统环境

前面我们介绍过 Nginx 安装目录下的二级制可执行文件 nginx 的很多命令,要想使用这些命令前提是需要进入 sbin 目录下才能使用,很不方便,如何去优化,我们可以将该二进制可执行文件加入到系统的环境变量,这样的话在任何目录都可以使用 nginx 对应的相关命令。具体实现步骤如下:
方法1:
修改/etc/profile文件

vim /etc/profile

# 在最后一行添加
export PATH=$PATH:/usr/local/nginx/sbin

可执行文件 nginx 的路径根据自己的路径修改,这里是默认路径。
source /etc/profile

方法2:
可可执行文件nginx拷贝一份到/usr/bin目录下
cop /usr/local/nginx/sbin/ngin /usr/bin
任意位置执行nginx命令,测试成功
nginx -v

标签:www,Nginx,32,36nginx,server1,nginx,html,home
来源: https://www.cnblogs.com/mayanan/p/16630295.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有