07Web网站 Nginx(第二部分)

news/2024/5/9 17:23:46/文章来源:https://blog.csdn.net/weixin_45598345/article/details/118873268

Nginx虚拟主机设置

  • 1.Nginx虚拟主机
    • 01.nginx虚拟主机介绍
    • 02.nginx相关错误
    • 03.虚拟主机的常见类型
      • 1 基于域名的虚拟主机的配置
      • 2 基于端口的配置
      • 3 基于IP虚拟主机的配置
    • 04.nginx处理用户请求过程※※※
    • 05.nginx核心配置※※※
      • 1 nginx日志格式
      • 2 log_format日志格式的详细介绍
    • 06.nginx配置文件切割
      • 1 不同的虚拟主机如何指定不同的日志文件
    • 07.nginx状态模块机权限控制
    • 08.nginx状态模块
    • 09.nginx的location规则
      • 1 location的作用
      • 2 location语法
      • 3 location语法说明
      • 4 匹配标识分别代表的含义
      • 5 location的优先级
      • 6 location规则应用:

1.Nginx虚拟主机

01.nginx虚拟主机介绍

1个虚拟主机相当于是一个网站
nginx中多个server标签

02.nginx相关错误

ping 域名
curl 域名(nginx服务)
nginx配置之后,要检查语法并reload

03.虚拟主机的常见类型

基于域名的虚拟主机:不同的域名访问不同虚拟主机(网站)
基于端口:不同的端口访问不同的虚拟主机----网站需要特殊端口的时候使用
基于IP虚拟主机:不同的IP地址访问不同的虚拟主机,在负载均衡的时候也会使用

1 基于域名的虚拟主机的配置

\\\在nginx进行配置
[root@web01 ~]# vim /etc/nginx/conf.d/nginx.conf 
……server   {listen       80;server_name  www.oldboy.com;location / {root   /usr/share/nginx/html/www;index  index.html index.htm;}  }   server   {listen       80;server_name  blog.oldboy.com;location / {root   /usr/share/nginx/html/blog;index  index.html index.htm;}}\\\创建index.html文件   
[root@web01 ~]# for i in www blog ;do echo  $i.oldboy.com>/usr/share/nginx/html/$i/index.html;done\\\检查语法
[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\\\平滑启动nginx服务
[root@web01 ~]# systemctl reload nginx
[root@web01 ~]# cat /usr/share/nginx/html/{www,blog}/index.html
www.oldboy.com
blog.oldboy.com\\\新添加的域名一定要进行hosts解析
[root@web01 ~]# vim /etc/hosts
……
172.16.1.7      web01 www.oldboy.com blog.oldboy.com 
……\\\检查域名是否可用
[root@web01 ~]# curl www.oldboy.com  
www.oldboy.com
[root@web01 ~]# blog.oldboy.com

2 基于端口的配置

\\\在nginx进行配置
[root@web01 ~]# vim /etc/nginx/conf.d/nginx.conf 
……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;
[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 reload  nginx \\\检查配置之后的端口
[root@web01 ~]# ss -lntup|grep nginx
tcp    LISTEN     0      128       *:81                    *:*                   users:(("nginx",pid=51376,fd=10),("nginx",pid=30059,fd=10))
tcp    LISTEN     0      128       *:82                    *:*                   users:(("nginx",pid=51376,fd=11),("nginx",pid=30059,fd=11))
[root@web01 ~]# curl www.oldboy.com
curl: (7) Failed connect to www.oldboy.com:80; Connection refused
[root@web01 ~]# curl http://10.0.0.7:81
www.oldboy.com
[root@web01 ~]# curl http://10.0.0.7:82
blog.oldboy.com
[root@web01 ~]# 

3 基于IP虚拟主机的配置

[root@web01 ~]# vim /etc/nginx/conf.d/nginx.conf 
……server   {listen      10.0.0.7:80;server_name  www.oldboy.com;location / {root   /usr/share/nginx/html/www;index  index.html index.htm;}}server   {listen       10.0.0.9:80;server_name  blog.oldboy.com;location / {root   /usr/share/nginx/html/blog;index  index.html index.htm;}}}
"/etc/nginx/nginx.conf" 49L, 1004C written   
\\检查报错,语法没错,但是里面的指定10.0.0.9这个端口在本地不存在                                                                         
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: [emerg] bind() to 10.0.0.9:80 failed (99: Cannot assign requested address)
nginx: configuration file /etc/nginx/nginx.conf test failed\\\因为新添加的10.0.0.0.9这个网段在本机没有,需要添加
[root@web01 ~]# ip addr add 10.0.0.9/24 dev eth0 label eth0:1
[root@web01 ~]# ip a s eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000link/ether 00:0c:29:36:af:f4 brd ff:ff:ff:ff:ff:ffinet 10.0.0.7/24 brd 10.0.0.255 scope global eth0valid_lft forever preferred_lft foreverinet 10.0.0.9/24 scope global secondary eth0:1valid_lft forever preferred_lft foreverinet6 fe80::20c:29ff:fe36:aff4/64 scope link valid_lft forever preferred_lft forever
[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 restart nginx
[root@web01 ~]# ss -lntup|grep nginx  \\检查端口
tcp    LISTEN     0      128    10.0.0.9:80                    *:*                   users:(("nginx",pid=51851,fd=7),("nginx",pid=51850,fd=7))
tcp    LISTEN     0      128    10.0.0.7:80                    *:*                   users:(("nginx",pid=51851,fd=6),("nginx",pid=51850,fd=6))
[root@web01 ~]# curl 10.0.0.7:80
www.oldboy.com
[root@web01 ~]# curl 10.0.0.9:80
blog.oldboy.com
[root@web01 ~]# 

04.nginx处理用户请求过程※※※

1>看nginx配置中指定的端口
2>是否有我想要的域名(host)
  如果有,使用这个虚拟主机
  如果没有,使用默认的虚拟主机(第1个)

在这个配置中,nginx只测试请求的头字段“host”,以确定请求>应该路由到哪个服务器。如果它的值与任何服务器名称不匹配,或者请求根本不包含这个头字段,那么nginx将把请求路由到这个端口的默认服务器。在上面的配置中,默认服务器是第一个——这是nginx的标准默认行为。还可以通过listen指令中的default_server参数明确设置默认服务器:

> <pre style="padding: 0px; margin: 0px;">
server { listen 80 **default_server** ; 
server_name example.net www.example.net; 
...  
}</pre>

05.nginx核心配置※※※

1 nginx日志格式

log_format:指定nginx日志格式
access_log:开启日志,指定日志文件
  buffer=16k
  flush=time
gzip 压缩
解压:gzip -d
看压缩包中的内容用以下命令:zcat、zless、zgrep、zmore、zegrep ,如:zcat /etc/nginx/conf.d/default.conf.gz

2 log_format日志格式的详细介绍

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;'$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地址  

注:nginx -V:检查nginx所有的模块:

06.nginx配置文件切割

1 不同的虚拟主机如何指定不同的日志文件

[root@web01 ~]# vim /etc/nginx/nginx.conf 
……server   { listen      80;server_name  www.oldboy.com;location / { root   /usr/share/nginx/html/www;index  index.html index.htm;access_log  /var/log/nginx/access_www.log  main;}}   server   {listen       80;server_name  blog.oldboy.com;location / { root   /usr/share/nginx/html/blog;index  index.html index.htm;access_log  /var/log/nginx/access_blog.log  main;}}   "/etc/nginx/nginx.conf" 51L, 1102C written
[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 reload nginx
[root@web01 ~]# ll /var/log/nginx/
total 12
-rw-r--r-- 1 root  root    0 Jun  5 11:48 access_blog.log
-rw-r----- 1 nginx adm  5058 Jun  5 11:20 access.log
-rw-r--r-- 1 root  root    0 Jun  5 11:48 access_www.log
-rw-r----- 1 nginx adm  2013 Jun  5 11:20 error.log
[root@web01 ~]# 

实例2.2.2 在实际工作中会有很多的虚拟主机,都配置到/etc/nginx/nginx.conf 中会配置多的server标签,也不好管理,估将不同的server标签分为不同的文件

[root@web01 ~]# vim /etc/nginx/nginx.conf 
……include /etc/nginx/conf.d/*.conf;
}[root@web01 ~]# vim /etc/nginx/conf.d/01-www.conf
server   {listen      80;server_name  www.oldboy.com;
▽       location / {root   /usr/share/nginx/html/www;index  index.html index.htm;access_log  /var/log/nginx/access_www.log  main;}}"01-www.conf" [New] 9L, 242C written                                                                                
[root@web01 ~]# vim /etc/nginx/conf.d/02-blog.conf
server   {listen       80;server_name  blog.oldboy.com;location / {root   /usr/share/nginx/html/blog;index  index.html index.htm;access_log  /var/log/nginx/access_blog.log  main;} }[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 reload nginx
[root@web01 ~]# ll /etc/nginx/conf.d/
total 16
-rw-r--r-- 1 root root 242 Jun  5 12:00 01-www.conf
-rw-r--r-- 1 root root 246 Jun  5 12:00 02-blog.conf
-rw-r--r-- 1 root root 488 Apr 23 22:34 default.conf.gz
[root@web01 ~]# curl www.oldboy.com
www.oldboy.com
[root@web01 ~]# curl blog.oldboy.com
blog.oldboy.com
[root@web01 ~]#  

注:将默认的站点目录default.conf用gzip压缩,不然会有影响,解压用gzip -d,查看gzip压缩用命令:zcat、zless、zgrep、zmore、zegrep

07.nginx状态模块机权限控制

配置状态模块

[root@web01 /etc/nginx/conf.d]# vim status.conf
server{listen   80;server_name   status.oldboy.com;stub_status  on;access_log  off;
}                                                                                                                                   
~                                                                                                                                     
"status.conf" [New] 7L, 108C written                                                                                
[root@web01 /etc/nginx/conf.d]# vim /etc/hosts
……
172.16.1.7      web01 www.oldboy.com blog.oldboy.com bbs.oldboy.com status.oldboy.com[root@web01 /etc/nginx/conf.d]# systemctl reload nginx
[root@web01 /etc/nginx/conf.d]# curl status.oldboy.com
Active connections: 1 
server accepts handled requests13 13 13 
Reading: 0 Writing: 1 Waiting: 0 
[root@web01 /etc/nginx/conf.d]# 

如果指定某个ip网段可以查看状态,其他网段都不能查看,进行以下设置即可

在这里插入图片描述
案例:
权限控制
基于用户登录配置(简单验证)
1>在status.conf 中配置配置用户及密码

[root@web01 /etc/nginx/conf.d]# cat status.conf 
server{ listen   80; server_name   status.oldboy.com;stub_status  on;access_log  off;auth_basic "Auth access Blog Input your Passwd!";   \\指定用户密码提示auth_basic_user_file /etc/nginx/htpasswd;     \\指定用户密码文件}

2>添加密码文件

[root@web01 /etc/nginx/conf.d]# htpasswd -bc /etc/nginx/htpasswd  oldboy   oldboy
Adding password for user oldboy

3>设置密码文件的权限为600,所有者及属组为nginx

\\修改密码文件的权限为600
[root@web01 /etc/nginx/conf.d]# chmod 600 /etc/nginx/htpasswd\\修改密码文件的所有者及所有属组为nginx
[root@web01 /etc/nginx/conf.d]# chown nginx.nginx /etc/nginx/htpasswd

5>启动nginx服务

[root@web01 /etc/nginx/conf.d]# systemctl reload nginx

6>浏览器输入域名检查,如图:

08.nginx状态模块

通过监控软件查看nginx的状态
在这里插入图片描述

[root@web01 ~]# curl status.oldboy.com
Active connections: 1 
server accepts handled requests23 23 23 
Reading: 0 Writing: 1 Waiting: 0 \\------------------分别代表的含义-----------------------------------
Active connections: 1     当前的连接数量(已经建立的连接)server accepts             服务器接收到的请求数量
server handled             服务器处理的请求数量
server requests            用户一共向服务器发出多少请求 Reading: 0                 当前nginx正在读取的用户请求头的数量 
Writing: 1                 当前nginx正在响应用户请求的数量
Waiting: 0                 当前等待被nginx处理的请求数量 

取出本地的状态码

[root@web01 /etc/nginx/conf.d]# curl  10.0.0.7
HTTP/1.1 200 OK
Server: nginx/1.16.0
Date: Thu, 06 Jun 2019 01:58:40 GMT
Content-Type: text/html
Content-Length: 15
Last-Modified: Wed, 05 Jun 2019 09:52:47 GMT
Connection: keep-alive
ETag: "5cf790ef-f"
Accept-Ranges: bytes[root@web01 ~]# curl 10.0.0.7|awk 'NR==1{print $2}'% Total    % Received % Xferd  Average Speed   Time    Time     Time  CurrentDload  Upload   Total   Spent    Left  Speed
100    16  100    16    0     0  22471      0 --:--:-- --:--:-- --:--:-- 16000[root@web01 ~]# curl -sI 10.0.0.7|awk 'NR==1{print $2}'
200

curl 常见的参数:
  -s:不显示网页的内容
  -w:什么输出完成后
  -o:把网站页面的内容写入到哪里或黑洞`

09.nginx的location规则

1 location的作用

根据用户请求的URL来执行不同的应用,即URI的内容,即获取的文件后缀不同,如同.html .jsp 等

2 location语法

location[=|~|~*|^~]url{……}

3 location语法说明

在这里插入图片描述

4 匹配标识分别代表的含义

在这里插入图片描述

5 location的优先级

[root@web01 /etc/nginx/conf.d]# cat 01-www.conf
server {listen       80;server_name  www.oldboy.com;root   html/www;location / {return 200  "location / \n";}location = / {return 200 "location = \n";}location /documents/ {return 200 "location /documents/ \n";}location ^~ /images/ {return 200 "location ^~ /images/ \n";}location ~* \.(gif|jpg|jpeg)$ {return 200 "location ~* \.(gif|jpg|jpeg) \n";}access_log off;
}

在这里插入图片描述

6 location规则应用:

1>限制敏感目录

location /admin{deny all;
}

2>区分不同的文件类型

location ~* \.(gif|jpg|jpeg)$ {在用户浏览器缓存10年
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_904443.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

o7Web网站Nginx(第三部分)

nginx 常用模块 1.模块 ngx_http_access_module 指定网段访问功能01.功能介绍02.指令03.示例配置04.应用场景 2.模块 ngx_http_auth_basic_module 页面认证功能01.功能介绍02.指令03.示例配置04.应用场景 3.模块 ngx_http_autoindex_module 列表目录01.功能介绍02.指令03.示例配…

07 Web网站 Nginx (第四部分)

企业应用Nginx以及LNMP架构 1.企业应用过程2.利用Nginx搭建一个多网站3.访问形式4.LNMP架构实现过程01.LNMP架构说明1 搭建网站必备环境2 LNMP架构环境部署 02.LNMP架构读写原理与通信原理解析1实现Nginx与PHP.server的通信2实现PHP.server与MySQL的通信 1.企业应用过程 &#…

07 Web网站 Nginx (第五部分)

LNMP架构补充 负载均衡 高可用服务 01.LNMP补充1.LNMP上节课补充2.LNMP架构与存储服务器建立联系3.LNMP架构与数据库服务建立联系&#xff08;数据迁移&#xff09; 02.Nginx反向代理 负载均衡1.反向代理 负载均衡说明2.什么是反向代理&#xff0c;什么是负载均衡&#xff1f;3…

新媒体专员必备素材网站,没有灵感,找它就对了!

大家都知道&#xff0c;新媒体运营专员每天都要写很多文字&#xff0c;很多不同的&#xff0c;优质的内容来达到自己的目的&#xff01; 但是&#xff0c;天天写&#xff0c;日日写&#xff0c;肚子里再多墨水也有干涸的时候&#xff01;当我们写不出东西时&#xff0c;该怎么…

给大家分享个 网站头像上传的 插件

给大家分享个 网站头像上传的 插件&#xff0c;可以实现头像的修改&#xff0c; 下载链接&#xff1a;http://download.csdn.net/detail/abc456456456456/6621241

学会了SEO就真的会网络营销吗?

目前国内的形式是跟风流&#xff0c;最近SEO的兴起带起了不少SEO培训机构的兴起&#xff0c;线上培训&#xff0c;线下培训屡见不鲜&#xff0c;我也会是在一所培训机构中认识SEO的从而创立了现在的泰州SEO博客网站!SEO的兴起带动了不少企业以及个人的网赚理想&#xff0c;因为…

http://nian.so/#网站的拓展工具编写

这个网站叫做念我用了很久&#xff0c;有安卓和ios版本&#xff0c;我对网页版进行了处理&#xff1b; 写了此社交网站的一个拓展工具。 功能–&#xff1a;对自己或者他人的进展页面内容进行处理&#xff0c;可以筛选关键字。不需要可以隐藏。简单有效。其他页面无法使用。 …

HTML基础——网站图片显示页面

1、图片标签 <img /> 属性&#xff1a;     src:指的是图片显示的路径(位置)       绝对路径&#xff1a;D:\Pictures\Saved Pictures       相对路径&#xff1a;           ①同一级&#xff1a;直接写文件名称或者./文件名称        …

网站银联支付证书更换

题记&#xff1a; 因为公司项目已经运行几年 银联支付的证书在这个月底到期需要更换 步骤: 1.登录 地址: https://merchant.unionpay.com/cas/login 2.点击商户服务平台 3. 3.点击安全证书管理 4.点击下载证书 5.确认启用该证书 禁用要过期的证书 6.点击ie的设置按钮…

手机H5如何对接支付宝登陆授权以及支付(H5网站支付)

场景需求&#xff1a; 通过支付宝扫描二维码进入手机H5网站,获取用户唯一标识appid来标识用户以及处理逻辑。一系列操作后&#xff0c;调起支付窗口完成支付宝支付 1、支付宝开放平台配置 1、登陆支付宝开放平台中心&#xff0c;选择“ 网页&移动应用 ”&#xff0c;然后…

网站demo

学成教育在线网站demo <style>*{margin: 0;padding: 0;}.w{width: 1200px;margin: auto;}body{background-color: #f3f5f7;}/*清除li前面的原点*/li{list-style: none;}/*ul,li{margin:0; padding:0;}*/a{text-decoration: none;}/*清除浮动&#xff0c;咱也不清楚&#…

大型网站架构演变和知识体系

转自&#xff1a;http://www.phpchina.com/html/40/n-35340.html 之前也有一些介绍大型网站架构演变的文章&#xff0c;例如LiveJournal的、ebay的&#xff0c;都是非常值得参考的&#xff0c;不过感觉他们讲的更多的是每次演变的结果&#xff0c;而没有很详细的讲为什么需要做…

springboot“传情旧物”网站计算机毕业设计(源码、运行环境)

登录界面 旧物信息 公告信息 首页 本源代码和数据库都放至公众号毕业admin,需要此套源代码可以在公众号里获取。

springboot报价制酒水交易网站计算机毕业设计(源码、运行环境)

登录界面 商家界面 商品信息 商家注册界面 首页 本源代码和数据库都放至公众号毕业admin,需要此套源代码可以在公众号里获取。

jsp重庆美食网站计算机毕业设计(源码、运行环境)

登录界面 用户管理 美食类型管理 首页界面 美食信息 本源代码和数据库都放至公众号毕业admin,需要此套源代码可以在公众号里获取。

java小区失物招领网站计算机毕业设计(源码、运行环境)

登录界面 论坛交流管理 注册界面 失物展示管理 论坛信息 本源代码和数据库都放至公众号毕业admin,需要此套源代码可以在公众号里获取。

java/php/net/python家教信息网站设计

本系统带文档lw万字以上+答辩PPT+查重 如果这个题目不合适,可以去我上传的资源里面找题目,找不到的话,评论留下题目,或者站内私信我, 有时间看到机会给您发 系统体系结构 家教信息网站 结构图4-1所示: 图4-1 系统结构模块包括主界面,首页、个人中心、系统管理、管理员…

java/php/net/python二手手机回购网站设计

本系统带文档lw万字以上+答辩PPT+查重 如果这个题目不合适,可以去我上传的资源里面找题目,找不到的话,评论留下题目,或者站内私信我, 有时间看到机会给您发 1管理员用例 管理员登录后可进行首页、个人中心、会员管理、类型信息管理、系统管理、手机信息管理、手机估价管…

静和动态网站访问的流程

ip ip就是像127.0.0.1这些比较长的数字组成的东西&#xff0c;很难记住 【注】&#xff1a;由于ip太难记住了所以有了域名 域名&#xff1a; 比如百度&#xff1a;www.baidu.com、淘宝&#xff1a;。。。 这些都是容易记住的名字 DNS: 这货听着很高大上&#xff0c;我第一次也…

记录值得推荐的几本编程入门书和网站

1.Data structure & Algorithm in Java&#xff08;通俗易懂&#xff0c;很适合初学者&#xff09; 2. 嵌入式操作系统基础μC/OS-II和Linux&#xff08;通俗易懂&#xff0c;很适合初学者&#xff09; 3. 图解TCP/IP 协议&#xff08;通俗易懂&#xff0c;很适合初学者&am…