2019-06-04日 HTTP补充及ngnix基础

HTTP补充

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

|------------URL------------------|
www.oldboyedu.com/images/lidao.jpg
|-------URI------|

  • Http状态码301和302概念简单区别及企业应用案例
    https://blog.51cto.com/oldboy/1774260

  • 生产环境常见HTTP状态码的博客文章见
    http://oldboy.blog.51cto.com/2561410/716294。

SEM 百度竞价
SEO 搜索引擎优化

  • 伪装成静态页面:伪静态

Nginx ( engine x )基础

功能

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

nginx apache
tengine (淘宝基于nginx2次开发) web服务器

nginx apache主要区别:

nginx 异步模型 epoll
apache 同步模型 select

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.安装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:00     
nginx/x86_64/primary_db                                                                           |  46 kB  00: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 - x86_64                           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
[root@web01 ~]# yum list 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                                           ngin    
  • 4.检查及有关文件简介
[root@web01 ~]# rpm -qa nginx 
nginx-1.16.0-1.el7.ngx.x86_64
[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/nginx/fastcgi_params
        /etc/nginx/koi-utf
        /etc/nginx/koi-win
        /etc/nginx/modules
        /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/html                      #站点目录 网站的根目录 www.oldboyedu.com/oldboy.jpg 
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
/var/cache/nginx
/var/log/nginx                             #nginx日志 access.log 访问日志

[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'

  • 5.启动
[root@web01 ~]# systemctl start nginx 
[root@web01 ~]# ps -ef |grep nginx 
root       8398      1  0 10:51 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      8399   8398  0 10:51 ?        00:00:00 nginx: worker process
root       8425   7482  0 10:51 pts/0    00:00:00 grep --color=auto nginx


systemctl start  nginx     ===== nginx 
systemctl reload nginx     ===== nginx -s reload 
systemctl stop   nginx     ===== nginx -s stop 
  • 6.nginx -t #检查语法
[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

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

[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()

  • 7.配置文件
user  nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}
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;
    keepalive_timeout  65;
    #gzip  on;
    server   {
        listen       80;
        server_name  localhost;
        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
    }
}
nginx配置文件详解.png

测试

www.oldboy.com
显示
www.oldboy.com

  • 配置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
^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
[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
    浏览器访问即可

tcp三次握手
http

nginx 多个网站
nginx 访问日志格式
nginx location

curl
-v
-I
-L

你可能感兴趣的:(2019-06-04日 HTTP补充及ngnix基础)