Web基础入门

Nginx概述

Nginx是一个开源且高性能、可靠的Http Web服务、代理服务。

开源: 直接获取源代码高性能: 支持海量并发可靠: 服务稳定

Nginx应用场景

架构(day10)_nginx

nginx快速安装

  • rpm安装(yum安装)
  • epel仓库(阿里云)
  • 官方仓库
  • 源码安装

使用官方源

Nginx官方网站:​​TP​

架构(day10)_nginx_02

架构(day10)_nginx_03

架构(day10)_html_04

# 1.添加nginx官方源
[root@web01 ~]# vim /etc/yum.repos.d/nginx.repo

[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

# 2.安装nginx
[root@web01 ~]# yum install -y nginx

# 3.启动nginx并加入开机自启
[root@web01 ~]# systemctl start nginx
[root@web01 ~]# systemctl enable nginx

# 4.查看nginx的版本
[root@web01 ~]# nginx -v
nginx version: nginx/1.22.0

# 5.查看nginx的版本和源码安装生成步骤的参数有哪些
[root@web01 ~]# nginx -V

--prefix=/application/nginx-1.22.0 --user=nginx --group=nginx --with-compat --with-file-aio
--with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module
--with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-
D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -
grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -
pie'

nginx的启停

# 1.启动
systemctl start nginx
nginx
/app/nginx/sbin/nginx

# 2.停止
systemctl stop nginx
nginx -s stop
/app/nginx/sbin/nginx -s stop

# 3.重新加载
systemctl reload nginx
nginx -s reload
/app/nginx/sbin/nginx -s reload

-c:指定配置文件的路径
-t:检查配置文件的语法(无法检测单词拼写)
-s:启停重载,服务操作
-v:查看版本号
-V:查看版本和编译参数

## nginx启动脚本systemd管理
[root@web01 ~]# vim /usr/lib/systemd/system/nginx.service
[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=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /var/run/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /var/run/nginx.pid)"
[Install]
WantedBy=multi-user.target


## 源码安装,使用fpm打包,脚本内容:
# 先写脚本
[root@web01 ~]# vim post_install_nginx.sh
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配置文件

1.Nginx主配置文件

路径

类型

作用

/etc/nginx/nginx.conf

配置文件

nginx主配置文件

/etc/nginx/conf.d/default.conf

配置文件

nginx网站示例配置文件

2.Nginx代理相关参数文件

路径

类型

作用

/etc/nginx/fastcgi_params

配置文件

Fastcgi代理配置文件

/etc/nginx/scgi_params

配置文件

scgi代理配置文件

/etc/nginx/uwsgi_params

配置文件

uwsgi代理配置文件

3.Nginx编码相关配置文件

路径

类型

作用

/etc/nginx/win-utf

配置文件

Nginx编码转换映射文件

/etc/nginx/koi-utf

配置文件

Nginx编码转换映射文件

/etc/nginx/koi-win

配置文件

Nginx编码转换映射文件

/etc/nginx/mime.types

配置文件

Content-Type与扩展名

4.Nginx管理相关命令

路径

类型

作用

/usr/sbin/nginx

命令

Nginx命令行管理终端工具

/usr/sbin/nginx-debug

命令

Nginx命令行与终端调试工具

5.Nginx日志相关目录与文件

路径

类型

作用

/var/log/nginx

目录

Nginx默认存放日志目录

/etc/logrotate.d/nginx

配置文件

Nginx默认的日志切割

nginx配置文件详解

## nginx主配置文件
[root@web02 nginx]# grep -Ev '^$|#' /etc/nginx/nginx.conf
## 注意:nginx配置文件,每一行,都';'结尾

[root@web01 nginx]# vim /etc/nginx/nginx.conf

## 核心层(核心模块)、全局配置
# nginx启动用户配置
user nginx;
# nginx工作线程数量(cpu亲和)
worker_processes auto;(auto 自动根据cpu的核心数来启动对应的工作进程数)
# 错误日志 日志路径 日志级别
error_log /var/log/nginx/error.log notice;
# 程序启动进程号(pid号)存放的路径
pid /var/run/nginx.pid;


## 事件层(事件模块)
events {
# 一个worker进程的最大连接数
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;
#tcp_nopush on;

## 长链接,超时时间 65s
keepalive_timeout 65;

## 数据传输过程中,使用gzip压缩
#gzip on;

## 包含 nginx其他子配置文件(网站虚拟主机配置文件server)
include /etc/nginx/conf.d/*.conf;
}

## 一定有同学,写虚拟主机配置文件,不加.conf


## 日志格式
$remote_addr:远端的IP(上一个节点的IP)
$remote_user:登录的用户
[$time_local]:时间
"$request":请求方式、请求uri、HTTP协议版本号
$status:状态码
$body_bytes_sent:流量
"$http_referer":跳转地址(从哪个网站跳转过来的)
"$http_user_agent":客户端信息
"$http_x_forwarded_for":记录透传IP地址(获取用户的真实IP)


## 虚拟主机配置文件
server {
## 该网站的监听端口
listen 80;
## 该网站的主机IP或域名
server_name localhost;
## 该网站的日志路径及日志格式
#access_log /var/log/nginx/host.access.log main;

## uri跳转
location / {
## 站点目录
root /usr/share/nginx/html;
## 默认首页,索引页面
index index.html index.htm;
}
}

server层是网站配置层,包含在http层中
http{
server{
location /{
location /xxx{}
}
location /api{}
location /wjh{}
}
server{
}
...
}



http{
access_log /var/log/nginx/access.log main;

server{
listen 80;
server_name www.baidu.com;
...
access_log /var/log/nginx/www.baidu.com.access.log main;
}
server{
listen 80;
server_name zhidao.baidu.com;
...
access_log /var/log/nginx/zhidao.baidu.com.access.log main;
}
server{
listen 80;
server_name map.baidu.com;
...
access_log /var/log/nginx/map.baidu.com.access.log main;
}
server{
listen 80;
server_name fanyi.baidu.com;
...
access_log /var/log/nginx/fanyi.baidu.com.access.log main;
}
}

多虚拟主机(多web网站配置)

在企业中,是不可能用一个nginx对应一套业务,多个网站都在一个nginx中配置

架构(day10)_配置文件_05

基于IP的多虚拟主机

架构(day10)_nginx_06

[root@web01 conf.d]# ifconfig eth0:0 10.0.0.10
[root@web01 conf.d]# ifconfig eth0:1 10.0.0.11
[root@web01 conf.d]# cat 1_game.conf
server{
listen 80;
server_name 10.0.0.10;

location /{
root /game/h5_games;
index index.html;
}
}

[root@web01 conf.d]# cat 1_wjh.conf
server{
listen 80;
server_name 10.0.0.7;
root /code;

location /{
index index.html;
}
location /wjh{
index index_1.html huanglong.html;
}
}
[root@web01 conf.d]# ifconfig
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)

基于多端口的虚拟主机

架构(day10)_配置文件_07

[root@web01 conf.d]# cat game.conf
server{
listen 8081;
server_name 10.0.0.7;

location /{
root /game/h5_games;
index index.html;
}
}
[root@web01 conf.d]# cat wjh.conf
server{
listen 8082;
server_name 10.0.0.7;
root /code;

location /{
index index.html;
}

location /wjh{
index index_1.html huanglong.html;
}
}

基于多域名的虚拟主机

架构(day10)_html_08

[root@web01 conf.d]# cat blog.wjh.com.conf
server{
listen 80;
server_name blog.wjh.com;
root /wjh1;
index index.html;
}
[root@web01 conf.d]# cat www.wjh.com.conf
server{
listen 80;
server_name www.wjh.com;
root /wjh;
index index.html;
}

在windows系统中,配置本地的DNS

1.按win+r打开运行

架构(day10)_html_09

2.输入:drivers

架构(day10)_配置文件_10

3.进入与etc目录

架构(day10)_html_11

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

架构(day10)_配置文件_12

5.添加域名解析

架构(day10)_html_13

架构(day10)_nginx_14