web架构1------(nginx的安装和基础配置)

一.前言

本期来和大家介绍一下web架构,主要是和大家介绍一下nginx,当然看这个之前最好把前面的linux基础给看一遍,当然介绍这个之前还是有网络相关的知识没有和大家讲,但是我看来网络的话大家理解个基本就好了,主要还是得先会用,再去理解其原理。

大家如果看过linux基础专栏里的基础11,当时给大家安装了一个httpd那个也就是apache(阿帕奇),但是今天说的nginx比他功能强大,但是功能都类似,学会了这个也就同时学会了httpd。

当然像是http协议呀,网络三要素之类的,大家可以去看我前面python全栈讲的socket编程

python之网络编程(socket)_python socket-CSDN博客文章浏览阅读4.8k次,点赞18次,收藏51次。今天我们就讲完这么多了,本来想和大家讲一个文件的上传和下载的,但是发现讲不完根本讲不完,大家可以自己练习试试。_python socket https://yystar.blog.csdn.net/article/details/141186919

二.nginx安装

nginx是俄罗斯人在2002年开发的,这个占用内存资源少。

# 可以改一下主机名,方便我们记忆:hostnamectl set-hostname web01

安装方法1:

使用epel源安装

# yum repolist #查看当前系统的yum仓库有哪些软件包

yum install epel-release -y # 安装yum的扩展包

yum install nginx -y

systemctl start nginx.service

systemctl enable nginx.service

# netstat -lntup # 查看端口占用情况

# 可以看到nginx默认占用了80端口

# 安装完之后,如果我们继续安装一个apache的httpd,还是可以安装上的

# yum install httpd -y

# 但是当我们启动httpd的时候会报错

# systemctl start httpd

# 查看它的状态

# systemctl status httpd # 可以看到启动失败了

[root@web01 ~]# systemctl status httpd

● httpd.service - The Apache HTTP Server

        Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)

        Active: failed (Result: exit-code) since 二 2023-04-11 20:20:47 CST; 17s ago Docs: man:httpd(8) 

        man:apachectl(8)

        Process: 1819 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=1/FAILURE)

        Main PID: 1819 (code=exited, status=1/FAILURE)

4月 11 20:20:28 web01 systemd[1]: Starting The Apache HTTP Server...

# 不能监听80端口,因为已经被80端口给占用了。所以一旦80端口被某个其他服务占用了,那么我们就不能 以80端口来启动其他的服务,所以想其他其他的服务,就要改服务默认的端口号

4月 11 20:20:47 web01 httpd[1819]: AH00558: httpd: Could not reliably determine the server's fully ...ssage

4月 11 20:20:47 web01 httpd[1819]: (98)Address already in use: AH00072: make_sock: could not bind t...:]:80

4月 11 20:20:47 web01 httpd[1819]: (98)Address already in use: AH00072: make_sock: could not bind t....0:80

4月 11 20:20:47 web01 httpd[1819]: no listening sockets available, shutting down

4月 11 20:20:47 web01 httpd[1819]: AH00015: Unable to open logs

4月 11 20:20:47 web01 systemd[1]: httpd.service: main process exited, code=exited, status=1/FAILURE

4月 11 20:20:47 web01 systemd[1]: Failed to start The Apache HTTP Server.

4月 11 20:20:47 web01 systemd[1]: Unit httpd.service entered failed state.

4月 11 20:20:47 web01 systemd[1]: httpd.service failed. Hint: Some lines were ellipsized, use -l to show in full. 

接下来看一下nginx的一些配置,nginx所有的修改都需要修改配置文件来进行控制。

三.nginx配置 

修改配置之前,一般都需要我们备份一下配置文件,以防改错了,那么nginx也帮你考虑到了,所以提前 给我们准备了一个备份文件,如下

[root@web01 ~]# ll /etc/nginx/

总用量 68

drwxr-xr-x 2 root root 6 11月 11 00:58 conf.d

drwxr-xr-x 2 root root 6 11月 11 00:58 default.d

-rw-r--r-- 1 root root 1077 11月 11 00:58 fastcgi.conf

-rw-r--r-- 1 root root 1077 11月 11 00:58 fastcgi.conf.default # fastcgi.conf的备份文件

-rw-r--r-- 1 root root 1007 11月 11 00:58 fastcgi_params 

-rw-r--r-- 1 root root 1007 11月 11 00:58 fastcgi_params.default # fastcgi_params的备份文件

-rw-r--r-- 1 root root 2837 11月 11 00:58 koi-utf

-rw-r--r-- 1 root root 2223 11月 11 00:58 koi-win

-rw-r--r-- 1 root root 5231 11月 11 00:58 mime.types

-rw-r--r-- 1 root root 5231 11月 11 00:58 mime.types.default # mime.types的备份文件

-rw-r--r-- 1 root root 2336 11月 11 00:58 nginx.conf # nginx的主配置文件,nginx每次 启动都会加载它。

-rw-r--r-- 1 root root 2656 11月 11 00:58 nginx.conf.default # nginx.conf的备份文件

-rw-r--r-- 1 root root 636 11月 11 00:58 scgi_params

-rw-r--r-- 1 root root 636 11月 11 00:58 scgi_params.default # scgi_params的备份 文件

-rw-r--r-- 1 root root 664 11月 11 00:58 uwsgi_params

-rw-r--r-- 1 root root 664 11月 11 00:58 uwsgi_params.default # uwsgi_params的备 份文件

-rw-r--r-- 1 root root 3610 11月 11 00:58 win-utf

所以其实我们不用自行进行备份了。

# 先过滤一下配置文件,因为里面的#号,空行等太多了,带#号的都是注释不用的,所以可以去掉

cd /etc/

nginx/ grep -Ev '#|^$' nginx.conf.default > nginx.conf

# 编辑配置文件

vim nginx.conf

#删除17-20行,剩下的就是最小配置了

# 注意配置文件的语法格式,每行结尾必须是英文的分号。

worker_processes 1; #启动nginx时工作进程的数量,可以加大这个数字来提高nginx的处理请求的 效率,但是这个数字也不能太大,因为进程是消耗系统内存资源的。调整一下这个数字,然后通过free指令可 以查看一下内存容量的变化。建议和CPU核数一致就行

# worker_processes 2; # 改完配置文件都需要重启nginx才生效,systemctl restart nginx.service

events {

        worker_connections 1024; #连接数量,每个进程可以处理1024连接

}

http {                                 #http模块

        include         mime.types;         #include是包含的意思,这行的意思是,nginx启动的时候加 载nginx.conf主配置文件的时候,加载到这一行的时候,先包含加载一下mime.types文件里面的配置,这 个文件主要是用来标识支持哪些多媒体格式,这个文件在nginx.conf所在目录

        default_type         application/octet-stream; #如果不能识别的文件,那么默认以八进制数据流 的方式来打开文件

        # 下面这两个配置也可以删掉,现在不太适合讲,后面再讲

        # sendfile on;

        # keepalive_timeout 65;

        charset utf-8;                 #设置字符集,这是我多加的一个配置,默认没 有,比如vim jaden.html写入一些中文,去掉和加上这个配置看看效果

        server {         #一个网站,一个nginx可以运行多个网站,添加 这个配置项即可

                listen         80;         #监听端口,可以修改,比如改个81看看效果,再 启动apache看看80效果

                server_name         localhost; #网站的域名,现在没有配置域名,默认就是localhost,比如后面可以配置www.xxx.com

                location / {                 #目录,必须要有个location 

                        root         html; #root是站点根目录的意思,值为html表示一个 相对路径,绝对路径是/usr/share/nginx/html,也可以改昂,比如改成绝对路径root

/usr/share/nginx/html,或者改为其他路径root /web

                        #root /web # 改为这个试一下,别忘了去根目录下创建一个web目录,给web目录一些文件 看看效果,比如之前那个游戏网站,http://192.168.61.139:81/game/,还可以直接把游戏的目录内容 直接拷贝到/web目录中,就不用每次访问都/game/了

                        # [root@web01 web]# mv yiliao/* .

                        # 在访问:http://192.168.61.139:81/

                        index index.html index.htm; #默认首页,访问网址根路径的时候,自动访问站 点根目录下面的index或者index.html或者index.htm文件,如果没有这几个名字的文件呢?访问的时候就 会提示403,需要在网址上手动指定文件名称,这几个文件名称也是可以改的,比如改为xxx.html。

                }
        }

# 第二个网站:

#server {

#         listen         81;

#         server_name         localhost;

#         location / {

#                 root         html;

#                 index         index.html         index.htm;

#         }

#}

}

# 修改完配置文件之后,可以检查一下配置文件的语法是否ok

[root@web01 nginx]# nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

# 百度是php语言开发的:下面两个方式访问效果一样,baidu的首页其实是index.php

# https://www.baidu.com/

# https://www.baidu.com/index.php

查看nginx进程

[root@web01 nginx]# ps -ef | grep nginx

root         1725         1         0         20:16 ?         00:00:00         nginx: master process /usr/sbin/nginx # master process是主进程的意思,也叫做管理进程,它是用来管理nginx整个运行 的,nginx的其他子进程如果死掉了,它会自动在启动其他的子进程,比如尝试kill 下面的子进程,你会发 现另外一个进程又自动启动了。真正干活的进程是下面的worker process进程,叫做工作进程,有请求来了 都是它处理的

nginx 1727 1725 0 20:16 ? 00:00:00 nginx: worker process # 可以看到nginx是以nginx用户身份启动的

root 1869 1552 0 20:35 pts/0 00:00:00 vim nginx.conf

root 1898 1872 0 20:35 pts/1 00:00:00 grep --color=auto nginx 

查看mime.types,nginx支持的多媒体类型文件

vim mime.types 

默认nginx的站点根目录(存放网站代码的目录,也叫做网站的物理路径、真实路径等)在如下位置

[root@web01 nginx]# cd /usr/share/nginx/html/

[root@web01 nginx]# ls

404.html 50x.html en-US icons img index.html nginx-logo.png poweredby.png 

我们通过浏览器网址访问nginx启动的网站页面时,默认nginx都会在这个目录中去寻找用户访问的页面 对应的HTML文件。

http://192.168.61.139/ --- index.html

http://192.168.61.139/img/centos-logo.png --- img/centos-logo.png 

比如我们放一个mp4格式的文件上来,直接访问文件名称就可以播放

http://192.168.61.139/jaden.mp4 # 这是因为nginx的配置文件mime.types中看到,支持mp4, 如果删掉了配置文件中mp4那一行数据,那么就不支持mp4格式文件的预览了,也就是不能直接在浏览器上播放mp4视频了,而是直接下载,但是现在浏览器做的功能比较强大了,有些浏览器也会自动帮我们播放mp4的视 频。如果是nginx不能识别的文件格式、并且浏览器也不能识别这种文件格式,比如xx.jaden,那么访问一 下会直接下载这个文件。

#再比如,如果是nginx支持,但是浏览器不支持的文件格式,比如htc格式的文件,nginx能识别,但是浏览 器不支持,那么浏览器会尝试用txt的方式来打开这个文件,如果是一些视频或者图片,那么就在浏览器上看 到一堆乱码。 如果nginx和浏览器都不支持的,基本就会看到直接下载。 

如果我们删除了 /usr/share/nginx/html/ 目录中的全部文件和目录,如下

[root@web01 nginx]# cd /usr/share/nginx/html/

[root@web01 html]# rm -rf * 

访问一下,怎么还能看到这个页面,这是浏览器做的缓存,将你的页面已经缓存到你自己电脑本地了, 一访问还是访问的本地缓存的页面

可以多刷新几次,或者ctrl+f5强制刷新访问,也就是让浏览器不要访问缓存,直接去拿最新的页面,但 是什么页面都没有了,效果如下,看到403数字和英文单词Forbidden,不允许访问的意思。

 

四.总结 

本期主要是讲解了nginx的基础配置,大家跟着这个安装一下就好了,这个主要是我们项目部署到服务器要用到,后续还会继续更新,期待大家的点赞关注加收藏 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值