2019-05-11 Nginx Web应用深入
Day45
作者:方
归档:笔记
时间:2019/5/5
Nginx Web应用深入



Nginx功能模块化:
模块化是为了耦合度更低,易于管理!工作中做事学会低耦合。
SQA架构。RPC服务都属于低耦合的技术模式。




配置文件了信息详情:cat -n nginx.conf


实践基于域名的虚拟主机:
第一步:过滤空行生成新的配置文件
egrep -v "^$|#" nginx.conf.default >nginx.conf

第二步:vim nginx.conf 后四行干掉

第三步:创建站点目录文件index.html,把域名输入进去
mkdir ../html/www
echo "www.etiantian.org" >../html/www/index.html
cat ../html/www/index.html

第四步:把域名和IP追加到 /etc/hosts
echo "10.0.0.8 www.etiantian.org" >>/etc/hosts

第五步:ping 域名,验证
tail -1 /etc/hosts
ping www.etiantian.org

配置变量:
echo 'PATH="/application/nginx/sbin:$PATH"' >>/etc/profile
. /etc/profile
echo $PATH
nginx 检查语法:
nginx -t
nginx 平滑重启:
nginx -s reload
验证成功:
curl www.etiantian.org

WINDOWS下测试:
C:\Windows\System32\drivers\etc\hosts
10.0.0.8 www.etiantian.org

往配置文件里面添加server标签:

创建站点目录文件,把域名和IP信息追加到/etc/hosts

nginx检查语法,平滑重启,curl验证

基于域名的虚拟主机通信原理介绍

基于端口的虚拟主机实践:
1.备份:

2.修改配置文件内的端口即可

3.检查语法,没问题平滑重启

4.检查端口,验证配置结果(结尾需加端口)

基于IP的虚拟主机:(了解即可)
第一步:配置网卡

第二步:检查ping

第三步:修改配置文件


防止网站被恶意解析,设置配置文件:
在配置里,第一个标签添加:


实践优化nginx文件:


优化nginx配置文件:
[root@web02 /application/nginx/conf]# mkdir extra
[root@web02 /application/nginx/conf]# sed -n '10,17p' nginx.conf
打印www.etiantian.org虚拟主机配置文件 server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
}
[root@web02 /application/nginx/conf]#
sed -n '10,17p' nginx.conf >extra/01_www.conf
[root@web02 /application/nginx/conf]# sed -n '18,25p' nginx.conf
server {
listen 80;
server_name bbs.etiantian.org;
location / {
root html/bbs;
index index.html index.htm;
}
}
[root@web02 /application/nginx/conf]#
sed -n '18,25p' nginx.conf >extra/02_bbs.conf
[root@web02 /application/nginx/conf]# sed -n '26,33p' nginx.conf
server {
listen 80;
server_name blog.etiantian.org;
location / {
root html/blog;
index index.html index.htm;
}
}
[root@web02 /application/nginx/conf]#
sed -n '26,33p' nginx.conf** >extra/03_blog.conf
[root@web02 /application/nginx/conf]# cd extra/
[root@web02 /application/nginx/conf/extra]# cat 01_www.conf
server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
}
[root@web02 /application/nginx/conf/extra]# cat 02_bbs.conf
server {
listen 80;
server_name bbs.etiantian.org;
location / {
root html/bbs;
index index.html index.htm;
}
}
[root@web02 /application/nginx/conf/extra]# cat 03_blog.conf
server {
listen 80;
server_name blog.etiantian.org;
location / {
root html/blog;
index index.html index.htm;
}
}
[root@web02 /application/nginx/conf/extra]# cd ../
[root@web02 /application/nginx/conf]# sed -n '10,33p' nginx.conf
server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
}
server {
listen 80;
server_name bbs.etiantian.org;
location / {
root html/bbs;
index index.html index.htm;
}
}
server {
listen 80;
server_name blog.etiantian.org;
location / {
root html/blog;
index index.html index.htm;
}
}
[root@web02 /application/nginx/conf]# sed -i '10,33d' nginx.conf **
[root@web02 /application/nginx/conf]#
sed -i '10 i include extra/01_www.conf;\ninclude extra/02_bbs.conf;\ninclude extra/03_blog.conf;' nginx.conf
[root@web02 /application/nginx/conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include extra/01_www.conf;
include extra/02_bbs.conf;
include extra/03_blog.conf;
}
[root@web02 /application/nginx/conf]# nginx -t
nginx: the configuration file /application/nginx-1.16.0//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.16.0//conf/nginx.conf test is successful
[root@web02 /application/nginx/conf]# nginx -s reload
[root@web02 /application/nginx/conf]# curl www.etiantian.org
www.etiantian.org
[root@web02 /application/nginx/conf]# curl bbs.etiantian.org
bbs.etiantian.org
[root@web02 /application/nginx/conf]# curl blog.etiantian.org
blog.etiantian.org
别名:一个名字另外一个名字
两个域名都可以访问到相同的内容。



配置server标签:

配置完成后进行本机/etc/hosts解析。







#log_format main 'remote_user [
request" '
**# '$status $body_bytes_sent "$http_referer" '**
**# '"$http_user_agent" "$http_x_forwarded_for"';**

脚本日志切割


if uri then 相当于一个判断句




下面是官方给出的的location示例,我们通过实例来验证不同的location标签生效的顺序,Nginx的配置文件为:
[root@web01 conf]# cp extra/01_www.conf{,.ori}
[root@web01 conf]# cat extra/01_www.conf
server {
listen 80;
server_name www.etiantian.org etiantian.org;
root html/www;
location / {
return 401;
}
location = / {
return 402;
}
location /documents/ {
return 403;
}
location ^~ /images/ {
return 404;
匹配任何以/images/开头的任何查询并且停止搜索。任何正则表达式匹配将不会被检查。
"^~" 这个前缀的作用:在常规的字符串匹配检查之后,不做正则表达式的检查,即如果最明确的那个字符串匹配的location配置中有此前缀,那么不会做正则表达式的检查。
}
}
location ~* .(gif|jpg|jpeg)$ {
#匹配任何以 gif、jpg 或 jpeg 结尾的请求。
return 500;
}
access_log logs/access_www.log main gzip buffer=32k flush=5s;
}
检查语法并使得修改的配置生效:
[root@web01 conf]# nginx -t
[root@web01 conf]# nginx -s reload
然后以Linux客户端为例对上述location匹配进行真实测试,配置hosts文件如下。
[root@web01 conf]# tail -1 /etc/hosts
10.0.0.7 www.etiantian.org bbs.etiantian.org blog.etiantian.org etiantian.org
实验结果如下:
[root@web01 conf]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org
402
[root@web01 conf]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/
402
[root@web01 conf]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/index.html
401
[root@web01 conf]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/documents/document.html
403
[root@web01 conf]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/images/1.gif
404
[root@web01 conf]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/documents/1.jpg
500
[root@web01 conf]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/oldboy/
401
[root@web01 conf]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/abc/
401

rewrite实现伪静态功能

301跳转:
[root@web02 /application/nginx/conf/extra]# cat 01_www.conf
server {
listen 80;
server_name etiantian.org;
rewrite ^/(.*) http://www.etiantian.org/$1 permanent;
}
server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
access_log logs/access_www.log main;
}
access_log logs/access_www.log main;



出现403的原因:
1、 没有首页文件,index.xxxx
2、 站点目录权限太低 755