day46-http协议补充-nginx基础

安装nginx

1.nginx依赖包

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

2.配置nginx官方yum源

[root@web ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

3.检查yum源

出现nginx表示成功

[root@web01 ~]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
nginx                                      | 2.9 kB     00:00     
nginx/x86_64/primary_db                      |  46 kB   00:01     
repo id          repo name                                  status
base/7/x86_64    CentOS-7 - Base - mirrors.aliyun.com       10,019
epel/x86_64      Extra Packages for Enterprise Linux 7 - x8 13,221
extras/7/x86_64  CentOS-7 - Extras - mirrors.aliyun.com        409
nginx/x86_64     nginx repo                                    152
updates/7/x86_64 CentOS-7 - Updates - mirrors.aliyun.com     1,982
repolist: 25,783

检查nginx版本

[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
Available Packages
nginx.x86_64               1:1.16.0-1.el7.ngx                nginx
[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'
[root@web01 ~]# nginx -v
nginx version: nginx/1.16.0

启动nginx

[root@web01 ~]# systemctl start nginx

类似的开启 关闭 平滑重启命令
systemctl start nginx ===== nginx
systemctl reload nginx ===== nginx -s reload
systemctl stop nginx ===== nginx -s stop

检查nginx进程

有nginx了说明成功

[root@web01 ~]# ps -ef|grep nginx
root       9161      1  0 10:51 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      9162   9161  0 10:51 ?        00:00:00 nginx: worker process
root       9202   7641  0 10:52 pts/0    00:00:00 grep --color=auto nginx

nginx -t #检查语法

修改完nginx的配置文件一定要先检查语法再启动

[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

检查之后再启动

[root@web01 ~]# systemctl start nginx 
[root@web01 ~]# ps -ef |grep nginx 
root       8512      1  0 11:15 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      8513   8512  0 11:15 ?        00:00:00 nginx: worker process
root       8516   7482  0 11:15 pts/0    00:00:00 grep --color=auto nginx

检查nginx端口

[root@web01 ~]# ss -lntup |grep nginx 
tcp    LISTEN     0      128       *:80                    *:*                   users:(("nginx",pid=8399,fd=6),("nginx",pid=8398,fd=6))  

c6中启动nginx的方法

[root@web01 ~]# nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()

nginx配置相关文件

[root@web01 ~]# rpm -ql nginx 
/etc/logrotate.d/nginx                     #nginx日志切割的配置文件
/etc/nginx
        /etc/nginx/nginx.conf              #nginx主配置文件 
        /etc/nginx/conf.d
        /etc/nginx/conf.d/default.conf     #nginx.conf一样 
        /etc/nginx/mime.types              #媒体类型 (http协议中的文件类型)
/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/html                      #站点目录 网站的根目录 www.oldboyedu.com/oldboy.jpg 
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html    ##nginx默认站点目录
/var/cache/nginx
/var/log/nginx                             #nginx日志 access.log 访问日志

nginx配置文件的每列含义

user  nginx;     ##nginx进程使用的用户
worker_processes  1;   ##worder进程数量 所有cpu核心数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协议中的文件类型
    default_type  application/octet-stream;    ###定义了nginx访问日志的格式  

    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;   ##开启高效传输模

    keepalive_timeout  65;    ##超时时间

    #gzip  on;

   # 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 的hosts解析

[root@web01 /etc/nginx]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.1.5      lb01
172.16.1.6      lb02
172.16.1.7      web01 www.oldboy.com      #配置www.oldboy.com 的hosts解析
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

检查解析是否成功

[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

往首页文件追加内容

[root@web01 /etc/nginx]# echo www.oldboy.com  >/usr/share/nginx/html/index.html 

解析成功

[root@web01 /etc/nginx]# curl  www.oldboy.com
www.oldboy.com

检查http报文

[root@web01 /etc/nginx]# curl -v  www.oldboy.com
* About to connect() to www.oldboy.com port 80 (#0)
*   Trying 172.16.1.7...
* Connected to www.oldboy.com (172.16.1.7) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: www.oldboy.com
> Accept: */*
> 
< HTTP/1.1 200 OK
< Server: nginx/1.16.0
< Date: Tue, 04 Jun 2019 04:09:23 GMT
< Content-Type: text/html
< Content-Length: 15
< Last-Modified: Tue, 04 Jun 2019 04:09:09 GMT
< Connection: keep-alive
< ETag: "5cf5eee5-f"
< Accept-Ranges: bytes
< 
www.oldboy.com
* Connection #0 to host www.oldboy.com left intact

到此为止Linux命令行测试ok

测试windows浏览器访问

windows hosts解析(写法与linux一致) >C:\Windows\System32\drivers\etc\hosts

9469008-6294c3386b1352ed.png
image.png

9469008-a1fe1abce2348def.png
image.png
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值