Linux架构之Nginx Web基础1
第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的更多相关文章
- Nginx Web 基础入门
目录 Nginx Web 基础入门 Nginx快速安装 两种方式部署Nginx 如何升级nginx或者添加功能 使用systemd管理nginx nginx相关配置文件 nginx的配置文件详解 虚拟 ...
- Nginx web基础入门
目录 Nginx web基础入门 如何升级nginx或者添加功能 使用systemd管理nginx nginx相关配置文件 nginx的配置文件详解 日志格式 game日志记录实战 日志切割 手写虚拟 ...
- Linux架构之Nginx 高可用
第53章 Nginx之高可用Keepalived 一.Keepalived高可用基本概述 1.1)什么是高可用 一般是指2台机器启动着完全相同的业务系统,当有一台机器down机了,另外一台服务器就能快 ...
- Linux架构之Nginx之HTTPS
第52章 Nginx之HTTPS 第52章 Nginx之HTTPS 1.HTTPS安全证书基本概述 1.1 模拟服务器篡改内容 1.1.1 配置目标网站nginx 1.1.2 配置网页 1.1.3 访 ...
- linux篇之Nginx web服务器简单部署
一.安装部署nginx 1. 部署前先对nginx介绍下别嫌BB: 如果你听说或使用过Apache软件,那么很快就会熟悉Nginx软件,与Apache软件类似, Nginx(“engine x”)是一 ...
- Linux架构之Nginx 七层负载均衡
第50章 Nginx七层负载均衡 一.Nginx负载均衡基本概述 1)为什么要使用负载均衡 当我们的Web服务器直接面向用户,往往要承载大量并发请求,单台服务器难以负荷.使用多台Web服务器组成集群, ...
- Linux架构之Nginx 负载均衡会话保持
案例No.50:Nginx负载均衡会话保持 前期准备环境 web01.web02 (web01.web02.db01.nfs01都要优化基本源)[root@web01 ~]# vim /etc/yum ...
- Linux 服务器作为Nginx web服务器常见优化参数
内核参数调整cat /etc/sysctl.conf# sysctl settings are defined through files in # /usr/lib/sysctl.d/, /run/ ...
- Linux架构之Nginx 常见问题
第54章 Nginx常见问题 一.Nginx多Sever优先级 在开始处理一个http请求时,nginx会取出header头中的Host变量,与nginx.conf中每个server的server_n ...
随机推荐
- [CSP-S模拟测试]:集合论(模拟)
题目传送门(内部题73) 输入格式 输入文件$jihe.in$ 第一行一个整数$m$,表示操作的次数. 接下来$m$行,每行描述一个操作. 每行的开始都是一个数字,$1,2,3,4$依次代表$unio ...
- 任何国家都无法限制数字货币。为什么呢? 要想明白这个问题需要具备一点区块链的基础知识: 区块链使用的大致技术包括以下几种: a.点对点网络设计 b.加密技术应用 c.分布式算法的实现 d.数据存储技术 e.拜占庭算法 f.权益证明POW,POS,DPOS 原因一: 点对点网络设计 其中点对点的P2P网络是bittorent ,由于是点对点的网络,没有中心化,因此在全球分布式的网
任何国家都无法限制数字货币.为什么呢? 要想明白这个问题需要具备一点区块链的基础知识: 区块链使用的大致技术包括以下几种: a.点对点网络设计 b.加密技术应用 c.分布式算法的实现 d.数据存储技 ...
- Oracle 10046 event
http://czmmiao.iteye.com/blog/1497509 10046事件概述Oracle的10046事件,可以跟踪应用程序所执行的SQL语句,并且得到其解析次数.执行次数,CPU使用 ...
- GMM demo
# GMM model # // library(mvtnorm) ) n1 = n2 = mu1 = c(,) mu2 = c(-,-) sigma1 = matrix(c(,.,.,),nrow= ...
- HttpWebRequest、WebClient、RestSharp、HttpClient区别和用途
HttpWebRequest 已经不推荐直接使用了,这已经作为底层机制,不适合业务代码使用,比如写爬虫的时候WebClient 不想为http细节处理而头疼的coder而生,由于内部已经处理了通用设置 ...
- UI自动化之读取浏览器配置
以火狐浏览器为例 目录 1.找到配置项 2.读取配置 1.找到配置项 打开Firefox点右上角设置>?(帮助)>故障排除信息>显示文件夹,复制文件管理器地址栏 2.读取配置 用Fi ...
- spring boot 启动之后404
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...
- 028 (H5*) 商城实战
目录: 正文: 1:创建项目 介绍 ESlintESLint 是一个ECMAScript/JavaScript 语法规则和代码风格的检查工具,它的目标是保证代码的一致性和避免错误. utit test ...
- (4.31)quotename函数
操作sql server尤其是写存储过程时,要用到各种各样的函数,今天就总结一个quotename()的用法. 1.语法: quotename(‘character_string’[,‘quote_c ...
- JDK8 parallelStream性能测试
https://blog.csdn.net/u011870280/article/details/80700993 public static void main(String[] args) {lo ...


