http协议与nginx

动态页面与静态页面的差别:

(1)URL不同
静态⻚⾯链接⾥没有“?”
动态⻚⾯链接⾥包含“?”
(2)后缀不同 (开发语⾔不同)
静态⻚⾯⼀般以 .html .htm .xml 为后缀
动态⻚⾯⼀般以 .php .jsp .py等为后缀
(3)内容不同
静态⻚⾯的内容是固定的
动态⻚⾯的内容会因⽤户、浏览器、时间、地点等⽽发⽣变化。

静态资源:根据开发者保存在项目资源目录中的路径访问静态的资源,包括html 图片 js  css 音乐  视频

http协议

http原理:

HTTP是⼀个基于TCP/IP通信协议来传递数据的协议,传输的数 据类型为HTML ⽂件,图⽚⽂件,查询结果等。
HTTP协议⼀般⽤于B/S架构。浏览器作为HTTP客户端通过URL 向HTTP服务端即web服务器发送所有请求,web服务器收到客 户端请求后进⾏响应。

http状态码:

2xx:成功,200成功、201已经创建
3xx:重定向,304未修改
4xx:请求错误,404未找到⽂件、408请求超时
5xx:服务器错,500服务器内部错误、502⽹关错误

[root@git ]# yum -y install httpd
[root@git html]# echo "我是静态的html文件" > index.html

[root@git html]# dd if=/dev/zero of=/var/www/html/a.txt bs=30M count=1

apache

最早的 web 服务程序,基于 http 协议提供⽹⻚浏览服务
模块化设置、开放源代码、跨平台应⽤、⽀持多种 web 编程语 ⾔、运⾏稳定。

搭建apache服务器:

查看华为云主机的所有的打开的端口

firewall-cmd --list-ports

停用防火墙或打开指定端口

firewall-cmd  --zone=public --add-port=80/tcp --permanent

firewall-cmd  --reload   启动端口

firewall-cmd  --zone=public --add-service=http --permanent     防⽕墙开放 http服务

下载http,并执行以下操作:

touch /var/www/html/index.html

mkdir /var/www/html/img/

cp 300.3.png /var/www/html/img

vim /var/www/html/index.html

浏览器测试:

nginx

源码编译安装nginx

注意:html目录中的文件发生修改之后,不需要重启nginx服务

1.下载源码包

在nginx.org中复制nginx-1.26.1.tar.gz包的地址,在虚拟机中下载:

 wget https://nginx.org/download/nginx-1.26.1.tar.gz

2.解压

tar -zxvf nginx-1.26.1.tar.gz

3.下载nginx所需要的依赖包 gcc gcc-c++  openssl-devel   pcre-devel

yum -y install gcc gcc-c++ openssl-devel pcre-devel

4.编译安装nginx

 ./configure --prefix=/usr/local/nginx/ --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-stream

make && make install

5.nginx的启动

1)nginx的启动与关闭:

2)创建脚本来启动nginx服务:

[root@slave nginx]# vim ~/nginx.sh

3)守护进程:以systemctl控制nginx

如果直接使用sbin目录下的nginx,就无法使用systemctl

在 /usr/lib/systemd/system ⽬录下新建⼀个 nginx.service ⾮⼿ 动执⾏脚本,并使⽤ vim 命令添加以下内容

  170  vim /usr/lib/systemd/system/nginx.service

[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=Flase

[Install]
WantedBy=multi-user.target

 

  171  systemctl daemon-reload
  172  systemctl stop nginx
  173  systemctl restart nginx
  174  reboot
  175  systemctl start nginx.service 
 

4)创建软连接:直接使用nginx命令

将软件⽬录下的 /usr/local/nginx/sbin/nginx 可执⾏程序软链接到/usr/sbin,如果按照 Windows ⽅式来说相当于制作了⼀个 nginx 的快捷⽅式

之所以指令能在命令行使用,是因为在$PATH目录中能找到这个可执行文件或者是可执行文件的链接文件

[root@slave nginx]# nginx
-bash: nginx: 未找到命令
[root@slave nginx]# $PATH 
-bash: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin: 没有那个文件或目录

[root@slave nginx]# ln -s  /usr/local/nginx/sbin/nginx   /usr/bin/
[root@slave nginx]# ls -l /usr/bin/nginx
lrwxrwxrwx. 1 root root 27 7月  29 16:15 /usr/bin/nginx -> /usr/local/nginx/sbin/nginx
[root@slave nginx]# nginx -s stop 
[root@slave nginx]# netstat -lnput | grep nginx
[root@slave nginx]# nginx
[root@slave nginx]# netstat -lnput | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      8232/nginx: master  

 

添加监控块:

修改配置文件,找到“server { }”,添加⼀个“location { }”,也就是虚拟主机

[root@slave ~]# vim /usr/local/nginx/conf/nginx.conf


[root@slave ~]# systemctl reload nginx.service 

配置多个虚拟主机:

⼀个“location”相当于⼀个虚拟主机,也就是⽤户访问⽹站时, 点击跳转的另⼀个⻚⾯。

location 内可以添加 nginx 各种功能模块


        location / {
            root   html;
            index  index.html index.htm;
        }

nginx反向代理配置

建立后端服务器:

  206  scp root@192.168.118.54:~/nginx-1.26.1.tar.gz ./
  207  tar -zxvf nginx-1.26.1.tar.gz 
  209  cd nginx-1.26.1/

  210  yum -y install gcc gcc-c++ openssl-devel  pcre-devel

  219   ./configure --prefix=/usr/local/nginx/ --user=nginx --group=nginx --with-http_ssl_module --               with-http_stub_status_module --with-http_realip_module --with-stream
  220  make 
  221  make install
  222  useradd -s /bin/nologin -M nginx
  223  echo "我是后端服务器" > /usr/local/nginx/html/index.html

  227  firewall-cmd --zone=public --add-port=80/tcp --permanent
  228  /usr/local/nginx/sbin/nginx
  234  firewall-cmd --reload


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值