Nginx多个相同Server_name优先级php
[root@nginx ~]# mkdir /soft/code{1..3} -p [root@nginx ~]# for i in {1..3};do echo "<h1>Code $i</h1>" > /soft/code"$i"/index.html;done
[root@Nginx conf.d]# ll 总用量 12 -rw-r--r-- 1 root root 123 4月 19 19:08 testserver1.conf -rw-r--r-- 1 root root 123 4月 19 19:09 testserver2.conf -rw-r--r-- 1 root root 123 4月 19 19:09 testserver3.conf //内容以下 [root@Nginx conf.d]# cat testserver{1..3}.conf server { listen 80; server_name testserver1 192.168.69.113; location / { root /soft/code1; index index.html; } } server { listen 80; server_name testserver2 192.168.69.113; location / { root /soft/code2; index index.html; } } server { listen 80; server_name testserver3 192.168.69.113; location / { root /soft/code3; index index.html; } } //检测语法 [root@Nginx conf.d]# nginx -t nginx: [warn] conflicting server name "192.168.69.113" on 0.0.0.0:80, ignored nginx: [warn] conflicting server name "192.168.69.113" on 0.0.0.0:80, ignored nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful //重启Nginx [root@Nginx conf.d]# nginx -t
#1.当用户第一次访问, 由code1.conf返回输出信息 [root@Nginx conf.d]# curl 192.168.69.113 <h1>Code 1</h1> #2.此时将code1.conf修改成code5.conf后进行重载Nginx [root@Nginx conf.d]# mv testserver1.conf testserver5.conf [root@Nginx conf.d]# nginx -s reload #3.再次访问时, 由code2.conf返回输出信息 [root@Nginx conf.d]# curl 192.168.69.113 <h1>Code 2</h1>
[root@web01 conf.d]# cat server4.conf server { listen 80 default_server; server_name _; return 503; }
[root@web01 conf.d]# cat server4.conf server { listen 80 default_server; server_name _; return 302 https://www.xuliangwei.com; }
一台服务器配置多个网站,若是配置都写在nginx.conf主配置文件中,会致使nginx.conf主配置文件变得很是庞大并且可读性很是的差。那么后期的维护就变得麻烦。 假设如今但愿快速的关闭一个站点,该怎么办?html
Include包含的做用是为了简化主配置文件,便于人类可读。java
inlcude /etc/nginx/online/*.conf #线上使用的配置 /etc/nginx/offline #保留配置,不启用(下次使用在移动到online中)nginx
root与alias路径匹配主要区别在于nginx如何解释location后面的uri,这会使二者分别以不一样的方式将请求映射到服务器文件上,alias是一个目录别名的定义,root则是最上层目录的定义。 root的处理结果是:root路径+location路径 alias的处理结果是:使用alias定义的路径web
[root@Nginx ~]# mkdir /local_path/code/request_path/code/ -p [root@Nginx ~]# echo "Root" > /local_path/code/request_path/code/index.html //Nginx的root配置 [root@Nginx ~]# cat /etc/nginx/conf.d/root.conf server { listen 80; index index.html; location /request_path/code/ { root /local_path/code/; } } //请求测试 [root@Nginx conf.d]# curl http://192.168.69.113/request_path/code/index.html Root //实际请求本地文件路径为 /local_path/code/'request_path/code'/index.html
[root@Nginx ~]# mkdir /local_path/code/request_path/code/ -p [root@Nginx ~]# echo "Alias" > /local_path/code/index.html //配置文件 [root@Nginx ~]# cat /etc/nginx/conf.d/alias.conf server { listen 80; index index.html; location /request_path/code/ { alias /local_path/code/; } } //测试访问 [root@Nginx ~]# curl http://192.168.69.113/request_path/code/index.html Alias //实际访问本地路径 /local_path/code/'index.html'
server { listen 80; server_name image.oldboy.com; location / { root /code; } location ~* ^.*\.(png|jpg|gif)$ { 请求图片回到alias定义的路径去找 alias /code/images/; } }
[root@bgx ~]# cat /etc/nginx/conf.d/try_file.conf server { listen 80; server_name try.bgx.com; root /code; location / { try_files $uri $uri/ /404.html; } }
$uri匹配文件名,如用户请求try.oldboy.com/index.html那么$uri则会上对应的root指定的站点目录中查找是否存在该文件 $uri/匹配目录下的文件,如用户请求try.oldboy.com/,那么$uri/则会上/对应root指定的站点目录中查找文件面试
[root@Nginx ~]# echo "Try-Page" > /soft/code/index.html [root@Nginx ~]# echo "Tomcat-Page" > /soft/app/apache-tomcat-9.0.7/webapps/ROOT/index.html //启动tomcat [root@Nginx ~]# sh /soft/app/apache-tomcat-9.0.7/bin/startup.sh //检查tomcat端口 [root@Nginx ~]# netstat -lntp|grep 8080 tcp6 0 0 :::8080 :::* LISTEN 104952/java
[root@web01 conf.d]# cat try.conf server { listen 80; server_name try.oldboy.com; root /code; index index.html; location / { try_files $uri $uri/ @java_page; #:@是内部跳转,会找location匹配 } location @java_page { proxy_pass http://172.16.1.8:8080; } } //重启Nginx [root@Nginx ~]# nginx -s reload
[root@Nginx ~]# curl http://192.168.69.113/index.html Try-Page //将/soft/code/index.html文件移走 [root@Nginx ~]# mv /soft/code/{index.html,index.html_bak} //发现由Tomcat吐回了请求 [root@Nginx ~]# curl http://192.168.69.113/index.html Tomcat-Page
在nginx使用过程当中,上传文件的过程当中,一般须要设置nginx报文大小限制。避免出现413 Request Entity Too Large正则表达式
nginx上传文件大小限制配置语法数据库
Syntax:client_max_body_size size; Default:client_max_body_size 1m; Context:http,server,location
nginx上传文件大小限制配置示例: 也能够放在http层,全局生效apache
server{ ... client_max_body_size 200m; ... }
error_page错误日志vim
[root@web01 conf.d]# cat error.conf server { listen 80; server_name try.oldboy.com; root /code; location / { index index.html; } location ~ \.php$ { fastcgi_pass 127.0.0.0:9000; } #如服务器返回以下错误状态码,则进行跳转,跳转至/xxx.jpg #error_page 403 404 /40x.jpg; #error_page 500 502 503 404 /50x.jpg; #精准定位错误类型 error_page 403 /403.jpg; error_page 404 /404.jpg; #精准匹配访问 location = /404.jpg { root /code/err; 这会到/code/err目录下找404.jpg } location = /50x.jpg { root /code/err; } }
一个server出现多个location
完整匹配 | 优先级高 |
---|---|
= | 进行普通字符精确匹配, 彻底匹配 |
^~ | 表示普通字符匹配, 使用前缀匹配 |
正则匹配 | 匹配后会继续查找更精确匹配的location |
~ | 区分大小写匹配 |
~* | 不区分大小写 |
[root@Nginx conf.d]# cat testserver.conf server { listen 80; server_name 192.168.69.113; root /soft; index index.html; location = /code1/ { rewrite ^(.*)$ /code1/index.html break; } location ~ /code* { rewrite ^(.*)$ /code3/index.html break; } location ^~ /code { rewrite ^(.*)$ /code2/index.html break; } }
[root@Nginx conf.d]# curl http://192.168.69.113/code1/ <h1>Code 1</h1> //注释掉精确匹配=, 重启Nginx [root@Nginx ~]# curl http://192.168.69.113/code1/ <h1>Code 2</h1> //注释掉^~, 重启Nginx [root@Nginx ~]# curl http://192.168.69.113/code1/ <h1>Code 3</h1>
Nginx传递用户的真实IP地址 $remote_addr 只能获取到最近一台服务器访问IP
角色 | 外网IP(NAT) | 内网IP(LAN) | 安装工具 |
---|---|---|---|
web01 | eth0:10.0.0.7 | eth1:172.16.1.7 | nginx |
lb01 | eth0:10.0.0.5 | eth1:172.16.1.5 | nginx |
lb02 | eth0:10.0.0.6 | eth1:172.16.1.6 | nginx |
[root@web01 ~]# cat /etc/yum.repos.d/nginx.repo [nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/7/$basearch/ gpgcheck=0 enabled=1 #安装Nginx [root@web01 ~]# yum install nginx -y
systemctl start nginx systemctl enable nginx
修改日志格式
写配置文件
[root@web01 conf.d]# cat proxy.web.xly.com.conf server { listen 80; server_name web.xly.com; location / { root /web; index index.html; } }
[root@lb01 conf.d]# vim proxy.web.xly.com.conf server { listen 80; server_name web.xly.com; location / { proxy_pass http://10.0.0.6:80; include proxy_params; } }
[root@lb02 conf.d]# cat proxy.web.xly.com.conf server { listen 80; server_name web.xly.com; location / { proxy_pass http://10.0.0.7:80; include proxy_params; } }
10.0.0.5 web.xly.com
使用nginx Realip_module获取多级代理下的客户端真实IP地址,须要在Web上配置
set_real_ip_from 10.0.0.5; set_real_ip_from 10.0.0.6; set_real_ip_from 10.0.0.7; real_ip_header X-Forwarded-For; real_ip_recursive on;
set_real_ip_from:真实服务器上一级代理的IP地址或者IP段,能够写多行 real_ip_header:从哪一个header头检索出须要的IP地址 real_ip_recursive:递归排除set_real_ip_from里面出现的IP,其他没有出现的认为是用户真实IP 例如: "10.0.0.1, 10.0.0.5, 10.0.0.6" 10.0.0.5,10.0.0.6都出如今set_real_ip_from中,仅仅10.0.0.1没出现,那么他就被认为是用户的ip地址,而且赋值到$remote_addr变量
200 正常请求 301 永久跳转 302 临时跳转 400 请求参数错误 401 帐户密码错误(authorization required) 403 权限被拒绝(forbidden) 404 文件没找到(Not Found) 413 用户上传文件大小限制(Request Entity Too Large) 502 后端服务无响应(boy gateway) 504 后端服务执行超时(Gateway Time-out)
网站相关术语 若是一栋大厦里全部工做人员经过1个IP公网接口上网, 总共100个设备, 当全部人同时请求一个网站, 而且刷新了5次, 那么请求pv、ip、uv分别是多少
pv:页面浏览量 500 uv:惟一设备100 ip:惟一出口 1
面试时需注意: 1.按照分层结构 CDN层->负载层->WEB层->存储层->缓存层->数据库层 同时须要注意, 每一层都有对应的缓存机制
Nginx优化
1.gzip压缩 2.expires静态文件缓存 3.调整网络IO模型,调整Nginx worker进程的最大链接数 5.隐藏Nginx名称和版本号 6.配置防盗链,防止资源被盗用 7.禁止经过IP地址访问,禁止恶意域名解析,只容许域名访问 8.防DDOS、cc攻击, 限制单IP并发请求链接 9.配置错误页面,根据错误代码指定网页反馈用户 10.限制上传资源目录被程序访问,防止木马入侵系统 11.Nginx加密传输优化
基于Nginx中间件的架构
静态资源服务的功能设计 类型分类(视频、图片、html) 浏览器缓存 防盗链 流量限制 防资源盗用 压缩(压缩模式, 压缩比例, 压缩类型) 代理服务 协议类型 正向代理 反向代理 负载均衡 代理缓存 头信息处理 Proxy_Pass LNMP 动静分离
硬件 CPU、内存、磁盘 系统(用户权限、日志目录存放) 代理服务/负载均衡 (CPU、内存) 静态资源服务(硬盘容量、硬盘转速) 动态资源服务(硬盘转速、读写效率) 缓存资源服务(SSD固态)
合理配置 了解原理 http协议原理 http状态原理 操做系统原理 关注日志 日志是否有打开 是否有对应请求 请求状态码信息符合 错误日志信息吐出来 错误日志内容和含义