Nginx和apache主要区别:
nginx是异步传输
apache是同步传输
例:apache是一个一个得找,nginx是将所需内容放到一起找,效率更高
安装nginx软件依赖包
yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake wget httpd-tools vim tree
1.基于官方源进行nginx安装
官网:http://nginx.org/en/download.html
![](https://img.haomeiwen.com/i16952149/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
image
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 #跟nginx有关的配置文件
/etc/nginx/conf.d #nginx主配置文件(最主要的)
/etc/nginx/conf.d/default.conf #与conf.d类似
/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日志 access.log(核心访问日志)
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地址
image
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.服务配置文件
https://www.processon.com/view/link/5cf5e2cae4b0bc8329e71aad
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.官网查询帮助
模块格式 官网查询模块格式
image
image
image
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;
}
}
}
为域名添加/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
14.在上面进入网站的时用Wireshark抓包:
查询http协议 与 tcp协议
三次握手 ---》请求连接 ---》响应连接 ---》断开连接
网站的小图标如果没有的话,会报一个404
![](https://img.haomeiwen.com/i16952149/bb359e76740f1260.png)
![](https://img.haomeiwen.com/i16952149/c5e39d51e9db6805.png)
nginx语法使用
systemctl start nginx等于nginx
systemctl reload nginx等于nginx -s reload
systemctl stop nginx等于nginx -s stop
nginx主配置文件详解
vim /etc/nginx/nginx.conf
![](https://img.haomeiwen.com/i16953283/593dcb853c9be6b1.png)
中间部分的include /etc/nginx/mtinme.types表示把其他位置的文件加入到这个位置
![](https://img.haomeiwen.com/i16953283/10a68f57da3b6e1e.png)
第三行server_name后面写域名
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; ---引用这个文件 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; ---可以搭建几个网站(现在表示将此目录下以.conf结尾的都算)
server { ---以下8行表示访问网站所必须配置的内容
listen 80;
server_name www.oldboy.com;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
}
状态信息
![](https://img.haomeiwen.com/i16953283/696408daa1907983.png)
查看访问网站的状态码
curl -sw "%{http_code}\n" -o /dev/null jd.com
虚拟主机
认识
1个虚拟主机 相当于是1个网站
Nginx多个server标签
不同虚拟主机
虚拟主机(必备)
不同的域名不同的网站
1.Nginx的server标签
[8:55 root@web01 ~]# mkdir -p /usr/share/nginx/html/{www,blog} \\创建www与blog站点目录
[09:00 root@web01 ~]# for n in www blog ;do echo $n.oldboy.com >/usr/share/nginx/html/$n/index.html ;done
\\给俩个站点目录index.html文件 添加内容
[09:00 root@web01 ~]# cat /usr/share/nginx/html/{www,blog}/index.html
www.oldboy.com
blog.oldboy.com
[09:01 root@web01 ~]# vim /etc/hosts \\添加域名解析
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.loca
ldomain4
::1 localhost localhost.localdomain localhost6 localhost6.loca
ldomain6
172.16.1.5 lb01
172.16.1.6 lb02
172.16.1.7 web01 www.oldboy.com blog.oldboy.com \\添加域名解析
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
[09:03 root@web01 ~]# systemctl restart nginx \\重启nginx服务
[09:03 root@web01 ~]# curl www.oldboy.com \\curl一下www的域名
www.oldboy.com
[09:04 root@web01 ~]# curl blog.oldboy.com \\curl一下blog的域名
blog.oldboy.com
[09:04 root@web01 ~]#
※【不同的虚拟主机】
基于域名的虚拟主机(必备)
不同的域名访问不同虚拟主机(网站)
基于端口的虚拟主机
不同的端口访问不同的虚拟主机
正常端口 80 443
网站后台人员 使用特殊端口
基于ip的虚拟主机
nginx处理用户请求过程
3. 基于端口的虚拟主机
用81和82端口测试一下
修改端口为81 82
server {
listen 81;
server_name www.oldboy.com;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
}
}
server {
listen 82;
server_name blog.oldboy.com;
location / {
root /usr/share/nginx/html/blog;
index index.html index.htm;
}
}
}
[10:14 root@web01 ~]# curl http://10.0.0.7
curl: (7) Failed connect to 10.0.0.7:80; Connection refused
[10:14 root@web01 ~]# curl http://10.0.0.7:81
www.oldboy.com
[10:14 root@web01 ~]# curl http://10.0.0.7:82
blog.oldboy.com
4.基于ip的虚拟主机
nginx.conf中添加一个新的ip
server {
listen 10.0.0.9:80;
server_name blog.oldboy.com;
location / {
root /usr/share/nginx/html/blog;
index index.html index.htm;
}
}
修改之后直接重启或者检查语法会报错
因为没有注册ip
[10:30 root@web01 ~]# systemctl restart nginx
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
[10:30 root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: [emerg] bind() to 10.0.0.66:81 failed (99: Cannot assign requested address)
nginx: configuration file /etc/nginx/nginx.conf test failed
添加ip地址
[10:30 root@web01 ~]# ip addr add 10.0.0.9/24 dev eth0 label eth0:1
[10:31 root@web01 ~]# ip a
.....
....
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:68:78:4f brd ff:ff:ff:ff:ff:ff
inet 10.0.0.7/24 brd 10.0.0.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet 10.0.0.9/24 scope global secondary eth0:1
valid_lft forever preferred_lft forever
systemctl重启nginx
curl看一下
[10:31 root@web01 ~]# systemctl restart nginx
[10:35 root@web01 ~]# curl 10.0.0.9:80
blog.oldboy.com
[10:38 root@web01 ~]# curl 10.0.0.7
www.oldboy.com
5.nginx配置默认访问第一个
在此配置中,nginx仅测试请求的标头字段“Host”,以确定请求应路由到哪个服务器。如果其值与任何服务器名称都不匹配,或者请求根本不包含此标头字段,则nginx会将请求路由到此端口的默认服务器。在上面的配置中,默认服务器是第一个 - 这是nginx的标准默认行为。它也可以
default_server
使用listen指令中的参数明确设置哪个服务器应该是默认的:
> <pre style="padding: 0px; margin: 0px;">
server { listen 80 **default_server** ;
server_name example.net www.example.net;
...
}</pre>
※6.nginx的日志
/var/log/nginx/access.log
Nginx内置变量
![](https://img.haomeiwen.com/i16952149/d2b5b988ee8d29c1.png)
'$remote_addr 客户端ip地址
$remote_user 远程用户(空)
[$time_local] 时间
"$request" 请求报文的起始行 $request_uri 只取出uri
'$status 状态码
$body_bytes_sent 身体 字节 发送 服务端发给客户端大小(每个文件的大小)
"$http_referer" 记录着用户从哪里跳转过来的
'"$http_user_agent" 用户浏览器
"$http_x_forwarded_for"'; 负载均衡: web服务器用来记录用户真实ip地址
日志格式:
10.0.0.7 - - [05/Jun/2019:11:06:14 +0800] "GET /index.html HTTP/1.1" 200 15 "-" "curl/7.29.0" "-"
"$http_referer"记录的用户从哪里跳转过来的
百度搜索本地ip
image.png
304 Not Modifed 用户读取浏览器缓存
image
ip访问量:
awk '{print $1}' /var/log/nginx/access.log |sort |uniq -c
image
pv访问量:
wc -l /var/log/nginx/access.log
image
8.Nginx日志格式
access_log
nginx配置文件中添加配置日志access_log
[11:48 root@web01 ~]# vim /etc/nginx/nginx.conf
....
server {
listen 80;
server_name www.oldboy.com;
access_log /var/log/nginx/access_www.log main; \\日志
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
}
}
server {
listen 80;
server_name blog.oldboy.com;
access_log /var/log/nginx/access_blog.log main; \\日志
location / {
root /usr/share/nginx/html/blog;
index index.html index.htm;
}
}
}
去/etc/hosts添加域名解析
[root@m01 /usr/share/nginx/html]# vim /etc/hosts
10.0.0.7 www.oldboy.com blog.oldboy.com status.oldboy.com
重启nginx后检查语法 查看日志路径下的内容:
[11:52 root@web01 ~]# systemctl reload nginx.service
[11:52 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
[11:52 root@web01 ~]# ll /var/log/nginx/access*
-rw-r--r-- 1 root root 0 Jun 5 11:48 /var/log/nginx/access_blog.log
-rw-r----- 1 nginx adm 12983 Jun 5 11:36 /var/log/nginx/access.log
-rw-r--r-- 1 root root 0 Jun 5 11:48 /var/log/nginx/access_www.log
nginx状态模块及权限控制
状态模块
权限控制
7.将配置中的模块单独写出来放到conf.d下
最好将站点目录的默认
zcat zless zmore zgrep zegrep
压缩gzip 解压 gzip -d
[12:13 root@web01 /etc/nginx]# ll conf.d/
total 12
-rw-r--r-- 1 root root 233 Jun 5 12:04 01-www.conf
-rw-r--r-- 1 root root 254 Jun 5 12:04 02-blog.conf
-rw-r--r-- 1 root root 488 Apr 23 22:34 default.conf.gz
8.添加status.oldboy.com域名
配置status.conf添加到conf.d下
重启nginx检查语法 curl一下status.conf中添加的域名
[12:22 root@web01 /etc/nginx]# cat conf.d/status.conf
server {
listen 80;
server_name status.oldboy.com;
stub_status on;
access_log off;
}
[12:22 root@web01 /etc/nginx]# ll conf.d/
total 16
-rw-r--r-- 1 root root 233 Jun 5 12:04 01-www.conf
-rw-r--r-- 1 root root 254 Jun 5 12:04 02-blog.conf
-rw-r--r-- 1 root root 488 Apr 23 22:34 default.conf.gz
-rw-r--r-- 1 root root 90 Jun 5 12:21 status.conf
[12:26 root@web01 /etc/nginx]# systemctl restart nginx
[12:26 root@web01 /etc/nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[12:26 root@web01 /etc/nginx]# curl status.oldboy.com
Active connections: 1
server accepts handled requests
1 1 1
Reading: 0 Writing: 1 Waiting: 0
9.添加allow 允许网段
官网:http://nginx.org/en/docs/http/ngx_http_access_module.html#allow
ngx_http_access_module 模块
不要写错格式
检查语法后重启 curl一下status.oldboy.comimage
[12:36 root@web01 /etc/nginx]# vim conf.d/status.conf
server {
listen 80;
server_name status.oldboy.com;
stub_status on;
access_log off;
allow 172.16.1.0/24; \\添加允许网段
deny all;
}
[12:37 root@web01 /etc/nginx]# nginx -t
nginx: [emerg] unexpected "}" in /etc/nginx/conf.d/status.conf:8
nginx: configuration file /etc/nginx/nginx.conf test failed
[12:37 root@web01 /etc/nginx]# systemctl reload nginx
[12:37 root@web01 /etc/nginx]# curl status.oldboy.com
Active connections: 1
server accepts handled requests
7 7 7
Reading: 0 Writing: 1 Waiting: 0
浏览器看一下今天配置的这几个域名可不可以访问
image.png
image.png
image.png
10.curl -H的用法
curl -H Host:status.oldboy.com 10.0.0.7
[12:45 root@web01 /etc/nginx]# curl -H Host:status.oldboy.com 10.0.0.7
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.16.0</center>
</body>
</html>
网友评论