第十一周-day46-HTTP协议补充与Nginx网站服务

16952149-0cd36aafe1759004.png
第十一周-day45-HTTP协议补充与Nginx服务.png

1. URL和URI

URL 统一资源定位符
URI 统一资源标识符,范围更广


16952149-cfc794bc9dc8e04b.png
16952149-ea40d9a5e1894dc8.png

16952149-c2f2574e22ca151a.png

2.状态码

老男孩老师博客的状态码总结:https://blog.51cto.com/oldboy/716294

16952149-7f931db294e92fc8.png
16952149-ef069ee6ba897f39.png

3.静态网页和动态网页

静态网页

静态页面
文字信息 图片 视频
网站只进行请求与响应
客户端浏览器进行处理(解析)
静态:web服务器自己能处理: nginx
搜索引擎容易收入

16952149-c2bed4306d7e4f87.png
16952149-685eef6d5399fbf2.png

动态页面

交互信息 评论 上传
URL包含 & 符号 一半是动态页面
需要服务器进行处理 把处理好的结果发给客户端
动态:nginx+php/java
搜索引擎不容易收入

16952149-6cc8cc2fabc7f0e9.png

4.IP PV UV 衡量网站访问量的单位

衡量网站访问量的单位
IP 独立ip数量
PV 页面访问量
UV 独立的访客数,有多少人访问



5.※※Nginx※※

安装软件:

yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake wget httpd-tools vim tree

功能
1.web服务器 (http请求 http响应)
2.nginx负载均衡
3.nginx缓存

Nginx网站服务软件功能作用

支持网站页面请求处理功能
支持反向代理负载均衡功能
支持前端业务数据缓存功能

16952149-b5fc5f26a5200bae.png
Nginx网站服务软件优势特点(与apache对比)

apache 网站服务处理用户请求方法:
select 模型 便利方式处理用户请求

nginx 网站服务处理用户请求方法:
epoll 模型 精准方式处理用户请求

***************************************************************

1.基于官方源进行nginx安装

官网:http://nginx.org/en/download.html

16952149-b14d6d754f044e77.png

2.配置官方源:

[10:35 root@web01 ~]# vim /etc/yum.repos.d/nginx.repo 
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
16952149-c044a9a032b27a2b.png

3.检查是否配置成功:

[10:41 root@web01 ~]# yum list nginx
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Installed Packages
nginx.x86_64            1:1.16.0-1.el7.ngx             @nginx-stabl

4.查看软件包内容

[10:42 root@web01 ~]# rpm -ql nginx
/etc/logrotate.d/nginx
/etc/nginx
/etc/nginx/conf.d
/etc/nginx/conf.d/default.conf
/etc/nginx/fastcgi_params
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/modules
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
/etc/nginx/win-utf
/etc/sysconfig/nginx   #systemctl 管理nginx的使用的文件
/usr/lib/systemd/system/nginx.service  #systemctl 管理nginx(开 关 重启 reload) 配置文件
/usr/sbin/nginx  #nginx命令
/usr/sbin/nginx-debug  #调试
/usr/share/nginx
/usr/share/nginx/html  #站点目录 网站的根目录  www.linuxcx.cn/lcx.jpg
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
/var/cache/nginx
/var/log/nginx  #nginx日志

5.查看nginx版本方法

[10:49 root@web01 ~]# nginx -v  \\小v 查看版本
nginx version: nginx/1.16.0
[10:49 root@web01 ~]# nginx -V  \\查看编译安装的命令
nginx version: nginx/1.16.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --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'

6.启动nginx服务并查看进程

[10:51 root@web01 ~]# systemctl start nginx
[10:51 root@web01 ~]# ps -ef|grep nginx
root       8598      1  0 00:53 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      8599   8598  0 00:53 ?        00:00:00 nginx: worker process
root      10711  10118  0 10:51 pts/0    00:00:00 grep --color=auto nginx

7.浏览器搜索ip地址

16952149-51fcce1043c47de4.png

8. 开启 重启 关闭 nginx服务

systemctl start nginx ===== nginx 开启

systemctl reload nginx ===== nginx -s reload 平滑重启

systemctl stop nginx ===== ngnx -s stop 关闭

检查语法

nginx -t

[11:15 root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

9.nginx常用命令

nginx -h

[17:50 root@web01 ~]# nginx -h
nginx version: nginx/1.16.0
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /etc/nginx/)
  -c filename   : set configuration file (default: /etc/nginx/nginx.conf)
  -g directives : set global directives out of configuration file

10.服务配置文件

vim /etc/nginx/nginx.conf
[11:18 root@web01 ~]# vim /etc/nginx/nginx.conf 
user  nginx;  \\指定nginx进程属于用户nginx
worker_processes  1;    \\worker进程数量 所有核心数或x2
error_log  /var/log/nginx/error.log warn;   \\指定错误日志 warn日志格式 只显示警告信息
pid        /var/run/nginx.pid;  \\pid进程文件
events {    \\events模块(区域)
    worker_connections  1024;   \\每个进程数最大连接数量
}
http {  \\#http区域#
    include       /etc/nginx/mime.types;    \\媒体类型http协议中的文件类型 include(把其他位置的文件加入到这个位置)
    default_type  application/octet-stream; 
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '   \\定义了nginx访问日志的格式
                      '$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;
    keepalive_timeout  65;  \\超时时间
    #gzip  on;
    include /etc/nginx/conf.d/*.conf;   \\#指定的服务配置文件(接下文)
}

11.服务配置文件中的server区域

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

搭建1个网站 虚拟主机
默认每一行都有";"结尾

[11:36 root@web01 ~]# egrep -v '^$|#' /etc/nginx/conf.d/default.conf 
server {
    listen       80;    \\指定监听端口
    server_name  localhost; \\域名 www.linuxcx.cn
    location / {
        root   /usr/share/nginx/html;   \\指定站点目录
        index  index.html index.htm;    \\首页文件(默认展示的文件)
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

12.官网查询帮助

模块格式 官网查询模块格式

16952149-bb843b2916e6f38c.png
16952149-512f85d2dd7e4cc7.png
16952149-e57aa684f418b5a5.png

13.给www.oldboy.com 这个网址配置hosts解析

在/etc/nginx/nginx.conf 的最下面 include文件类型下添加server区域,将include注释
添加域名 www.oldboy.com
[ root@web01 ~]# vim /etc/nginx/nginx.conf 
.....
#    include /etc/nginx/conf.d/*.conf;
     server   {
     listen       80;
     server_name  www.oldboy.com;

     location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
                }
              }
}
www.oldboy.com添加/etc/hosts本地域名解析
[ root@web01 ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.local
domain4
::1         localhost localhost.localdomain localhost6 localhost6.local
domain6
172.16.1.5      lb01
172.16.1.6      lb02
172.16.1.7      web01 www.oldboy.com  \\# 此虚拟机ip为10.0.0.7
172.16.1.8      web02
172.16.1.31     nfs01
172.16.1.41     backup
172.16.1.51     db01 db01.etiantian.org
172.16.1.61     m01

windows本地域名解析文件路径:

如果经常用到最后创建一个快捷方式到桌面
16952149-a0561dee02cc943d.png
16952149-05af9f9ba27f8ecc.png
用nopated++打开hosts文件添加域名解析:
16952149-d62d51602ea93678.png

检查解析是否成功

[root@web01 /etc/nginx]# ping www.oldboy.com 
PING web01 (172.16.1.7) 56(84) bytes of data.
64 bytes from web01 (172.16.1.7): icmp_seq=1 ttl=64 time=0.036 ms
64 bytes from web01 (172.16.1.7): icmp_seq=2 ttl=64 time=0.055 ms
^C
--- web01 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.036/0.045/0.055/0.011 ms

给站点目录的主页写入此网址
[root@web01 /etc/nginx]# echo www.oldboy.com  >/usr/share/nginx/html/index.html 
[root@web01 /etc/nginx]# 
[root@web01 /etc/nginx]# curl  www.oldboy.com
www.oldboy.com  

windows环境下可以解析了:

16952149-ace131628277ac6f.png

14.在上面进入网站的时用Wireshark抓包:

查询http协议 与 tcp协议

三次握手 ---》请求连接 ---》响应连接 ---》断开连接

网站的小图标如果没有的话,会报一个404
16952149-bb359e76740f1260.png

16952149-c5e39d51e9db6805.png

猜你喜欢

转载自blog.csdn.net/weixin_33694620/article/details/90780861
今日推荐