第41章 Nginx Web基础入门

41.1 Nginx部署

41.1.1 Nginx的安装方式

  源码编译 官方仓库 epel仓库
优点 规范 安装简单 安装简单
  便于管理 配置易读  
缺点 安装复杂   配置不易读
  升级繁琐    
版本状态 版本随意 版本较新 版本较低
是否推荐使用 企业实际应用多,推荐 效率高,优先推荐 不推荐

1)方式1——官方仓库

步骤1、安装Nginx依赖
yum install -y gcc gcc-c++ autoconf pcre pcre-devel openssl-devel make automake
步骤2、配置官方yum源
[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
步骤3、安装Nginx服务
[root@web01 ~]# yum install nginx -y
步骤4、启动并设置开机自启
[root@web01 ~]# systemctl start nginx
[root@web01 ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

PS:启动报错:

需要输入 journalctl -xe 或者 systemctl status nginx.service,此处输入journalctl -xe。

问题根源:80端口被占用,一台服务器上不能端口冲突,端口是唯一的。

解决方法:谁占用的,干掉谁。停止占用80端口的服务————systemctl stop httpd,然后再开启nginx服务————systemctl start nginxs
使用命令netstat -lntup————查看端口状况
[root@web01 ~]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp       0     0 0.0.0.0:80             0.0.0.0:*               LISTEN     8092/nginx: master  
tcp       0     0 0.0.0.0:22             0.0.0.0:*               LISTEN     6722/sshd          
tcp       0     0 127.0.0.1:25           0.0.0.0:*               LISTEN     6804/master        
步骤5、检测nginx是否启动成功
1)方法一:监测进程
[root@web01 ~]# ps -ef|grep [n]ginx
root     12457     1 0 11:44 ?       00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     12458 12457 0 11:44 ?       00:00:00 nginx: worker process

2)方法二:如果遇到僵尸进程,可以使用此方法————监测端口
[root@web01 ~]# netstat -lntup|grep 80
tcp       0     0 0.0.0.0:80             0.0.0.0:*               LISTEN     12457/nginx: master

3)方法三:systemd
[root@web01 ~]# systemctl status nginx
● nginx.service - nginx - high performance web server
  Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
  Active: active (running) since Tue 2019-08-13 11:44:03 CST; 8min ago
    Docs: http://nginx.org/en/docs/
Process: 12456 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 12457 (nginx)
  CGroup: /system.slice/nginx.service
          ├─12457 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
          └─12458 nginx: worker process 4)方法四:curl命令
[root@web01 ~]# curl 10.0.0.7
[root@web01 ~]# curl www.driverzeng.com 5)方法五:打开浏览器访问,输入10.0.0.7

2)方式2——源码编译

#此处V为大写字母V
[root@web01 ~]# nginx -V
步骤一、基础环境准备
[root@web02 ~]# yum install -y gcc make wget
步骤二、下载源码包
[root@web02 ~]# wget http://nginx.org/download/nginx-1.16.0.tar.gz
步骤三、解压源码包,并进入相应目录
[root@web02 ~]# tar xf nginx-1.16.0.tar.gz
[root@web02 ~]# ll
total 1020
-rw-------. 1 root root   1445 Jul 24 09:43 anaconda-ks.cfg
-rw-r--r--. 1 root root     473 Jul 24 10:52 host_ip.sh
drwxr-xr-x 8 1001 1001     158 Apr 23 21:13 nginx-1.16.0
-rw-r--r-- 1 root root 1032345 Apr 23 21:58 nginx-1.16.0.tar.gz
[root@web02 ~]# cd nginx-1.16.0

步骤四、配置相关的选项,并生成Makefile,按系统提示补安装相应的依赖

[root@web02 nginx-1.16.0]# ./configure --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'

#提示要安装依赖之一——PCRE    
./configure: error: the HTTP rewrite module requires the PCRE library.

#安装pcre
[root@web02 nginx-1.16.0]# yum install -y pcre-devel

重复执行
[root@web02 nginx-1.16.0]# ./configure --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'

#提示要安装依赖之一——OpenSSL
./configure: error: SSL modules require the OpenSSL library.

#安装openssl
[root@web02 nginx-1.16.0]# yum install -y openssl-devel

重复执行
[root@web02 nginx-1.16.0]# ./configure --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'


#验证命令是否成功,非0都不算成功
[root@web02 nginx-1.16.0]# echo $?
0
步骤五、编译并安装

[root@web02 nginx-1.16.0]# make

[root@web02 nginx-1.16.0]# make install

#验证命令是否成功,非0都不算成功
[root@web02 nginx-1.16.0]# echo $?
0

#Nginx编译的时候指定了用户组和用户,但是系统中并没有这个用户导致的问题
[root@web02 nginx-1.16.0]# /usr/sbin/nginx
nginx: [emerg] getpwnam("nginx") failed ​
#创建用户nginx
[root@web02 nginx-1.16.0]# id nginx
id: nginx: no such user
[root@web02 nginx-1.16.0]# useradd nginx ​
#创建目录/var/cache/nginx/client_temp
[root@web02 nginx-1.16.0]# /usr/sbin/nginx
nginx: [emerg] mkdir() "/var/cache/nginx/client_temp" failed (2: No such file or directory)
[root@web02 nginx-1.16.0]# mkdir -p /var/cache/nginx/client_temp
[root@web02 nginx-1.16.0]# /usr/sbin/nginx
​ #检测nginx源码安装是否成功
[root@web02 nginx-1.16.0]# netstat -lntup |grep 80
tcp       0     0 0.0.0.0:80             0.0.0.0:*               LISTEN     14385/nginx: master
[root@web02 nginx-1.16.0]# ps -ef |grep nginx
root     14385     1 0 17:23 ?       00:00:00 nginx: master process /usr/sbin/nginx
nginx     14386 14385 0 17:23 ?       00:00:00 nginx: worker process
root     14390   7922 0 17:24 pts/0   00:00:00 grep --color=auto nginx


41.1.2 查看Nginx的版本

[root@web01 ~]# nginx -v
nginx version: nginx/1.16.0

41.3 在Nginx首页添加滚动效果代码块

步骤1、查询到index.html路径
[root@web01 ~]# rpm -ql nginx
​ 步骤2、vim 编辑index.html文件,进入index.html文件
[root@web01 ~]# vim /usr/share/nginx/html/index.html


#处理乱码问题
<meta http-equiv="Content-Type" content="text/html"; charset="utf-8">
​ #滚动效果的代码
<html>
<head>
<body>
<meta http-equiv="Content-Type" content="text/html"; charset="utf-8">
<div style="">
<marquee border="0" class="float-content-title-left-up" onmouseout="this.start();" onmouseover="this.stop();" direction="up" scrolldelay="50" style="border:1px solid pink; height: 500px;">
<center><h1><font color='black'>讲师自我介绍</font></h1></center>
<b><p>
<font color='red'>
曾老湿, 江湖人称曾老大。
</font>
<br>
<br><font color='orange'>
联系方式QQ:133411023、253097001
</font>
<br>
<br><font color='yellow'>
交流群:198571640
</font>
<br>
<br><font color='green'>
微信:z133411023
</font>
<br>
<br><font color='red'>
多年互联网运维工作经验,曾负责过大规模集群架构自动化运维管理工作。
</font>
<br>
<br><font color='orange'>
擅长Web集群架构与自动化运维,曾负责国内某大型金融公司运维工作。
</font>
<br>
<br><font color='yellow'>---------------------------------------------------------------------------</font>
<br>
<br><font color='green'>
devops项目经理兼DBA。
开发过一套自动化运维平台(功能如下)
</font>
<br>
<br><font color='red'>
1)整合了各个公有云API,自主创建云主机
</font>
<br>
<br><font color='orange'>
2)ELK自动化收集日志功能。
</font>
<br>
<br><font color='yellow'>
3)Saltstack自动化运维统一配置管理工具。
</font>
<br>
<br><font color='green'>
4)Git、Jenkins自动化代码上线及自动化测试平台。
</font>
<br>
<br><font color='red'>
5)堡垒机,连接Linux、Windows平台及日志审计。
</font>
<br>
<br><font color='orange'>
6)SQL执行及审批流程。
</font>
<br>
<br><font color='yellow'>
7)慢查询日志分析web界面。
</font>
<br>
</p></b>
</marquee>
</div>

<script>
var titleCtx = document.querySelector('.float-content-title-left-up');
document.onscroll = function() {
// t是上滚距离
var t = document.documentElement.scrollTop || document.body.scrollTop;
window.console.log(t)
// 开始变化的节点
if (t > 500) {
titleCtx.style.marginTop = t - 500 + 'px'
} else {
titleCtx.style.marginTop = '0px'
}
}

</script>
</body>
</head>
</html>

41.4  状态码

状态码(status-code)是响应报文状态行中包含的一个3位数字,指明特定的请求是否被满足,如果没有满足,原因是什么。状态码分为以下五类:

状态码非常重要,需要牢记所有的状态码,状态码200、304、403、404等等。

/var/log/httpd/access_log 


1)状态码200————成功
2)状态码304————本地缓存
多刷几遍,刷新页面,可以看到

3)状态码403————找不到主页,权限不足
[root@web01 ~]# cd /usr/share/nginx/html
[root@web01 html]# ll
total 8
-rw-r--r-- 1 root root 494 Apr 23 22:36 50x.html
-rw-r--r-- 1 root root 2778 Aug 13 16:36 index.html

#备份index.html
[root@web01 html]# cp index.html index.html.bak
[root@web01 html]# ll
total 12
-rw-r--r-- 1 root root 494 Apr 23 22:36 50x.html
-rw-r--r-- 1 root root 2778 Aug 13 16:36 index.html
-rw-r--r-- 1 root root 2778 Aug 13 18:05 index.html.bak

#将index.html改成inde.html
[root@web01 html]# mv index.html inde.html
[root@web01 html]# ll
total 12
-rw-r--r-- 1 root root 494 Apr 23 22:36 50x.html
-rw-r--r-- 1 root root 2778 Aug 13 16:36 inde.html
-rw-r--r-- 1 root root 2778 Aug 13 18:05 index.html.bak

4)状态码404————找不到页面

10.0.0.8/index
 

状态码 含义
200 成功
301 永久重定向(跳转)
302 临时重定向(跳转)
304 本地缓存
307 内部重定向(跳转)
400 客户端错误
401 认证失败
403 找不到主页,权限不足
404 找不到页面
500 内部错误
502 找不到后端主机
503 服务器过载
504 后端主机超时

Linux架构之Nginx Web基础1的更多相关文章

  1. Nginx Web 基础入门

    目录 Nginx Web 基础入门 Nginx快速安装 两种方式部署Nginx 如何升级nginx或者添加功能 使用systemd管理nginx nginx相关配置文件 nginx的配置文件详解 虚拟 ...

  2. Nginx web基础入门

    目录 Nginx web基础入门 如何升级nginx或者添加功能 使用systemd管理nginx nginx相关配置文件 nginx的配置文件详解 日志格式 game日志记录实战 日志切割 手写虚拟 ...

  3. Linux架构之Nginx 高可用

    第53章 Nginx之高可用Keepalived 一.Keepalived高可用基本概述 1.1)什么是高可用 一般是指2台机器启动着完全相同的业务系统,当有一台机器down机了,另外一台服务器就能快 ...

  4. Linux架构之Nginx之HTTPS

    第52章 Nginx之HTTPS 第52章 Nginx之HTTPS 1.HTTPS安全证书基本概述 1.1 模拟服务器篡改内容 1.1.1 配置目标网站nginx 1.1.2 配置网页 1.1.3 访 ...

  5. linux篇之Nginx web服务器简单部署

    一.安装部署nginx 1. 部署前先对nginx介绍下别嫌BB: 如果你听说或使用过Apache软件,那么很快就会熟悉Nginx软件,与Apache软件类似, Nginx(“engine x”)是一 ...

  6. Linux架构之Nginx 七层负载均衡

    第50章 Nginx七层负载均衡 一.Nginx负载均衡基本概述 1)为什么要使用负载均衡 当我们的Web服务器直接面向用户,往往要承载大量并发请求,单台服务器难以负荷.使用多台Web服务器组成集群, ...

  7. Linux架构之Nginx 负载均衡会话保持

    案例No.50:Nginx负载均衡会话保持 前期准备环境 web01.web02 (web01.web02.db01.nfs01都要优化基本源)[root@web01 ~]# vim /etc/yum ...

  8. Linux 服务器作为Nginx web服务器常见优化参数

    内核参数调整cat /etc/sysctl.conf# sysctl settings are defined through files in # /usr/lib/sysctl.d/, /run/ ...

  9. Linux架构之Nginx 常见问题

    第54章 Nginx常见问题 一.Nginx多Sever优先级 在开始处理一个http请求时,nginx会取出header头中的Host变量,与nginx.conf中每个server的server_n ...

随机推荐

  1. 【CF1252F】Regular Forestation(重心,树同构)

    题意:给定一棵n个点的树,问删去某个点之后所有的树同构,这样分割出来的树最多能有几棵 n<=4000 思路:分割成至少两个size相等的联通块之后size必定小于n/2,与树的重心的定义相同 预 ...

  2. android读取xml

    /*** 从config.xml中获取版本信息以及应用id* * @param urlPath* @return* @throws Exception*/public List getUpdateIn ...

  3. [BZOJ4552]:[Tjoi2016&Heoi2016]排序(桶排序)

    题目传送门 题目描述 在2016年,佳媛姐姐喜欢上了数字序列. 因而她经常研究关于序列的一些奇奇怪怪的问题,现在她在研究一个难题,需要你来帮助她. 这个难题是这样子的:给出一个1到n的全排列,现在对这 ...

  4. 启用和配置 FILESTREAM

    2017/08/23 在开始使用 FILESTREAM 之前,必须在 SQL Server 数据库引擎实例中启用 FILESTREAM. 本主题说明了如何使用 SQL Server 配置管理器来启用 ...

  5. PROD异机全备份恢复验证实施文档

    PROD异机全备份恢复验证实施文档 ******利用10月25日的全量备份.10月26日当天的归档日志及当前redo日志,恢复数据库到故障前数据库状态****** 准备工作:source 源库:PRO ...

  6. loc() iloc() at() iat()函数

    1 四个函数都是用于dataframe的定位 []用于直接定位. loc()函数是用真实索引,iloc()函数是用索引序号. loc()函数切片是左闭右闭,iloc()函数切片是左闭右开. at(), ...

  7. Centos 下更改MySQL源数据存放目录(datadir)

    MySQL在安装完成之后,其源数据默认存放在 /var/lib/mysql/ 目录下,一般情况下,该目录在根目录下,由于Linux系统默认  根目录所在挂载的磁盘容量有限,随着生产数据的不断产生,该目 ...

  8. python自动化测试接口测试http请求报404的其中一个坑

    在敲代码的路上 ,总是会遇到报错找半天原因,最后发现是个低级错误的时候! 这不今天为了这个错误找了半天原因.......... http请求接口测试中报404我遇到的大部分都是url的问题: 但是今天 ...

  9. Whatever happens tomorrow, we've had today

    bathe: v. 用水清洗 resemblance:n. 相似.相像 cavity:n. 洞,孔,腔 stubborn: adj. 顽强的,固执的 stillness: n. 静止,沉静 tenth ...

  10. ubuntu 上用virtualenv安装python不同版本的开发环境。

    1.用pip安装virtualenv apt-get install python-virtualenv 2.创建python2的虚拟环境,进入要创建虚拟环境的目录下,我是放在/home/pyenv/ ...