小案例
需求如下
(1)有如下访问:
http://192.168.47.8:8081/server1/location1
访问的是:index_sr1_location1.html
http://192.168.47.8:8081/server1/location2
访问的是:index_sr1_location2.html
http://192.168.47.8:8082/server2/location1
访问的是:index_sr2_location1.html
http://192.168.47.8:8082/server2/location2
访问的是:index_sr2_location2.html
(2)如果访问的资源不存在,
返回自定义的404页面
(3)将/server1和/server2的配置使用不同的配置文件分割
将文件放到/home/www/conf.d目录下,然后使用include进行合并
(4)为/server1和/server2各自创建一个访问日志文件
把原有的nginx.conf 配置文件备份
[root@Nginx-1 conf]# ls
fastcgi.conf koi-win scgi_params
fastcgi.conf.default mime.types scgi_params.default
fastcgi_params mime.types.default uwsgi_params
fastcgi_params.default nginx.conf uwsgi_params.default
koi-utf nginx.conf.default win-utf
[root@Nginx-1 conf]# pwd
/usr/local/nginx/conf
[root@Nginx-1 conf]# cp nginx.conf.default nginx.conf #通过模板生成新的配置文件
创建如下目录
[root@Nginx-1 sbin]# tree /home/www/
/home/www/
├── conf.d # 创建
│?? ├── server1.conf # 创建
│?? └── server2.conf# 创建
└── myweb# 创建
├── 404.html# 创建
├── 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# 创建
10 directories, 9 files
[root@Nginx-1 sbin]#
配置主配置文件
/usr/local/nginx/conf/nginx.conf
##全局块 begin##
#配置允许运行Nginx工作进程的用户和用户组 www
user www;
#配置运行Nginx进程生成的worker进程数2
worker_processes 2;
#配置Nginx服务器运行对错误日志存放的路径 错误日志会存放在安装目录下的logs/error.log
error_log logs/error.log; //
#配置Nginx服务器允许时记录Nginx的master进程的PID文件路径和名称
pid logs/nginx.pid;
#配置Nginx服务是否以守护进程方法启动 默认不配置也是on
#daemon on;
##全局块 end##
##events块 begin##
events{
#设置Nginx网络连接序列化
accept_mutex on;
#设置Nginx的worker进程是否可以同时接收多个请求
multi_accept on;
#设置Nginx的worker进程最大的连接数
worker_connections 1024;
#设置Nginx使用的事件驱动模型
use epoll;
}
##events块 end##
##http块 start##
http{
#定义MIME-Type
include mime.types;
default_type application/octet-stream;
#配置允许使用sendfile方式运输 提升静态资源访问效率
sendfile on;
#配置连接超时时间
keepalive_timeout 65;
#配置请求处理日志格式
log_format server1 '===>server1 access log';
log_format server2 '===>server2 access log';
##server块 开始##
include /home/www/conf.d/*.conf; #下面的配置文件在这个路径下
##server块 结束##
}
##http块 end##
分别配置两个nginx页面配置文件
vim /home/www/conf.d/server1.conf
server {
listen 8081;
servername localhost;
access_log /home/www/myweb/server1/logs/access.log server1 ;
location /server1/location1{
root /home/www/myweb;
index index_sr1_location1.html
}
error_page 404 /404.html;
location = /404.html {
root /home/www/myweb;
index 404.html;
}
}
或者配置成这样
server{
#配置监听端口和主机名称
listen 8081;
server_name localhost;
#配置请求处理日志存放路径
access_log /home/www/myweb/server1/logs/access.log server1;
#配置错误页面
error_page 404 /404.html;
#配置处理/server1/location1请求的location
location /server1/location1{
root /home/www/myweb;
index index_sr1_location1.html;
}
#配置处理/server1/location2请求的location
location /server1/location2{
root /home/www/myweb;
index index_sr1_location2.html;
}
#配置错误页面转向
location = /404.html {
root /home/www/myweb;
index 404.html;
}
}
vim /home/www/conf.d/server2.conf
server{
#配置监听端口和主机名称
listen 8082;
server_name localhost;
#配置请求处理日志存放路径
access_log /home/www/myweb/server2/logs/access.log server2;
#配置错误页面,对404.html做了定向配置
error_page 404 /404.html;
#配置处理/server1/location1请求的location
location /server2/location1{
root /home/www/myweb;
index index_sr2_location1.html;
}
#配置处理/server2/location2请求的location
location /server2/location2{
root /home/www/myweb;
index index_sr2_location2.html;
}
#配置错误页面转向
location = /404.html {
root /home/www/myweb;
index 404.html;
}
}
写入网页内容
[root@Nginx-1 conf.d]# echo "index_sr2_location1.html server "> /home/www/myweb/server2/location1/index_sr2_location1.html
[root@Nginx-1 conf.d]# echo "index_sr2_location2.html server "> /home/www/myweb/server2/location2/index_sr2_location2.html
[root@Nginx-1 conf.d]# echo "index_sr1_location1.html server "> /home/www/myweb/server1/location1/index_sr1_location1.html
[root@Nginx-1 conf.d]# echo "index_sr1_location2.html server "> /home/www/myweb/server1/location2/index_sr1_location2.html
[root@Nginx-1 conf.d]# echo "404 error "> /home/www/myweb/404.html
测试nginx配置文件合规
[root@Nginx-1 sbin]# pwd
/usr/local/nginx/sbin
[root@Nginx-1 sbin]# ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@Nginx-1 sbin]#
每访问一次 就有一条日志输出
Nginx配置成系统服务
(1) 在/usr/lib/systemd/system
目录下添加nginx.service,内容如下:
vim /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
(2)添加完成后如果权限有问题需要进行权限设置
chmod 755 /usr/lib/systemd/system/nginx.service
(3)使用系统命令来操作Nginx服务
启动: systemctl start nginx
停止: systemctl stop nginx
重启: systemctl restart nginx
重新加载配置文件: systemctl reload nginx
查看nginx状态: systemctl status nginx
开机启动: systemctl enable nginx
可以正常使用命令
[root@Nginx-1 ~]# systemctl start nginx
[root@Nginx-1 ~]# systemctl status nginx
● nginx.service - nginx web service
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since 一 2022-06-20 15:46:06 CST; 5s ago
Docs: http://nginx.org/en/docs/
Process: 1199 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 1198 ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 1201 (nginx)
CGroup: /system.slice/nginx.service
├─1201 nginx: master process /usr/local/nginx/sbin/nginx
├─1202 nginx: worker process
└─1203 nginx: worker process
6月 20 15:46:06 Nginx-1 systemd[1]: Starting nginx web service...
6月 20 15:46:06 Nginx-1 nginx[1198]: nginx: the configuration file /...k
6月 20 15:46:06 Nginx-1 nginx[1198]: nginx: configuration file /usr/...l
6月 20 15:46:06 Nginx-1 systemd[1]: Failed to parse PID from file /u...t
6月 20 15:46:06 Nginx-1 systemd[1]: Started nginx web service.
Hint: Some lines were ellipsized, use -l to show in full.
Nginx命令配置到系统环境
[root@Nginx-1 ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
configure arguments:
[root@Nginx-1 ~]# nginx -v
-bash: nginx: 未找到命令
[root@Nginx-1 ~]#
(1)修改/etc/profile
文件
vim /etc/profile
在最后一行添加
export PATH=$PATH:/usr/local/nginx/sbin
(2)使之立即生效
source /etc/profile
(3)执行nginx命令
[root@Nginx-1 ~]# vim /etc/profile
[root@Nginx-1 ~]# source /etc/profile
[root@Nginx-1 ~]# nginx -v
nginx version: nginx/1.16.1
[root@Nginx-1 ~]#
Nginx静态资源部署
Nginx静态资源概述
我们请求的内容就分为两种类型,一类是静态资源、一类是动态资源。 静态资源即指在服务器端真实存在并且能直接拿来展示的一些文件,比如常见的html页面、css文件、js文件、图 片、视频等资源; 动态资源即指在服务器端真实存在但是要想获取需要经过一定的业务逻辑处理,根据不同的条件展示在页面不同 这 一部分内容,比如说报表数据展示、根据当前登录用户展示相关具体数据等资源;
Nginx处理静态资源的内容,我们需要考虑下面这几个问题:
(1)静态资源的配置指令 (2)静态资源的配置优化 (3)静态资源的压缩配置指令 (4)静态资源的缓存处理 (5)静态资源的访问控制,包括跨域问题和防盗链问题
Nginx静态资源的配置指令
一下指令 通过nginx访问静态资源的时候会用到,但不是nginx为配置静态资源的而设置的专用指令。
listen指令
listen:用来配置监听端口。