day12.2
web基础入门
nginx概述
开源 直接获取源代码
高性能 支持海量开发
可靠 服务稳定
nginx应用场景

nginx安装
1.epel仓库(阿里云)
2.官方仓库
使用官方源
nginx官方网站:TP



[root@web02 ~]
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[root@web02 ~]
[root@web02 ~]
[root@web02 ~]
[root@web02 ~]
nginx version: nginx/1.22.0
[root@web02 ~]
nginx的暂停
-c:指定配置文件路径
-t:检查配置文件的语法
-s:启停重载,服务操作
-v:查看版本号
-V:查看版本和编译参数
systemctl start nginx
nginx
绝对路径
systemctl stop nginx
nginx -s stop
绝对路径 -s stop
systemctl reload nginx
nginx -s reload
绝对路径 -s reload
源码安装启动脚本
[root@web01 ~]
ln -s /application/nginx-1.20.2 /opt/nginx
echo 'PATH="/usr/local/nginx/sbin:$PATH"' > /etc/profile.d/nginx.sh
cat >> /usr/lib/systemd/system/nginx.service <<EOF
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/application/nginx/nginx.pid
ExecStart=/application/nginx/sbin/nginx -c /application/nginx/conf/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /application/nginx/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /application/nginx/nginx.pid)"
[Install]
WantedBy=multi-user.target
EOF
nginx配置文件
路径 |
类型 |
作用 |
/etc/nginx/nginx.conf |
配置文件 |
nginx主配置文件 |
/etc/nginx/conf.d/default.conf |
配置文件 |
nginx网站示例配置文件 |
路径 |
类型 |
作用 |
/etc/nginx/fastcgi_params |
配置文件 |
Fastcgit代理配置文件 |
/etc/nginx/scgi_params |
配置文件 |
scgi代理配置文件 |
/etc/nginx/uwsgi_params |
配置文件 |
uwsgi代理配置文件 |
路径 |
类型 |
作用 |
/etc/nginx/win-utf |
配置文件 |
Nginx编码转换映射文件 |
/etc/nginx/koi-utf |
配置文件 |
Nginx编码转换映射文件 |
/etc/nginx/koi-win |
配置文件 |
Nginx编码转换映射文件 |
/etc/nginx/mime.types |
配置文件 |
Content-Type与扩展名 |
路径 |
类型 |
作用 |
/usr/sbin/nginx |
命令 |
nginx命令行管理终端工具 |
/usr/sbin/nginx-debug |
命令 |
nginx命令行与终端调试工具 |
路径 |
类型 |
作用 |
/var/log/nginx/ |
目录 |
nginx默认存放日志的目录 |
/etc/logrotate.d/nginx |
配置文件 |
nginx默认日志切割 |
nginx配置文件解析
[root@web02 ~]
'核心层、核心模块、全局配置文件'
user nginx;
worker_processes auto;(auto 自动根据CPU的核心数来启动对应的工作进程数)
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
'事件层(事件模块)'
events {
worker_connections 1024;
}
'http层、http模块、网站配置'
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
}
"注意:写虚拟主机配置文件,结尾一定要以.conf结尾"
日志格式
$remote_addr
$remote_user
[$time_local]
$request
$status
$body_bytes_sent
$http_referer
$http_user_agent
$http_x_forwarded_for
虚拟主机文件配置
server{
listen 80;
location / {
root /wc;
index index.html;
}
}
多虚拟主机(多web网站配置)

基于IP的多虚拟主机

[root@web01 conf.d]
[root@web01 conf.d]
[root@web01 conf.d]
server{
listen 80;
server_name 10.0.0.10;
location /{
root /game/h5_games;
index index.html;
}
}
[root@web01 conf.d]
server{
listen 80;
server_name 10.0.0.7;
root /code;
location /{
index index.html;
}
location /zls{
index index_1.html huanglong.html;
}
}
[root@web01 conf.d]
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.7 netmask 255.255.255.0 broadcast 10.0.0.255
inet6 fe80::a319:8c13:9238:5bb6 prefixlen 64 scopeid 0x20<link>
inet6 fe80::3077:9382:f10c:ae23 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:9a:b0:68 txqueuelen 1000 (Ethernet)
RX packets 80416 bytes 76841088 (73.2 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 47140 bytes 15283547 (14.5 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth0:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.10 netmask 255.0.0.0 broadcast 10.255.255.255
ether 00:0c:29:9a:b0:68 txqueuelen 1000 (Ethernet)
基于多端口的虚拟主机

[root@web02 conf.d]
[root@web02 conf.d]
server{
listen 8082;
server_name 10.0.0.8;
root /opt/wc;
index index.html
}
[root@web02 conf.d]
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web02 ~]
[root@web02 ~]
[root@web02 wc]
ewewqe
[root@web02 wc]
基于多域名的虚拟主机

[root@web02 conf.d]
[root@web01 ~]
server{
listen 80;
server_name www.wc1.com;
root /opt/wc1;
index index.html;
}
[root@web01 conf.d]
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 ~]
[root@web01 ~]
[root@web01 wc1]
opopopooop
[root@web01 wc]
1.按win+r打开运行

2.输入:drivers

3.进入etc目录

4.使用notepad++打开hosts文件

5.添加域名解析

匹配符 |
匹配规则 |
优先级 |
- |
精确匹配 |
1 |
^~ |
以某个字符开头 |
2 |
~ |
区分大小写的正则匹配 |
3 |
~* |
不区分大小写的匹配 |
4 |
!~ |
区分大小写的不匹配的正则 |
5 |
!~* |
不区分大小写不匹配的正则 |
6 |
/ |
通用匹配,任何匹配都会匹配 |
7 |
应用场景
location / {
...
}
location ~ \.php$ {
...
}
location ~ \.jsp$ {
...
}
location ~* .*\.(jpg|gif|png|js|css)$ {
...
}
location ~* \.(jpg|gif|png|js|css)$ {
...
}
location ~* "\.(sql|bak|tgz|tar.gz|.git)$"
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了